0

I am attempting to have a while loop display all records selected from table, and iterate the results encapsulated in a < div >. My query is working, and the while is displaying something...but only the latest record. I can't figure out why this is, and I'm very new to PHP so any assistance would be great.

Code:

<?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>
0

5 Answers 5

4

You're overwriting $display_block for each loop iteration:

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

Define it before the loop, then change the line above to this:

$display_block .= "<div class=\"inner\">";
Sign up to request clarification or add additional context in comments.

Comments

3

replace this line

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

by

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

Comments

2

since

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

is inside the loop so after every iteration in while loop the div gets instanstiated ....

you need to keep the old values so you have to concatenate it ... replace it with

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

Comments

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

Is inside the while loop, so each time it iterates it gets a new value and does not hold the previous.

Comments

0

Try changing your while to:

while($feat_array = mysql_fetch_array($r_get_features, MYSQLI_ASSOC)) {
...

Comments

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.