skip to Main Content

I have a table like this :


No Name Product Type Num of Units
1 ADA B112 3 Pcs
2 ADA B253 1 Pcs
3 ADA K23 6 Pcs
4 DUZK l1 10 Pcs
5 DUZK l5 10 Pcs
6 Naro NX 1 Pcs

I have this SQL :

$query = "SELECT *, GROUP_CONCAT(`Code Unit`) AS Codeunit, COUNT(`Code Unit`) AS NoU FROM `stock goods` GROUP BY `Name Product`, `Type` ORDER BY `Name Product`, `Type`";

NoU : Num of Units

Actually I have Code Units in my table

Here is my code:

echo "<table id='customers'>";
echo "<tr>

<th id='No'>Nomor</th>
<th id='nameproduct'>Name Product</th>
<th id='type'>Type</th>
<th id='NoU'>Num of Units</th>
<th id='Click'>Botton Click</th>
</tr>";

$nomor_urut = 1;
  
$nomy = 1;
while ($row = mysqli_fetch_assoc($result)) {
    $nomy = 1; // Initialize $nomy here
    echo "<tr>";
    echo "<td>" . $nomor_urut."</td>";
    echo "<td>" . $row["Name Product"] . "</td>";
    echo "<td>" . $row["Type"] . "</td>";
    echo "<td>" . $row["NoU"] . "</td>";
    $Type2 =  explode(',', $row["Type"]);
    $Kodeunits = explode(',', $row["Kodeunit"]);
    $dara = $row["Type"] . " - " . $row["Jumlah"] . "<br>";

    echo "<td><button onclick='myFunction(" . json_encode($row["Nama Produk"]) . "," . json_encode($row["Type"]) . ",". json_encode($Kodeunits) . ")'>List Stock</button></td>";
    echo "</tr>";
    $nomor_urut++;
}

echo "</table>";

I hope to convert to the table like this:

No Name Product Type Num of Units
1 B112 3 Pcs
2 ADA B253 1 Pcs
3 K23 6 Pcs
4 l1 10 Pcs
5 DUZK l5 10 Pcs
6 Naro NX 1 Pcs

I hope every same name of Name Product would be pressed into 1 row so it looks neat

3

Answers


  1. First in your php code, determine consecutive identical Name Product by adding other identifiers, followed by the rowspan tag to assemble your table_td.

    Login or Signup to reply.
  2. You have to count the number of product with same name. As your Database query has a group by and order by clause so the products with same name will be grouped together. Then you have to add rowspan in the product name <td>

    <?php
    // MYSQL Query. Please change the column names as per your table name
    $query = "SELECT `name_product`, `type`, `num_of_units` AS NoU FROM `Products` GROUP BY `name_product`, `type` ORDER BY `name_product`, `type`";
    $result = mysqli_query($conn, $query);
    
    if ($result) {
        $result = mysqli_fetch_all($result, MYSQLI_ASSOC); // Make an associative array from the result
        $currentProduct = "";
        $rowCount = 0;
        echo "<table border='1'>"; //I have added border, if you have css class, you can remove border
        echo "<tr>
         <th>No</th>
            <th>Name Product</th>
            <th>Type</th>
            <th>Num of Units</th>
          </tr>";
        $serialNumber = 0;
        foreach ($result as $index => $row) {
            //in the loop check if the product has changed
            if ($row['name_product'] != $currentProduct) {
                if ($index > 0) {
                    echo "</tr>"; // Close the previous rowspan row
                }
    
                $currentProduct = $row['name_product']; //new current product
                //total count of a particular product. 
                //Reference : https://www.php.net/manual/en/function.array-count-values.php 
                $rowCount = array_count_values(array_column($result, 'name_product'))[$currentProduct]; 
                
                echo "<tr>";
                echo "<td>" . ++$serialNumber . "</td>"; 
                echo "<td rowspan='{$rowCount}'>" . $row['name_product'] . "</td>";
                echo "<td>" . $row['type'] . "</td>";
                echo "<td>" . $row['NoU'] . "</td>";
            } else {
                echo "<tr>";
                echo "<td>" . ++$serialNumber . "</td>";
                echo "<td>" . $row['type'] . "</td>";
                echo "<td>" . $row['NoU'] . "</td>";
            }
    
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "Error executing query: " . mysqli_error($conn);
    }
    ?>
    

    Result:
    enter image description here

    Login or Signup to reply.
  3. The main problem with your code is that you are very quick to echo the results as fast as you get them, so by the time you get the second record having the same product name as the previous one, you’ve already written the first one. Instead, you need to grasp the concept of separation of concerns, a principle, which, in your case was violated by assuming that you want/need to echo out all the results as soon as you get them. But it’s actually not the case, because you will need to first parse the records in order to know what the rowspan will end up to be.

    Instead of echoing everything, separate the echoing into a function, which I will call echoTemplate here and do something like this:

        $prefix = "<div id='test'>Stock Barang</div>";
    
        // output data of each row
        $prefix .= "<div>";
        $prefix .= "<table id='customers'>";
        $prefix .= "<tr>";
        
        $prefix .= "<th id='KodeUnit' rowspan='2'>Nomor</th>";
        $prefix .= "<th id='namaproduct' colspan='2'>Nama Product</th>";
        $prefix .= "<th id='type' rowspan='2'>Type</th>";
        $prefix .= "<th id='Jumlah' rowspan='2'>Jumlah Stock</th>";
        $prefix .= "<th id='Button' rowspan='2'>Botton Click</th>";
    
            $nomor_urut = 1;
          
                $nomy = 1;
        $records = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $nomy = 1; // Initialize $nomy here
            $Type2 =  explode(',', $row["Type"]);
            $Kodeunits = explode(',', $row["Kodeunit"]);
            for ($index = 0; $index < count($Type2]; $index++) {
                $record = [
                    [
                        'value' => $nomor_urut
                    ],
                    [
                        'colspan' => 2,
                        'value' => $row["Nama Produk"]
                    ],
                    [
                        'value' => $Type2[$index]
                    ],
                    [
                        'value' => $Kodeunits[$index]
                    ]
                ];
                if ($index === 0) {
                    $records []= [];
                }
                $records[count($records) - 1][]=$record;
            }
        
            //echo "<td><button onclick='myFunction(" . json_encode($row["Nama Produk"]) . "," . json_encode($row["Type"]) . ",". json_encode($Kodeunits) . ")'>List Stock</button></td>";
            $nomor_urut++;
        }
    
        $suffix = "</table>";
        $suffix .= "</div>";
        echoTemplate($prefix, $records, $suffix);
    

    And then implement echoTemplate so that it actually echoes out the stuff you generated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search