4

So, I have a 'parent' column in the comments table and I call the comments like this...

SELECT 
    id, 
    parent_id, 
    member, 
    name, 
    email, 
    ip, 
    comment, 
    img, 
    date 
FROM cs_comments 
WHERE (row_id='$cmtid' AND page_type='$cmtt') 
ORDER BY date

I call the function like so... `

$comment_data = array();

while ($comments_array = $comments->fetch_object()) {
        $comment_data[] = $comments_array;
}

echoComments($comment_data, $memberlevel);

I have a function setup to print the comments but, how to print the replys? The function is inside it's self.

I can add more info if my question is not clear.

2 Answers 2

2

At First you should consider that using user input data directly in SQL query without sanitizing it, is very dangerous and is not recommended!


2) it is better to set default value for parent_id to 0, I'm not sure null will get same result


3) You may escape date and name column in your mysql query it can conflict with mysql native vars & functions

<?php
//Code in view
$comments = getComments($cmtid,$page_type);
echoComments($comment);



//This method will take all Comments with their replies at once;
function getComments($cmtid,$page_type,$parent=0)
{
     GLOBAL $mysqli;
     $comments = $mysqli->query("SELECT `id`,parent_id,member,`name`,email,ip,`comment`,img,`date` 
                                FROM cs_comments 
                                WHERE (row_id='{$cmtid}' AND page_type='{$cmtt}' AND parent_id = {$parent}) 
                                ORDER BY date(format)");
     $comment_data = array();
     while($comments_array = $comments->fetch_object())
     {
        $comments_array->replies = $comments_array->parent_id ? getComments($cmtid,$page_type,$comments_array->parent_id) : array();
        $comment_data[] = $comments_array;
     }
     return $comment_data;
}


//This method will print comment with replies
function echoComments($comment_data)
{
    foreach ($comment_data as $comment):
        echo    '<div class="comment">
                    '.$comment->comment.'
                    <hr />
                    '.echoComments($comment->replies).'
                </div>';
    endforeach;
}

?>

You can use this style sheet to give some indent for nested replies:

<style>
.comment{
    background:#fff;
}
.comment .comment{
    margin-left:5px;
    background:#eee;
}
<style>
Sign up to request clarification or add additional context in comments.

Comments

1

Since your question is quite abstract, this is the solution I'd start with:

// call it somewhere else inside some other function
$this->recursiveCaller();

// now the function itself
function recursiveCaller($parent = 0, $indent = 0) {
    // $Database->select is just pseudo for however you do the query!!!
    $Database->select('SELECT id, parent_id, member, name, email, ip, comment, img, date FROM cs_comments WHERE (row_id='.$cmtid.' AND page_type='.$cmtt.' AND parent_id = '.$parent.') ORDER BY date');
    $menuItems = $this->Database->fetchAll();

    while($comments_array = $comments->fetch_object()) {
    ?>
        <div style="margin-left: <?= $indent ?>px;">
        <!-- INSERT CONTENT HERE --> 
        </div>
        <?php
        // $this assumes you are doing this inside a class
        // it also assumes $comment_array holds an object retrieved by the query, where $comments_array->parent_id points to the relevant parent_id column
        $this->recursiveCaller($comments_array->parent_id, $indent+50);
    }
}

It will start with printing the "base" layer with no indentation and will create a "ladder" adding 50px to the left margin every time you go down the recursion.

3 Comments

Thank you very much for the answer. More to the point. What logic calls the 'resursiveCaller' function inside it's self? Are you saying there should be two functions? One for top level comments and one for replys?
@DaedBaet no, it's one function that calls itself. It all depends on your code structure, it's not an easy question to answer while you don't exactly know what you want and expect.
I needed to move the query inside the function and add the parent parameter to the function as you have in your example. Got it all squared away now. Thank you kindly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.