0

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

1
  • I would store db response in array variable. Do grouping in php and then draw table. Commented Oct 11, 2021 at 6:42

4 Answers 4

3

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>";     
}   
Sign up to request clarification or add additional context in comments.

3 Comments

That one is a good solution. I use this way in my projects sometimes.
im intrest the group method ,how about i also need output my sell price and cost price ?sorry i not expert on php .@Zhorov
@nzxy12 The answer is updated. If you need more columns simply use a nested array.
1

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>";
                           
 }

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.