Reputation: 21
Im trying to create a div from rows in a mysql database, but when I loop through the database table and apply the returned rows to the div, I only get back the last row. Here is the code :
<?php
$servername = "localhost";
$username = "divs";
$password = "password";
$dbname = "setdivs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from getinfo";
$result = $conn->query($sql);
echo " <div id=\"vertical-with-linking\" class=\"fn-container fn-vertical\" style=\"color: rgb(51, 51, 51); margin: 0px auto; overflow: hidden; position: relative; width: 580px; height: 280px\">";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["divtitle"]. " " . $row["datalink"] . "<br>";// works fine
//echo "<div title = " . $row['divtitle'] . " data-link: " . $row['datalink'] . "><img src=" . $row['imagesrc'] . " />";//returns last row only
}
} else {
echo "0 results";
}
$conn->close();
?>
Any advice would be greatly appreciated.
Upvotes: 0
Views: 61
Reputation: 4772
I think the problem might be that your Div's are not closed.
try this.
<?php
$servername = "localhost";
$username = "divs";
$password = "password";
$dbname = "setdivs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from getinfo";
$result = $conn->query($sql);
echo " <div id=\"vertical-with-linking\" class=\"fn-container fn-vertical\" style=\"color: rgb(51, 51, 51); margin: 0px auto; overflow: hidden; position: relative; width: 580px; height: 280px\">";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["divtitle"]. " " . $row["datalink"] . "<br>";// works fine
echo "<div title = " . $row['divtitle'] . " data-link= " . $row['datalink'] . "><img src=" . $row['imagesrc'] . " /></div>";//returns last row only
}
echo "</div>";
} else {
echo "0 results";
}
$conn->close();
?>
Upvotes: 0
Reputation: 94662
This is a safer way. You had no quotes around the attribute values and if any, like $row['divtitle']
have spaces in them then they must be quoted with either double quotes or single quotes.
Also it should be data-link=
and not data-link:
And you need to close the div after the img tag is completed
echo "<div title='{$row['divtitle']}' data-link='{$row['datalink']}'><img src='{$row['imagesrc']}'/></div>";
Upvotes: 1
Reputation: 436
You should close the div
echo "<div title = " . $row['divtitle'] . " data-link: " . $row['datalink'] . "><img src=" . $row['imagesrc'] . " />";//returns last row only
to
echo "<div title = " . $row['divtitle'] . " data-link: " . $row['datalink'] . "><img src=" . $row['imagesrc'] . " /></div>";//returns last row only
Upvotes: 0