SANTOSH VERMA SANTOSH VERMA - 4 months ago 11
SQL Question

Fetch data into tabular format

I am trying to fetch data from database in tabular format. I have taken count variable as a number of rows and 5 columns. When I execute this query all results are fetched in multiple row and single columns.
Here is my code :

<?php
include("connect.php");
$all_product = mysqli_query($connect, "SELECT * FROM product WHERE user_id='$fetch_user_id'");
echo '<table style="background-color:white; margin-left:50px; margin-right:50px;">';
$count = mysqli_num_rows($all_product);
$count = ($count / 5) + 1;

for ($i = 0; $i < $count; $i++) {
echo "<tr[$i]>";

for ($m = 0; $m < 5; $m++) {
echo "<td[$m]>";

while ($all = mysqli_fetch_array($all_product)) {
echo '<img src="' . $all['image'] . '" width="150px" height="150px" />
<div class="search_details">
<figcaption>' . $all['name'] . '</figcaption>
<figcaption>by ' . $all['brand'] . '</figcaption><figcaption>&#8360;' . $all['price'] . '</figcaption>
<figcaption>Save: &#8360;' . $local_price . ', (' . $all['discount'] . '%)</figcaption><figcaption>In stock:' . $all['stock'] . '</figcaption></div>';
}

echo "</td[$m]>";
}

echo "</tr[$i]>";
}

echo "</table>";
?>


Any help ? Where am I going wrong ?
Thank you in advance

Answer

I hope this helps:

 <?php
    $all_product = mysqli_query($connect, "SELECT * FROM product WHERE user_id='$fetch_user_id'");
    $count = mysqli_num_rows($all_product);
    $i= 0;
    $m=5;
    echo '<table width="100%" margin-left="30px" style="background-color:white;">';
    while ($all = mysqli_fetch_array($all_product)) {
    if($i%5==0) {
    echo "<tr>";
                }
    echo '<td style="width:20%">';
    echo '<img src="'.$all['image'].'" width="150px" height="150px" />
    <div class="search_details">
    <figcaption>'.$all['name'].'</figcaption>
    <figcaption>by '.$all['brand'].'</figcaption><figcaption>&#8360;'.$all['price'].'</figcaption>
    <figcaption>Save: &#8360;'.$local_price.', ('.$all['discount'].'%)</figcaption><figcaption>In stock: '.$all['stock'] .'</figcaption></div>';
    echo "</td>";
    if($i%$m==$m - 1){
    echo "</tr>";
                 }
    $i++;
                 }
    echo "</table>";
    ?>