![]() |
Simple question get_where() function - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Simple question get_where() function (/showthread.php?tid=52185) |
Simple question get_where() function - El Forum - 05-31-2012 [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) Controller - This creates a function books() that sends the data over to books.php(a view). Code: public function books() View - This displays all my listings in the "books" category: Code: <?php foreach ($news as $news_item): ?> Simple question get_where() function - El Forum - 05-31-2012 [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 ![]() Simple question get_where() function - El Forum - 05-31-2012 [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 ![]() [/quote] Thanks for the response. I'm defining it in my Controller Code: $category = $this->input->post('category'); Simple question get_where() function - El Forum - 05-31-2012 [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 Simple question get_where() function - El Forum - 05-31-2012 [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(); } Simple question get_where() function - El Forum - 05-31-2012 [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... Simple question get_where() function - El Forum - 05-31-2012 [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 .... Simple question get_where() function - El Forum - 05-31-2012 [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 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 Here is what my view code looks like again Code: <?php foreach ($news as $news_item): ?> Simple question get_where() function - El Forum - 06-01-2012 [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..... |