...

View Full Version : Always query certain number of posts



levani
09-08-2009, 11:41 AM
Hello,

I need small help with php. This is my code which queries latest 5 post title from database. I would like to query commented and uncommented posts separately and wrote this code:


<?php
global $wpdb;
$posts = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_author = $id AND post_type = 'post' LIMIT 5 ");
foreach ($posts as $post) {

if ($post->comment_count >= 1) {
echo '<li>' . $post->post_title . '</li>';
}
else if ($post->comment_count < 1) {
echo '<li>' . $post->post_title . '</li>';
}
}
?>

But there is one big problem. If the latest five posts are all uncommented when I view commented posts it doesn't display anything. I know that it happens because of comparing comments number to 1 and showing posts only after it, but I don't know any better ways.

Can anyone please help?

Fumigator
09-08-2009, 05:17 PM
So you always want the most recent 5 commented posts as well as the most recent 5 uncommented posts? I would just remove the LIMIT 5, and use a counter variable in the PHP code as you get a match for either type. Once both types have 5, break out of the loop.



foreach ($posts as $post) {

if ($post->comment_count >= 1 && $postsWithComment < 5) {
$postsWithComment++;
echo '<li>' . $post->post_title . '</li>';
}
else if ($post->comment_count < 1 && $postsWithoutComment < 5 ) {
$postsWithoutComment++;
echo '<li>' . $post->post_title . '</li>';
}
if ($postsWithComment >= 5 && $postsWithoutComment >= 5) {
break;
}
}


You can avoid retrieving too much data by fetching rows one at a time and then performing the tests, as opposed to calling a method that retrieves the entire resultset at once.

Fou-Lu
09-09-2009, 01:17 AM
What about a union query to take care of this? I think that using a LIMIT on the union will consider each as a resultset (ie: LIMIT 5 limits 5 of each union)? If that's wrong, than it should work on a limit 10 I would suspect.
Course, I guess that would all be different if the where included a timestamp of somesorts...

abduraooft
09-09-2009, 08:34 AM
if ($post->comment_count >= 1) {
echo '<li>' . $post->post_title . '</li>';
}
else if ($post->comment_count < 1) {
echo '<li>' . $post->post_title . '</li>';
}
} Is there any difference in the above two blocks after all? Are you going to echo them in seperate lists? :confused:

Fumigator
09-09-2009, 04:51 PM
What about a union query to take care of this? I think that using a LIMIT on the union will consider each as a resultset (ie: LIMIT 5 limits 5 of each union)? If that's wrong, than it should work on a limit 10 I would suspect.
Course, I guess that would all be different if the where included a timestamp of somesorts...

If you could use separate LIMITs on each query in the union, then yeah I'd recommend this too, but LIMIT only applies to what drops out the bottom end of the query, so there would be no way to grab 5 of one and 5 of the other every time.


Is there any difference in the above two blocks after all? Are you going to echo them in seperate lists?

That's a good point :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum