简体   繁体   中英

PHP foreach loop not return all result

I using foreach loop to select all the post, and it successful return all the result. After that, I create a function of comment to attach to all the post, but when I select all the result using foreach loop and it only return me latest result.

The problem is I create function to select all result of the comment_table with using for each loop.And the result only return 1 result but in database gt two result, it should return 2 result. After that, I call the function and echo it in ANOTHER foreach loop block.

PHP&HTML code (comment section):

  function getComments($conn,$postId){
    $commentSql = "    SELECT comments.id, comments.user_id, comments.user_desc, comments.timestamp, user.firstname, user.lastname, user.profile_photo 
                       FROM comments 
                       INNER JOIN user 
                       ON user.id = comments.user_id
                       WHERE post_id = ? ";

    if($commentStmt = $conn->prepare($commentSql)){
        $commentStmt->bind_param("i", $postId);
        $commentStmt->execute();

        $commentStmt->bind_result($commentId, $userId, $userComment, $comment_timestamp, $user_firstname, $user_lastname, $user_profile_photo);
        $commentIdArray = array();
        while ($commentStmt->fetch()) {
            $object1 = new stdClass();
            $object1->commentId = $commentId;
            $object1->userId = $userId;
            $object1->userComment = $userComment;
            $object1->comment_timestamp = $comment_timestamp;
            $object1->user_firstname = $user_firstname;
            $object1->user_lastname = $user_lastname;
            $object1->user_profile_photo = $user_profile_photo;

            $commentIdArray[] = $object1;



        }
        $commentStmt->close();

        $count = 0;
        foreach ($commentIdArray as $value1){
            $test = $value1;
            $comments = '



                    <div class="media-left">
                        <a href="#">
                             <img src="' . $value1->user_profile_photo . '" class="img-rounded custom-birthday-image"  /></img>
                        </a>
                    </div>

                    <div class="media-body">
                        <h4 class="media-heading"> ' . $value1->user_firstname . ' ' . $value1->user_lastname . '</h4>
                        <p>' . $value1->userComment . '</p>

                        <div class="comment-tools">
                            <ul id="c_option">
                                <li id="c_like" value="' . $value1->commentId . '"><a href="#" onclick="return false;">Like</a></li>
                                <li id="comment_reply'.$count.'" value="' . $value1->commentId . '" onclick=" getidforeachbutton(this.id);"><a href="" onclick="return false;"><span class="unselect">Reply</span></li>
                                <li id="" value="' . $value1->commentId. '"><a href="#"><img src="/img/people_liked.png"></a></li>
                                <li>
                                    <small class="timestamp">' . $value1->comment_timestamp . '</small>
                                </li>
                            </ul>
                        </div>
                    </div>



            ';
            ++$count;
        }
        print_r($test);

        return $comments;
    }else{
        return "error preparing sql";
    }
}

Comment Table:

在此处输入图片说明

Result:

在此处输入图片说明

Change:

foreach ($commentIdArray as $value1){
    $test = $value1;
    $comments = ...

To:

foreach ($commentIdArray as $value1){
    $test = $value1;
    $comments .= ...

您的$comments应该是一个用于返回多个值的数组,否则,它仅返回最后一个值。因此,您的comment变量将类似于函数内部:

$comments[] =

It should be:

 $comments.= '
                <div class="media-left">
                    <a href="#">
                         <img src="' . $value1->user_profile_photo . '" class="img-rounded custom-birthday-image"  /></img>
                    </a>
                </div>

                <div class="media-body">
                    <h4 class="media-heading"> ' . $value1->user_firstname . ' ' . $value1->user_lastname . '</h4>
                    <p>' . $value1->userComment . '</p>

                    <div class="comment-tools">
                        <ul id="c_option">
                            <li id="c_like" value="' . $value1->commentId . '"><a href="#" onclick="return false;">Like</a></li>
                            <li id="comment_reply'.$count.'" value="' . $value1->commentId . '" onclick=" getidforeachbutton(this.id);"><a href="" onclick="return false;"><span class="unselect">Reply</span></li>
                            <li id="" value="' . $value1->commentId. '"><a href="#"><img src="/img/people_liked.png"></a></li>
                            <li>
                                <small class="timestamp">' . $value1->comment_timestamp . '</small>
                            </li>
                        </ul>
                    </div>
                </div>
        ';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM