0

我正在尝试循环此代码,以便当 php 代码上的 while 语句启动时,数据库输出将有一个单独<li>的输出

    <li class="bar<?php echo $id; ?>">
    <div align="left" class="post_box">
    <span style="padding:10px"><?php echo $msg; ?> </span>
    <span class="delete_button"><a href="#" id="<?php echo $id; ?>" class="delete_update">X</a></span>
    <span class='feed_link'><a href="#" class="comment" id="<?php echo $id; ?>">comment</a></span>
    </div>
    <div id='expand_box'>
    <div id='expand_url'></div>
    </div>
    <div id="fullbox" class="fullbox<?php echo $id; ?>">
    <div id="commentload<?php echo $id; ?>" >
    </li>

这是我的 php 代码

    </div>
    <div class="comment_box" id="c<?php echo $id; ?>">
    <form method="post" action="" name="<?php echo $id; ?>">
    <textarea class="text_area" name="comment_value" id="textarea<?php echo $id; ?>">
    </textarea><br />
    <input type="submit" value=" Comment " class="comment_submit" id="<?php echo $id; ?>"/>
    </form>
    </div>
    </div>

<?php
if($_POST['submit'] == "submit"){
   $username="xxx_admin";
   $password="xxx";
   $database="xxx_database";

//connect to mysql server
                $mysqli = new mysqli("localhost", $username, $password, $database);

                                        //check if any connection error was encountered
                                        if(mysqli_connect_errno()) {
                                            printf("Connect failed: %s\n", mysqli_connect_error());
                                            exit;
                                        }   
   $client_id = $_POST['client_id'];
   echo $client_id;
  $query="select * from messages where client_id='$client_id'";
  $result = $mysqli->query( $query );
  $row = $result->fetch_assoc();
                                        while ($row = $result->fetch_object())
                                        {
                                                // set up a row for each record
                                                echo "<li>";
                                                $mes = $row->message;
                                                $mes = nl2br($mes);

                                                $msg ="{$mes} <br> . {$row->date_post;}";

?>
<li class="bar<?php echo $id; ?>">
<div align="left" class="post_box">
<span style="padding:10px"><?php echo $msg; ?> </span>
<span class="delete_button"><a href="#" id="<?php echo $id; ?>" class="delete_update">X</a></span>
<span class='feed_link'><a href="#" class="comment" id="<?php echo $id; ?>">comment</a></span>
</div>
<div id='expand_box'>
<div id='expand_url'></div>
</div>
<div id="fullbox" class="fullbox<?php echo $id; ?>">
<div id="commentload<?php echo $id; ?>" >

</div>
<div class="comment_box" id="c<?php echo $id; ?>">
<form method="post" action="" name="<?php echo $id; ?>">
<textarea class="text_area" name="comment_value" id="textarea<?php echo $id; ?>">
</textarea><br />
<input type="submit" value=" Comment " class="comment_submit" id="<?php echo $id; ?>"/>
</form>
</div>
</div>
</li>
<?php                                   }

     $mysqli->close();
}
?>

我正在尝试循环 html 代码,因为它将被放入<ol></ol>

数据库中的所有行都应该有一个单独的<li></li>

4

1 回答 1

1

官方 PHP 站点中存在相同的循环示例。

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br />\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}
?> 

你应该,而不是首先渲染 html,将消息加载到一个数组中,然后,迭代这个数组进行 html 渲染

祝你好运!

于 2012-11-01T11:25:15.733 回答