Negation in Database Query |
[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."'
[eluser]nicholas.byfleet[/eluser]
Oh, and I just caught my $this->db->select(); error. Sorry for that.
[eluser]mah0001[/eluser]
Try: Code: $this->db->where('id !=', $id);//ID of the post you want to exclude
[eluser]nicholas.byfleet[/eluser]
Perfect! Thanks so much for this tip. This would be a good thing to include in the documentation methinks.
[eluser]mah0001[/eluser]
It is already in the documentation. see http://ellislab.com/codeigniter/user-gui...ecord.html
[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! |
Welcome Guest, Not a member yet? Register Sign In |