Question

I have a PHP method which returns a json string as follows:

function getjson(){
$mun = $this->input->POST('municipality');
$sl  = $this->input->POST('saleslimit'); 
$q = $this->db->query("SELECT RollNum , Address, v2_lat, v2_lng  
    FROM tblontario WHERE Municipality = '".$mun."'"." LIMIT ".$sl);            
        $json_string = json_encode($q->result(), JSON_PRETTY_PRINT);
        file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.json', $json_string);
        echo $json_string;
}

Rather than return a json string, I would like to be able to return a CSV file so that I can then take that CSV file and somehow turn it into a geojson file that can be loaded into a google map.

Any ideas on how to accomplish this? Ultimately my goal is to create a geojson file, but I am uncertain of the best way to do this. Thanks.

Was it helpful?

Solution

If $row is an array you can just do something like:

function getCSV() {
    $mun = $this->input->POST('municipality');
    $sl  = $this->input->POST('saleslimit'); 
    $q = $this->db->query("
        SELECT 
            RollNum, 
            Address,
            v2_lat,
            v2_lng 
        FROM
            tblontario 
        WHERE 
            Municipality = '".$mun."'"." 
        LIMIT ".$sl
    );
    $row = $q->result();
    $csvRow = implode(",",$row);
    file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv', $csvRow);
    echo $csvRow;
}

Since I see a limit there, I'm guessing you're getting more than one result and that $q->result(); returns an array of results instead of just a row, in which case..

function getCSV() {
    $mun = $this->input->POST('municipality');
    $sl  = $this->input->POST('saleslimit'); 
    $q = $this->db->query("
        SELECT 
            RollNum, 
            Address,
            v2_lat,
            v2_lng 
        FROM
            tblontario 
        WHERE 
            Municipality = '".$mun."'"." 
        LIMIT ".$sl
    );
    $res = $q->result();
    if(!empty($res)){
        $fh = fopen('/home/apts/Dropbox/ptax.ca/js/salecomps.csv','w');
        foreach($res as $row){
            // as suggested by @GigaWatt, swapped fwrite for fputcsv
            fputcsv($fh,$row);
        }
        fclose($fh);
        echo file_get_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv');
    } else {
        echo "";
    }

}

EDIT: Looking a GeoJSON spec, what you prob want is something like:

function getGeoJSON() {
    $mun = $this->input->POST('municipality');
    $sl  = $this->input->POST('saleslimit'); 
    $q = $this->db->query("
        SELECT 
            RollNum, 
            Address,
            v2_lat,
            v2_lng 
        FROM
            tblontario 
        WHERE 
            Municipality = '".$mun."'"." 
        LIMIT ".$sl
    );
    $res = $q->result();
    if(!empty($res)){
        $geoArr = Array(
            "type" => "MultiPoint",
            "coordinates" => Array()
        );
        foreach($res as $row) {
            $geoArr["coordinates"][] = Array($row['v2_lat'],$row['v2_lng']);
        }
        $geoJSON = json_encode($geoArr);
        file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.geojson',$geoJSON);
        echo $geoJSON;
    } else {
        echo "{}";
    }

}

OTHER TIPS

From what I understand, you are basically looking for a getCSV() function. You can do this by manually concatenating the elements of the result set. Something like this.

function getCSV(){
    $mun = $this->input->POST('municipality');
    $sl  = $this->input->POST('saleslimit'); 
    $q = $this->db->query("SELECT RollNum , Address, v2_lat, v2_lng FROM tblontario WHERE Municipality = '".$mun."'"." LIMIT ".$sl);
    $row = $q->result();
    $csvString =  $row->{'RollNum'}.','.$row->{'Address'}.','.$row->{'v2 lat'}.','.$row->{'v2 lng'};
    file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv', $csvString);
    echo $csvString;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top