CodeIgniter Forums
Query Questions... newbie - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Query Questions... newbie (/showthread.php?tid=35492)



Query Questions... newbie - El Forum - 10-31-2010

[eluser]Unknown[/eluser]
I am creating my first CI sample app I need some help trying to query through some car inventory

I am trying to create a menu with available makes & models, how do I loop through the available inventory to return all like makes & models from the table. Thanks for any help.

<div>Make 1
<ul>
<li>Model 1 (Count)</li>
<li>Model 2 (Count)</li>
<li>Model 3 (Count)</li>
</ul>
</div>
<div>Make 2
<ul>
<li>Model 1 (Count)</li>
<li>Model 2 (Count)</li>
<li>Model 3 (Count)</li>
</ul>
</div>


Query Questions... newbie - El Forum - 10-31-2010

[eluser]Davcon[/eluser]
Hi,

I'll assume that you know how to build a model that does database queries. I'll also assume that you know how to build view pages and you know how get them to display variables.

So, here's how I'd do it...

ON YOUR CONTROLLER...
Code:
&lt;?php
session_start();
class Cars extends Controller {
    function Cars() {
        parent::Controller();
    }





function index() {

    //get all the makes from the database...
    $this->load->model('cars_model');
    $makes=$this->car_model->get_makes();

        //now, start building up an html string that contains
        //all of the information about the makes and models...

        if (!isset($html)) {
        $html="";
        }



                   foreach($makes as $make) {
                        $html.="<ul>$make</ul>";
                            $models=$this->car_model->get_models($make);                        
                            foreach($models as $model) {
                            $html.="<li>$model</li>";
                            }
                   }

$data['html']=$html;
$this->load->view('cars', $data);

}
?&gt;

cheap bike insurance

bike insurance


Query Questions... newbie - El Forum - 10-31-2010

[eluser]Unknown[/eluser]
Thanks this was just what I was looking for.


Query Questions... newbie - El Forum - 10-31-2010

[eluser]Davcon[/eluser]
No problem mate. Rock on!