Welcome Guest, Not a member yet? Register   Sign In
Simple question get_where() function
#1

[eluser]codelogic[/eluser]
Hi all,
I will try to describe my problem best I can. I have a table called 'news' in my database. Within that, a 'category' section, where I have a bunch of listings with a 'category' associated with them (i.e. Calculus book is the 'title', books is the category).

I am trying to display only book listings when a user visits /news/books/.
However, nothing is displaying, and I am getting a PHP error saying "Message: Undefined variable: category".

I'm sure it's just a minor problem... but it has been driving me crazy.

Thanks for all the help!


Here is my code that handles this process:


Based off testing for hours, I believe my error lines in the following line in my model.
However, I cannot figure out why.

Code:
$query = $this->db->get_where('news', array('category' => $category));



Model - This pulls the categories from the DB, and puts listings into the DB.
Code:
public function get_news_cate($slug = FALSE)
{
if ($slug === FALSE)
{
  // Displays our books page.
  $this->db->order_by("id", "desc");
  $query = $this->db->get_where('news', array('category' => $category));
  return $query->row_array();
}
//Displays a seperate page for selected listing
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}



//Puts listings into database.
public function set_news()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);
$category = $this->input->post('category');
$data = array(
  'title' => $this->input->post('title'),
  'slug' => $slug,
  'text' => $this->input->post('text'),
  'price' => $this->input->post('price'),
  'category' => $category
);

return $this->db->insert('news', $data);
}
}



Controller - This creates a function books() that sends the data over to books.php(a view).
Code:
public function books()
{
  $data['news'] = $this->news_model->get_news_cate();
  $data['title'] = 'Books';


$this->load->view('news/books', $data);



View - This displays all my listings in the "books" category:

Code:
<?php foreach ($news as $news_item): ?>
  <p>Categories -> &lt;?php echo $news_item['category']; ?&gt;
</p>
<p>
  
</p>
<div class = listing>
&lt;?php echo $news_item['title'] ?&gt;</a> -  &lt;?php echo $news_item['price'] ?&gt;
  </div>
  
  


&lt;?php endforeach ?&gt;


Messages In This Thread
Simple question get_where() function - by El Forum - 05-31-2012, 10:17 PM
Simple question get_where() function - by El Forum - 05-31-2012, 10:35 PM
Simple question get_where() function - by El Forum - 05-31-2012, 10:40 PM
Simple question get_where() function - by El Forum - 05-31-2012, 10:49 PM
Simple question get_where() function - by El Forum - 05-31-2012, 10:51 PM
Simple question get_where() function - by El Forum - 05-31-2012, 11:03 PM
Simple question get_where() function - by El Forum - 05-31-2012, 11:24 PM
Simple question get_where() function - by El Forum - 05-31-2012, 11:27 PM
Simple question get_where() function - by El Forum - 06-01-2012, 12:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB