I created an example (for me) of how I must get the comments and replies from the database.

Here is the result for example:

//I am ONLY thinking a query like this
$q = "SELECT * FROM comments_table WHERE post = 'url_code' ORDER BY date DESC";

//An example result (Not used the query above)

$comment = array(
            array(
                'id'            => '7',
                'user'          => 'Jul',
                'post_id'       => '1',
                'comment_id'    => '7',
                'reply_id'      => '1',
                'date'          => 'Jul 2016',
                'message'       => 'hello7'
            ),
            array(
                'id'            => '6',
                'user'          => 'Jun',
                'post_id'       => '1',
                'comment_id'    => '6',
                'reply_id'      => '1',
                'date'          => 'Jun 2016',
                'message'       => 'hello6'
            ),
            array(
                'id'            => '5',
                'user'          => 'May',
                'post_id'       => '1',
                'comment_id'    => '5',
                'reply_id'      => '3',
                'date'          => 'May 2016',
                'message'       => 'hello5'
            ),
            array(
                'id'            => '4',
                'user'          => 'Apr',
                'post_id'       => '1',
                'comment_id'    => '4',
                'reply_id'      => '2',
                'date'          => 'Apr 2016',
                'message'       => 'hello4'
            ),
            array(
                'id'            => '3',
                'user'          => 'Mar',
                'post_id'       => '1',
                'comment_id'    => '3',
                'reply_id'      => '',
                'date'          => 'Mar 2016',
                'message'       => 'hello3'
            ),
            array(
                'id'            => '2',
                'user'          => 'Feb',
                'post_id'       => '1',
                'comment_id'    => '2',
                'reply_id'      => '',
                'date'          => 'Feb 2016',
                'message'       => 'hello2'
            ),
            array(
                'id'            => '1',
                'user'          => 'Jan',
                'post_id'       => '1',
                'comment_id'    => '1',
                'reply_id'      => '',
                'date'          => 'Jan 2016',
                'message'       => 'hello'
            )
        );

Then the code to output it in the page:

foreach ($comment as $key => $value) {
    if(empty($value['reply_id'])) {
        $comments[$value['comment_id']] = array('user' => $value['user'], 'message' => $value['message']);
    } else {
        $replies[] = array('id' => $value['id'], 'reply_id' => $value['reply_id'], 'user' => $value['user'], 'message' => $value['message']);
    }
}

$reply_count = count($replies);
for ($i=0; $i < $reply_count; $i++) {
    $reply_id = $replies[$i]['reply_id'];
    if(isset($comments[$reply_id])) {
        $cc[$reply_id]['comment'] = array('user' => $comments[$reply_id]['user'], 'message' => $comments[$reply_id]['message']);
        $cc[$reply_id][$replies[$i]['id']] = array('user' => $replies[$i]['user'], 'message' => $replies[$i]['message']);
    }
}

foreach ($cc as $kkk => $vvv) {
    $reply = '';
    foreach ($vvv as $kk => $vv) {
        if(is_int($kk)) {
            $reply .= $vv['user'] . ' reply: ' . $vv['message'] . '<br>';
        } else {
            $comment = $vv['user'] . ' comment: ' . $vv['message'] . '<br>';
        }
    }
    echo $comment . $reply . '<br>';
}

The results are okay, but my question is, is this code okay to use?

It seems you wish to batch the posted messages together into threads. In other words, create a "parent - children" relationship. This can be achieved simply by using one loop to generate the new data structure and one smaller loop (I use array_map()) to display the text.

Code: (Demo)

foreach ($comments as $data) {
    if ($data['reply_id']!='') {
        $threads[$data['reply_id']][]="{$data['user']} reply: {$data['message']}";   // append element
    } else {
        if (!isset($threads[$data['id']])) {  // prevent Notice
            $threads[$data['id']] = [];
        }
        array_unshift($threads[$data['id']], "{$data['user']} comment: {$data['message']}");  // prepend element
    }
}

// display data (with no unwanted line breaks):
echo implode("<br><br>", array_map(function($v){return implode("<br>",$v);}, $threads));

Output (same as your posted code provides):

Jan comment: hello
Jul reply: hello7
Jun reply: hello6

Mar comment: hello3
May reply: hello5

Feb comment: hello2
Apr reply: hello4

Some additional notes:

  • Using ORDER BY date DESC will place newer replies above older replies, be sure that this makes sense in terms of discussion "flow".

  • There appears to be data redundancy with id and comment_id. If there is a difference between these two columns of data that doesn't present itself in your posted task, then they both can stay, otherwise you should omit one of these columns from your database.

  • A final consideration... when dealing with thread-based messages/posts/data, it simplifies tasks like these to assign a thread_id, then you can GROUP BY and/or ORDER BY the column in your SQL and spare much of the php handling.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.