0

我试图让 while 循环显示从表中选择的所有记录,并迭代封装在 <div> 中的结果。我的查询正在工作,而 while 正在显示一些东西……但只有最新的记录。我不知道为什么会这样,而且我对 PHP 很陌生,所以任何帮助都会很棒。

代码:

<?php
//Connect to database and select table
$conn = mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("substest", $conn) or die(mysql_error());
//Issue query
$get_features = "SELECT ID, Title, DateReceived, Synopsis FROM features ORDER BY ID";
$r_get_features = mysql_query($get_features) or die(mysql_error);

if(mysql_num_rows($r_get_features) < 1)  {
    $display_block = "<div class=\"inner\"><h2>Nothing to display</h2></div>";
}
else {
    while($feat_array = mysql_fetch_array($r_get_features)) {
    $feat_id = $feat_array["ID"];
    $feat_title = stripslashes($feat_array["Title"]);
    $feat_dater = stripslashes($feat_array["DateReceived"]);
    $feat_synopsis = stripslashes($feat_array["Synopsis"]);

    $display_block = "<div class=\"inner\">";
    $display_block .= "<h2>".$feat_title."</h2>";
    $display_block .= "<label for \"id\">ID:</label>";
    $display_block .= "<span class=\"formresult\" id=\"id\">".$feat_id."</span><br />";
    $display_block .= "<label for=\"title\">Title:</label>";
    $display_block .= "<span class=\"formresult\" id=\"title\">".$feat_title."</span><br />";
    $display_block .= "<label for=\"datereceived\">Date Received:</label>";
    $display_block .= "<span class=\"formresult\" id=\"datereceived\">".$feat_dater."</span><br />";
    $display_block .= "<label for=\"synopsis\">Synopsis:</label>";
    $display_block .= "<span class=\"formresult\" id=\"synopsis\">".$feat_synopsis."</span><br />";
    $display_block .= "</div>";
}
}
?>
<html>
<head>
<title>Galway Film Fleadh - View Submitted Feature Films</title>
<link href="css/submissions.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
    <div class="inner">
        <h1>Galway Film Fleadh - View Submitted Films</h1>
    </div>
    <?php
        print $display_block;
    ?>
</div>        
</body>
</html>
4

5 回答 5

4

您正在$display_block为每个循环迭代覆盖:

$display_block = "<div class=\"inner\">";

在循环之前定义它,然后将上面的行更改为:

$display_block .= "<div class=\"inner\">";
于 2013-03-28T16:04:43.793 回答
3

替换这一行

$display_block = "<div class=\"inner\">";

经过

$display_block .= "<div class=\"inner\">";
于 2013-03-28T16:04:57.873 回答
2

自从

 $display_block = "<div class=\"inner\">"; 

在循环内,所以在while循环中的每次迭代后,div都会被实例化......

您需要保留旧值,因此必须将其连接起来...将其替换为

 $display_block .= "<div class=\"inner\">";
于 2013-03-28T16:10:35.203 回答
1
$display_block = "<div class=\"inner\">";

位于 while 循环内,因此每次迭代时都会获得一个新值并且不保存前一个值。

于 2013-03-28T16:05:24.027 回答
0

尝试将您的 while 更改为:

while($feat_array = mysql_fetch_array($r_get_features, MYSQLI_ASSOC)) {
...
于 2013-03-28T16:04:23.957 回答