Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Always query certain number of posts
Old 09-08-2009, 06:56 AM Always query certain number of posts
Junior Talker

Posts: 2
Name: Levani
Trades: 0
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 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?
levani is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-08-2009, 07:35 PM Re: Always query certain number of posts
portkey's Avatar
Skilled Talker

Posts: 54
Name: Erica Dion
Trades: 0
I'm confused by what you're asking. Do you have any code to show for querying the comments?

If you're just looking to add something into the 'IF' cases, you can always manually print something like "No Comments" or sort through some of the comments with a while loop and a second query.
__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
Old 09-09-2009, 04:41 AM Re: Always query certain number of posts
Experienced Talker

Posts: 45
Trades: 0
PHP Code:
<?php
        
global $wpdb;
        
$posts $wpdb->get_results("SELECT * FROM wp_posts WHERE post_author = $id AND post_type = 'post'");

    
// Number of commented posts
    
$counter 0;

        foreach (
$posts as $post) {

    
// we have reached 5 commented posts
    
if ($counter == 5)
        break;

        if (
$post->comment_count >= 1) {
        echo 
'<li>' $post->post_title '</li>';
    
$counter++;
            }
?>
This will query all the posts from your DB but display the first 5 commented posts. Same goes for uncommented posts.
__________________

Please login or register to view this content. Registration is FREE
: Learn more about Web Hosting!
arbet is offline
Reply With Quote
View Public Profile Visit arbet's homepage!
 
Reply     « Reply to Always query certain number of posts
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11524 seconds with 12 queries