Home > Back-end >  PHP loop with sperate table
PHP loop with sperate table

Time:10-11

How do i sperate the data with different table ,i need show all my data with loop but 'Bundle Name' of column same will with one table .

PHP code:

<table>
<?php
include('connection.php');

$result1 = mysqli_query($conn, "SELECT * FROM bundel ");
while ($row = mysqli_fetch_assoc($result1)) {
    echo $row['BundelName'];
    $bundel = $row['BundelName'];

    echo '<tr>';
    echo '<td>';
    echo $row['SKU'];
    echo '</td>';
    echo '<td>';
    echo $row['Description'] ;
    echo '<td>';
    echo '</tr>';
    
    if ($row['BundelName'] != $bundel){
        echo"</table><br><br><table>";
    } 

}
?>
</table>

MySQL My result

my expected result

CodePudding user response:

If you want to parse the results using a single loop, the following approach is an option. The important part here is that you need an ORDER BY clause in your statement and appropriate changes in the script, to make your loop work.

A simplified example, based on the code in the question:

$result1 = mysqli_query($conn, "SELECT * FROM bundel ORDER BY BundelName");
$bundel = "";
while ($row = mysqli_fetch_assoc($result1)) {
    if ($bundel != $row["BundelName"]) {
        echo "<table>";
        echo '<tr><td>' . $row['BundelName'] . '</td></tr>';
    };

    echo '<tr>';
    echo '<td>' . $row['Description'] . '</td>';
    echo '<td>' . $row['SalesPrice'] . '</td>';
    echo '</tr>';

    if ($bundel != $row["BundelName"]) {
        echo "</table>";
        $bundel = $row["BundelName"];
    };
}

Of course, you can group and store the result set in an array using BundelName as key, and print the expected output using another loop:

$stmt = mysqli_query($conn, "SELECT * FROM bundel");
$result = array();
while ($row = mysqli_fetch_assoc($stmt)) {
    $result[$row["BundleName"]][] = array(
        "Description" => $row["Description"],
        "SalePrice" => $row["SalePrice"]
    );  
}

foreach ($result as $bundel => $products) {
    echo "<table>"; 
    echo '<tr><td>' . $bundel . '</td></tr>';
    foreach ($products as $product) {
        echo '<tr>';
        echo '<td>' . $product["Description"] . '</td>';
        echo '<td>' . $product["SalePrice"] . '</td>';
        echo '</tr>';
    }
    echo"</table>";     
}   

CodePudding user response:

I also recommend the way @Robert suggested.

But here is a quick and a dirty way to do this.

$result1 = mysqli_query($conn, "SELECT DISTINCT BundleName FROM bundel ");
while ($row = mysqli_fetch_assoc($result1)) {
      echo $row['BundelName'];
      $bundel=$row['BundelName'];
      echo"<table>";
      $query2 = "SELECT * FROM bundel WHERE BundelName='$bundel'";
      $result2 = mysqli_query($conn, $query2);
      while ($row2 = mysqli_fetch_assoc($result1)) {
             echo '<tr>';
             echo '<td>';
             echo $row2['SKU'];
             echo '</td>';
             echo '<td>';
             echo $row2['Description'] ;
             echo '<td>';
             echo '</tr>';
        }
    echo"</table>";
                           
 }

CodePudding user response:

You can use 'GROUP BY BundelName' in your sql query or you can first query 'SELECT DISTINCT(BundelName) FROM bundel ' and store in an array and use all distinct values of this array in creating separate tables. In group by case you can do a trick by creating a new table whenever you detect a new group in the while loop. Hint- in_array() in php.

CodePudding user response:

I prefer array_map or foreach but use "while" like on your example, try that:

    $results = [];

    while ($row = mysqli_fetch_assoc($result1)) {
      
      $results[$row['BundelName']] = $row['description'];

    }

And create table in html with data send by PHP.

  • Related