Beginnger-ish PHP/MYSQL Coder here, I'm creating a category browse page, and I have to display 50 images from a mysql database called "inventory". I generated the links through the while loop, but no clue on how I'm supposed to generate the images in succession without 50 lines of <img src
. The way I would like to generate it is:
Image
Link (on new line),
image2 (on new line),
link2 (on new line),
and etc...
My images are numbered from 1.jpg to 50.jpg.
<?php
include 'connection.php';
$sql = "SELECT * FROM products";
$result = mysqli_query($mysql, $sql) or die("Bad Query: $sql");
if(mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_array($result)) {
echo "<a href = 'details.php?ID={$row['Item_Number']}'>{$row['Item_Name']}</a><br>\n";
}
}
?>
Unless I missed the point there then you can simply add the img
tag to the generated HTML in the loop - selecting the appropriate path and the data from the db.
<?php
include 'connection.php';
$sql = 'select `jpg`, `item_name`, `item_number` from `products`';
$result = mysqli_query( $mysql, $sql ) or die("bad query: $sql");
if( mysqli_num_rows( $result ) > 0 ) {
while( $row = mysqli_fetch_array( $result ) ) {
# Ensure that the path below is correct
echo "
<img src='/path/to/images/{$row['jpg']}' alt='{$row['item_name']}' />
<br />
<a href='details.php?id={$row['item_number']}'>{$row['item_name']}</a>
<br />
<br />";
}
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.