Only the last array item is displayed

Asked

Viewed 63 times

1

How to play a php code, like a comment system.

Using this code I was able to make this possible but, in the same way the code returns only the last query item:

<?php

error_reporting(0);

header("Content-Type: text/html");
header("charset: UTF-8");

$comment_id = $_GET["postID"];

mysql_connect("localhost", "root", "usbw");
mysql_select_db("run");

$cmd = "SELECT * FROM `comments` WHERE `POST_ID` LIKE '$comment_id' LIMIT 0,10";
$cmd_q = mysql_query($cmd);

$xmlpath = "";

while($row = mysql_fetch_array($cmd_q)) {
    $xmlpath = "<div id='comment.usr'>" . $row["POST_NAME"] . "</div> disse <div id='comment.content'>" . $row["POST_COMMENT"] . "</div>";
}

?>

<div id="comments">
    <?php echo $xmlpath; ?>
</div>

NOTE: If I use $xmlpath += ... it returns a zero ( 0 )

  • 1

    <div id="comments">&#xA; <?php echo $xmlpath; ?>&#xA;</div> that stretch must be inside the while.

  • OK I’ll try! .

  • @rray worked! Thank you very much!

  • @rray comment as reply so I mark as accepted!

  • 1

    Depending on the CSS, it could work only using $xmlpath .= ""; inside the while, as it would concatenate all comments.

1 answer

3


Place the code snippet <div id="comments"><?php echo $xmlpath; ?></div> while inside so will be printed all comments and not only the last.

while($row = mysql_fetch_array($cmd_q)) {
    $xmlpath = "<div id='comment.usr'>" . $row["POST_NAME"] . "</div> disse <div id='comment.content'>" . $row["POST_COMMENT"] . "</div>";
    echo '<div id="comments">'. $xmlpath .'</div>';
}

The most equal operator(+=) only works for numbers, to concatenate strings use the point(.) or (.=).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.