Welcome Guest, Not a member yet? Register   Sign In
Get dropdown data and use it to search db and display results
#1

[eluser]bondjp[/eluser]
Hi, i need a little help and i couldn't find any searching the forum.
Problem:
I have a dropdown and i need to do a query to the db based on the selection from the dropdown and show that data on a new page.
Here's my code:

controller
Code:
funtion index {
        $data['brand'] = $this->HPage->getBrand();
        $data['model'] = $this->HPage->getModel();
        $this->load->view('home', $data);
}

model
Code:
function getBrand(){
        $data = array();
        
        $q = $this->db->get('brand');
        if($q->num_rows() > 0) {
            return $q->result();
        }
        return null;
    }
    
    function getModel(){
        $data = array();
        
        $q = $this->db->get('model');
        if($q->num_rows() > 0) {
            return $q->result();
        }
        return null;
    }

View
Code:
<?php
   echo form_open('searchresults');
   foreach($brand as $row):
   $brands[$row->brand] = &$row->brand;
   endforeach;
   foreach($model as $row):
   $models[$row->model] = &$row->model;
   endforeach;
?>
<h3> Brand: &lt;?php echo form_dropdown('brand', $brands);?&gt;</h3>
            
<h3> Model: &lt;?php echo form_dropdown('model', $models);?&gt;</h3>
            
<p>&lt;?php echo form_submit('searchresults', 'Search');?&gt;</p>
&lt;/form&gt;

Everything works up to the part when i hit "Search.
Now how can i get the user's choice and do a query to display the models and brands on the searchresults page?
Thanks
#2

[eluser]Frank Rocco[/eluser]
Try this:

$brand = $this->input->post('brand');
$model = $this->input->post('model');

$this->db->like("brand", $brand);
$this->db->like("model", $model);
$query = $this->db->get('table');

foreach($query->result() as $row)
{
...
}

HTH

Frank
#3

[eluser]bondjp[/eluser]
[quote author="Frank Rocco" date="1265863167"]Try this:

$brand = $this->input->post('brand');
$model = $this->input->post('model');

$this->db->like("brand", $brand);
$this->db->like("model", $model);
$query = $this->db->get('table');

foreach($query->result() as $row)
{
...
}

HTH

Frank[/quote]

Ok this goes to my new model, right?
#4

[eluser]bondjp[/eluser]
Ok, created a new model and now my results page show "Array" instead of the $brand and $model values.
What do you guys think it's wrong?
Thanks

model
Code:
&lt;?php
class Results extends Model{
    function Results(){
        parent::Model();
    }


function searchBrand(){
    $data=array();
    $brand = $this->input->post('brand');
    $this->db->like('brand', $brand);
    $query = $this->db->get('brand');

    foreach($query->result() as $row){
        $data[] = $row;
    }
    $query->free_result();
    return $data;
    }


function searchModel(){
    $data=array();
    $model = $this->input->post('model');
    $this->db->like('model', $model);
    $query = $this->db->get('model');

    foreach($query->result() as $row){
        $data[] = $row;
        }
    $query->free_result();
    return $data;
    }
}

controller
Code:
&lt;?php
class searchResult extends Controller {

    function searchResult()
    {
        parent::Controller();    
        $this->load->model('Results','',TRUE);
    }
    
    function index(){
        $data = array();
        $data['brand'] = $this->Results->searchBrand();
        $data['model'] = $this->Results->searchModel();
        $this->load->view('searchresult', $data);    
    }
    
    
}


View
Code:
<p>&lt;?php echo $brand;?&gt;</p>
<p>&lt;?php echo $model;?&gt;</p>
#5

[eluser]bondjp[/eluser]
Isn't there anyone who can help out with this one? Smile
I'm really stuck so i need a little guidance.
Thanks.
#6

[eluser]danmontgomery[/eluser]
Code:
foreach($query->result() as $row){
        $data[] = $row;
}

You're passing an array directly to the view.

Code:
$row = $query->row();
$data['brand'] = $row->brand;
$data['model'] = $row->model;
#7

[eluser]bondjp[/eluser]
Thanks for replying.
Although i couldn't make your example work(stll gave me "Array"), i did this instead and got it working:

View
Code:
&lt;?php if (isset($brand)) :
    foreach($brand as $row):
           echo $row->brand;
    endforeach;
    foreach($model as $row):
        echo $row->model;
    endforeach;    
        
            
       else :?&gt;
              <h2>No Records Available</h2>
        
&lt;?php endif ;?&gt;

Result:
ToyotaPrius

Is this good practice?
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB