[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 -> <?php echo $news_item['category']; ?>
</p>
<p>
</p>
<div class = listing>
<?php echo $news_item['title'] ?></a> - <?php echo $news_item['price'] ?>
</div>
<?php endforeach ?>