Welcome Guest, Not a member yet? Register   Sign In
Sanity check: Using Model to generate Google Maps code, pass to view
#1

[eluser]mtbkrdave[/eluser]
Hi Y'all - CI newbie checking back in with another [hopefully] quick question...

So I want to include a Google Map on some of the pages of my CI site. To do this, I've got to drop the Map generation/control javascript in my header, then hold a spot for it in my body section. I'm using different views for header, body and footer.

I'm just about to bite into coding, and my plan at this point is to:

1) Use a model to define several functions which pre-build the GMap javascript code for the different maps I need, dipping into my MySQL database to pull coordinates for each map's marker[s].

2)Pass a single $data array from controller to view, optionally containing map code if the page is supposed to have a map.

3) Put a conditional (e.g. if( isset ( $data["map_source"] ) {} ) into the header view that only echoes out the map source code if it's been inserted by the controller.

Am I even close to the right way to do this?

Thanks,
[medic]Dave
#2

[eluser]fesweb[/eluser]
Your basic premise sounds fine.

It's not necessary, but you can also have the function in the model in your model return FALSE if you want to make testing for its presence easy.

Code:
// controller

$data['map_source'] = $this->map_model->get_map_source($id);
$data['other_stuff'] = 'other info...';

$this->load->view('my_page', $data);

// model
function get_map_source($id = 0)
{
   // get the map info from the db
    $query = $this->db etc...
   if($query->num_rows() > 0)
   {
     // do results stuff
     return your data;
   }
   else
   {
     return FALSE;
   }
}

// view    
if($map_source) {
   // show it...
   echo $map_source;
}
echo $other_stuff;
#3

[eluser]mtbkrdave[/eluser]
Hey, thanks for the fast reply - I like that method better than using the isset() function. Seems to make more sense as MVC goes, and allows more future flexibility.

Thanks again!
[medic]Dave




Theme © iAndrew 2016 - Forum software by © MyBB