Reputation: 63
Before anything else this is my PHP Code
<?php
$conn = mysqli_connect("localhost", "root", "", "mydb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$promo1 = "";
$promo2 = "";
$promo3 = "";
$sql = "SELECT prmo_itemDescription FROM promos";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$promo1 = $row[0];
$promo2 = $row[1];
$promo3 = $row[2];
mysqli_free_result($result);
mysqli_close($conn);
?>
and this is where I want the description to be echoed
<div class="col-md-3">
<p><?php echo $promo1?></p>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-1">Buy Now!</button>
</div>
<div class="col-md-3">
<p><?php echo $promo2?></p>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-2">Buy Now!</button>
</div>
<div class="col-md-3">
<p><?php echo $promo3?></p>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-3">Buy Now!</button>
</div>
and this is my database in phpmyadmin My problem is I managed to display the $promo1 but the other two(promo2 and promo3) doesnt seem to display, it looks like it cannot find the other two rows. There's an error like unidentified offset. am I missing something?
Upvotes: 0
Views: 45
Reputation: 1876
It looks like you are trying to iterate over your results. You could do it with a loop, something like this
<?php
//Create Connection
$conn = new mysqli("localhost", "root", "", "mydb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT prmo_itemDescription as pname FROM promos";
if ($promos = $conn->query($sql)) {
$index = 1;
while ($promo = $promos->fetch_assoc()) {
?>
<div class="col-md-3">
<p><?php echo $promo['pname']; ?></p>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#details-<?php echo $index; ?>">Buy Now!</button>
</div>
<?php
$index += 1;
} //END WHILE
$promos->free();
} //END IF
$conn->close();
?>
Instead of index you could use promo id and use it on your html.
Upvotes: 1