![]() |
Negation in Database Query - 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: Negation in Database Query (/showthread.php?tid=24354) |
Negation in Database Query - El Forum - 11-07-2009 [eluser]nicholas.byfleet[/eluser] Hello. I am trying to show "related posts" in a blogging module of my own CI CMS. This related post section is just a list of 5 of the most recent posts in the same category as the current post being shown. Here is some of the code in my view: Code: <?php echo "<h3>{$category->name}</h3><p>{$category->description}</p>"; Here's my question: I want to exclude the current post being viewed from the list and was wondering if there is a way to use active record syntax to specify negation in the get_where statement. In normal SQL, I might write: Code: $sql = "SELECT * FROM `posts` WHERE `category`='".$category->id."' AND `id` != '".$id."' LIMIT 0,5 ORDER BY `date` DESC;"; Code: AND `id` != '".$id."' Negation in Database Query - El Forum - 11-07-2009 [eluser]nicholas.byfleet[/eluser] Oh, and I just caught my $this->db->select(); error. Sorry for that. Negation in Database Query - El Forum - 11-07-2009 [eluser]mah0001[/eluser] Try: Code: $this->db->where('id !=', $id);//ID of the post you want to exclude Negation in Database Query - El Forum - 11-07-2009 [eluser]nicholas.byfleet[/eluser] Perfect! Thanks so much for this tip. This would be a good thing to include in the documentation methinks. Negation in Database Query - El Forum - 11-07-2009 [eluser]mah0001[/eluser] It is already in the documentation. see http://ellislab.com/codeigniter/user-guide/database/active_record.html Negation in Database Query - El Forum - 11-07-2009 [eluser]nicholas.byfleet[/eluser] Wow, i feel like an idiot. Believe it or not I did read through the documentation before posting my question, however, since I wanted to condense the code into a get_where statement, I overlooked the where method documentation. In any case, thanks again! |