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;
#2

[eluser]CI_expert_indian[/eluser]
Please check the function "get_news_cate" in model ... here in line {{{$query = $this->db->get_where('news', array('category' => $category));}}} hows you defined $category here .... >>> Check it out Smile
#3

[eluser]codelogic[/eluser]
[quote author="deep_sheera" date="1338528911"]Please check the function "get_news_cate" in model ... here in line {{{$query = $this->db->get_where('news', array('category' => $category));}}} hows you defined $category here .... >>> Check it out Smile
[/quote]

Thanks for the response.

I'm defining it in my Controller
Code:
$category = $this->input->post('category');
#4

[eluser]CI_expert_indian[/eluser]
But dear you not pass that value to “get_news_cate” function... try to echo $category in model then verify it........ can you can provide line where error is coming ..... thanks
#5

[eluser]CI_expert_indian[/eluser]
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)); /// Is $category variable available here ???????????????
return $query->row_array();
}
//Displays a seperate page for selected listing
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

#6

[eluser]codelogic[/eluser]
[quote author="deep_sheera" date="1338529899"]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)); /// Is $category variable available here ???????????????
return $query->row_array();
}
//Displays a seperate page for selected listing
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

[/quote]

Am I using the get_where(); function correctly? I am trying to display all the data in a column in my table 'news'. The column name is 'category'. I am not sure if I am doing this right...
#7

[eluser]CI_expert_indian[/eluser]
there is no problem in column name .... problem is about the variable $column. Try to use

$query = $this->db->get_where(‘news’, array(‘category’ =>'books')); in this i replace $variable with static value 'books' . you can use any other category name instead of book ...try this ....
#8

[eluser]codelogic[/eluser]
[quote author="deep_sheera" date="1338531854"]there is no problem in column name .... problem is about the variable $column. Try to use

$query = $this->db->get_where(‘news’, array(‘category’ =>'books')); in this i replace $variable with static value 'books' . you can use any other category name instead of book ...try this ....[/quote]

Thanks for the help thus far.

This is what I get when I do that:

Code:
Categories -> 3

3 - 3
Categories -> 2

2 - 2
Categories -> 2

2 - 2
Categories -> 2

2 - 2
Categories -> 2

2 - 2
Categories -> b

b - b





Edit: I changed row_array(); to result_array(); and it works now! Thank you very much!

However, just a little problem, one thing is repeating which I dont want to repeat, and moving it above the foreach(); loop causes an error saying $news_item is undefined. Any ideas? But leaving it in the foreach() returns Categories -> Books repeating.


Code:
Categories -> Books

2 - 2

Categories -> Books

1 - 1


Here is what my view code looks like again


Code:
&lt;?php foreach ($news as $news_item): ?&gt;
  <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;
#9

[eluser]CI_expert_indian[/eluser]
check the value before forach by using print_r($news) .... from this array you will get the idea hows to print category name before the loop.....




Theme © iAndrew 2016 - Forum software by © MyBB