• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter database results?

#1
[eluser]Uplift[/eluser]
Hi, How can i filter results coming from the database? in this case it's by category,

i use the following code in my model:

Code:
function getEntries() {
    
     $this->db->order_by("id", "desc");
     $query = $this->db->get_where('work', array('published' => '1'));
    
     return $query->result_array();

}

and controller to call it:

Code:
$data['entries'] = $this->work_model->getEntries();

All pretty standard stuff but how do i filter by category?

eg, if user clicks 'all' it shows all entries, if user clicks 'web' it shows all websites

the links would be something like

/portfolio/ - if they click this link it would show all
/portfolio/web - if they click this link it will show only all websites
/portfolio/graphics - if they click this link it will show only all graphics

#2
[eluser]CroNiX[/eluser]
Check to see if there is a value for the 2nd url segment (or whatever segment "web" and "graphics" would be in), if there is pass it to your getEntries() method and in that method add it to the WHERE.

#3
[eluser]Uplift[/eluser]
How do i pass it back to the model?

I can add

Code:
$category = $this->uri->segment(2, 0); // returns web, graphics or nothing

it's adding the get_where category = $category i'm not sure about?

Sorry i'm still new to CI

#4
[eluser]Noobigniter[/eluser]
No, in your model, modify your function

Code:
function getEntries($category = FALSE)
{
    if($category)
    {
        … your code
    }
    else
    {
        … your code
    }

}

and in your controller, if you want by category
Code:
$data['entries'] = $this->work_model->getEntries($this->uri->segment(2, 0));

I think it is like this and I did not make any mistakes. ( Noobigniter ^^)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.