简体   繁体   中英

How to print multiple record from same table in php

I am new to php and developing website. I have one product page where many products are stored in database. When i print them all products come in single column, i did float:left but there is no change. But when i do the same thing entering manual products in html it is fine. Below is the code what i am using in php. Please help.

<div class="products-wrap">
<?php
$result = mysqli_query($con,"SELECT ProductName,Description,ProductImage FROM products");
while($row = mysqli_fetch_array($result))
  {
  echo  "<div class='product-img'><img src='upload/".$row['ProductImage']."'/></div>";
  echo '<br/>';
  echo "<p>".$row['Description']."</p>"; 
  echo '<div class="product-details"><a href="#">Product Details</a></div>';
  }
mysqli_close($con);
?>
</div>

produst-wrap is the main div which have to print several times. there are 4 products in 1 row but when i use this coding all products show in single column.

Thanks

What you have in your example "makes all elements under each other" (as you say, and you posted no CSS for us to know otherwise). What I think you might be missing (so please try that) is to have the <div class="products-wrap"> inside the while loop.

Like this:

<?php
$result = mysqli_query($con,"SELECT ProductName,Description,ProductImage FROM products");
while($row = mysqli_fetch_array($result))
{
echo "<div class='products-wrap'>";
echo "<div class='product-img'><img src='upload/".$row['ProductImage']."'/></div>";
echo '<br/>';
echo "<p>".$row['Description']."</p>"; 
echo '<div class="product-details"><a href="#">Product Details</a></div>';
echo '</div>';
}
mysqli_close($con);
?>

Let me know if this helped or was not what you are looking for.

Maybe try iterate through a foreach loop

<?php
$result = mysqli_query($con,"SELECT ProductName,Description,ProductImage FROM products");
$rows = mysqli_fetch_array($result, MYSQLI_ASSOC);

foreach($rows as $row)
{
   echo "<div class='products-wrap'>";
   echo "<div class='product-img'><img src='upload/".$row['ProductImage']."'/></div>";
   echo '<br/>';
   echo "<p>".$row['Description']."</p>"; 
   echo '<div class="product-details"><a href="#">Product Details</a></div>';
   echo '</div>';
}
mysqli_close($con);
?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM