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

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:

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:

$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

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.

How do i pass it back to the model?

I can add

$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

No, in your model, modify your function

function getEntries($category = FALSE)
        … your code
        … your code


and in your controller, if you want by category
$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.