开发者

Passing Results from SQL to Google Maps API in CodeIgniter

I'm hoping to use Google Maps on my site.

My addresses are stored in a DB. I’m pulling up a page where the information is all dynamic. For example: mysite.com/site/business/5 (where 5 is the id of the business).

Let’s say I do a query like this:

function addressForMap($id) {
$this->db->select('b.id, b.busaddress, b.buscity, b.buszip');
$this->db->from('business as b'开发者_JAVA技巧);
$this->db->where('b.id', $id);
$this->db->limit(1);

// add a limit $this->db->limit(1);

// get the results.. cha-ching
$q = $this->db->get();

// any results?
if($q->num_rows() !== 1)
{
    return FALSE;
}

return $q->row();

}

How can I output the info to the Google Maps API correctly so that it displays the map appropriately?

The API interface takes the results like this: $marker['address'] = 'Crescent Park, Palo Alto';.

Here's what I have in the controller:

$marker = array();
    $address = $this->Business_model->addressForMap($id); //Get result row
    $marker['address'] = $address->busaddress .' '.$address->buscity .', '. $address->buszip;


The MODEL

function addressForMap($id)
{
    $this->db->select('b.id, b.busaddress, b.buscity, b.buszip');
    $this->db->from('business as b');
    $this->db->where('b.id', $id);

    // add a limit
    $this->db->limit(1);

    // get the results.. cha-ching
    $q = $this->db->get();

    // any results?
    if($q->num_rows() !== 1)
    {
        return FALSE;
    }

    return $q->row();
}

The CONTROLLER

function your_controller_method($id)
{
    $data = $this->Business_model->addressForMap($id);

    // did we get any results?
    if($data === FALSE)
    {
        show_error();
        return;
    }  

    $marker['address'] = $data->busaddress.' '.$data->buscity.', '.$data->buszip;
}

Make sure you validate that $id is an actual numerical id. It's a good thing to do before actually performing the query.. IMO.

EDIT #2: See changed code above


EDIT #1: From your comment and changes

You've got this:

return $this->db->get();

when, based on the rest of your code, it should be this:

$query = $this->db->get();

When you return the query object, the variable $address gets assigned the CI result object. so, in your controller, you would actually have to do something like this:

$address = $this->Business_model->addressForMap($id);

$row = $address->row();

$marker['address'] = $row->busaddress .' '.$row->buscity .', '. $row->buszip;

Still... I would suggest changing return to $query = and you could keep your controller as it is.


Here's the Final correct code:

For the Controller:

$address = $this->Business_model->addressForMap($id); //Get result row
        $config['apikey'] = 'google-api-key';
        $config['center_address'] =  $address->busaddress .', '. $address->buscity;
        $this->googlemaps->initialize($config);
        $marker = array();

        // $marker['address'] = $address->busaddress .' '.$address->buscity .', '. $address->buszip;
        $marker['address'] = $address->busaddress .', '. $address->buscity;
        $this->googlemaps->add_marker($marker);

        $data['map'] = $this->googlemaps->create_map();

        $data['address'] = $address->busaddress .', '.$address->buscity;

For the Model:

function addressForMap($id) {
    $this->db->select('b.id, b.busaddress, b.buscity, b.buszip');
    $this->db->from('business as b');
    $this->db->where('b.id', $id);
    $this->db->limit(1);
// add a limit
    $this->db->limit(1);

    // get the results.. cha-ching
    $q = $this->db->get();

    // any results?
    if($q->num_rows() !== 1)
    {
        return FALSE;
    }

    return $q->row();
}


Thanks for using my library. It looks I posted the solution on my blog at near enough exactly the same time as you did on here :-P

Glad to hear you got it sorted though!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜