views:

158

answers:

3

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');
$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;
+2  A: 

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.

bschaeffer
I updated my post to show what I have for the model and the controller now. The problem I'm running into is I get Message: Undefined property: CI_DB_mysql_result::$buscity, Message: Undefined property: CI_DB_mysql_result::$buszip and Message: Undefined property: CI_DB_mysql_result::$busaddress
Jason Shultz
I see where you're problem is and edited my answer to reflect it.
bschaeffer
I'm getting Fatal error: Call to undefined method stdClass::row() in /home/welcomet/public_html/application/controllers/site.php on line 51 which is: $row = $address->row(); now?
Jason Shultz
i updated my code again.
Jason Shultz
You need to pick a method... either return the `$q->row()` object and just do `$address->buszip` in your controller, **OR** return the entire query object (`$q`) and do `$row = $address->row()` in your controller. I've edited my answer again to show the model and controller examples.
bschaeffer
i updated the code with the new code. i'm not getting any errors but i'm not getting the map to show the address either. so good and bad.
Jason Shultz
If your test site is accessible online, post a link to the page and I'll take a look at it.
bschaeffer
http://welcometojerome.com
Jason Shultz
I did a print_r of addressForMap($id), it's getting the proper data. it's just not working how i'm placing it into $marker['address'].
Jason Shultz
I got it working with thanks to you!
Jason Shultz
A: 

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();
}
Jason Shultz
A: 

Hi Jason,

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!

BIOSTALL
Thanks. Now that I got it working I'm loving it. :)
Jason Shultz
Part of what was throwing me, was that I thought the map would center automatically on wherever the marker was placed. I didn't realize that I had to center the map then place the marker. I probably had it working sooner but because no marker was showing on the screen and it stayed showing only Palo Alto, I thought i was still not doing something right.
Jason Shultz