views:

41

answers:

1

When I set my pagination to display 10 comments at a time my comments query wont count the comments replies as part of the display count how can I fix this so that my comments replies are counted? My comments replies queries are nested in my main query to display comments.

Query for pagination

SELECT COUNT(comment_id) FROM comments WHERE id = $id

The main query to display comments.

$dbc = mysqli_query($mysqli,"SELECT comments.*, users.*
                            FROM comments
                            LEFT JOIN users 
                            ON comments.user_id = users.user_id
                            WHERE id = '" . $id . "'
                            AND parent_comment_id = 0
                            LIMIT $start, $display");

The main querys reply comments.

//display comments replies
$dbc2 = mysqli_query($mysqli, "SELECT comments.*, users.*
                               FROM comments
                               LEFT JOIN users 
                               ON comments.user_id = users.user_id
                               WHERE id = '" . $id . "'
                               AND parent_comment_id >= 1");


//display comments replies
$dbc3 = mysqli_query($mysqli, "SELECT comments.*, users.*
                              FROM comments
                              LEFT JOIN users 
                              ON comments.user_id = users.user_id
                              WHERE id = '" . $id . "'
                              AND parent_comment_id >= 1");
A: 

Maybe this query, could you give the precise DB structure to work with.

SELECT 
    com1.*, 
    users.*,
    (
        SELECT COUNT(*)
        FROM comments AS com2
        WHERE com2.parent_comment_id = com1.id
    ) AS num_replies
FROM comments AS com1
LEFT JOIN users ON com1.user_id = users.user_id
WHERE 
        com1.id = '" . $id . "'
    AND com1.parent_comment_id = 0
LIMIT $start, $display
Igor