phpmysqlmysqliexplodemysqlimport

Array Explode values in single database table with 3 different rows print in a table how?


In my database, I have one table with a number of rows are there some rows of data saved as comma-separated and some rows are saved without comma separated.

My problem is explode the values some empty values are also view how to stop that values.

Source code looks like this

<table id="datatable" class="table table-bordered">
                 <thead>
                    <tr>
                       <th>Sl.No</th>
                       <th>Item Name </th>
                       <th>Category</th>
                       <th>Brand</th>
                       <th>Qty</th>
                       <th>Unit Price</th>
                       <th>Amount</th>
                       <th>Pic</th>
                       <th>Others</th>
                    </tr>
                 </thead>
                 <tbody>
                    <?php
                    require_once("../dbconfig.php");
                    extract($_REQUEST);
                    
                    $sql="SELECT * FROM `purchase_data` where project_id='$proid'";
                    $result = $conn->query($sql);
                    $count=$result->num_rows;
                    if ($count > 0) {
                    $x=1;
                    while ($pur_row = $result->fetch_assoc()) {
                           $proid =$pur_row['pur_id']; 

                            $category = $pur_row['pur_category'];
                            $categories = explode(',',$category);
                            $lenght = count($categories);

                            print_r($categories);

                            $product = $pur_row['pur_itemname'];
                            $products = explode(',',$product);

                            $brand = $pur_row['pur_brand'];
                            $brands = explode(',',$brand);

                            $unitprice = $pur_row['pur_rate'];
                            $unitprices = explode(',',$unitprice);

                            $qty = $pur_row['pur_qty'];
                            $quantity = explode(',',$qty);

                            $remarks = $pur_row['pur_others'];
                            $remark = explode(',',$remarks);

                            $est_amou = $pur_row['pur_amount'];
                            $est_amount = explode(',',$est_amou);

                            $edr_others = $pur_row['pur_others'];
                            $edr_other = explode(',',$edr_others);
                             ?>
                             <?php for($i=0; $i<=$lenght; $i++)
                                {  ?>
                    <tr>
                       <td><?=$x;?></td>
                       <td><?=$products[$i];?></td>
                       <td><?=$categories[$i];?></td>
                       <td><?=$brands[$i];?></td>
                       <td><?=$quantity[$i];?></td>
                       <td><?=$unitprices[$i];?></td>
                       <td><?=$est_amount[$i];?></td>
                       <td></td>
                       <td><?=$edr_other[$i];?></td>
                    </tr>
                   <?php $x++;  }  } }  ?>
                 </tbody>
             </table>

Here I will share my screenshots of the database and front view

database look like this

front end look lie this

remove white spaces and continue values


Solution

  • Your problem is in your for loop, $lenght is the number of categories, but you are looping from 0 to $lenght instead of 0 to $lenght-1 which is resulting in the blank rows. Change your for statement to:

    <?php for($i=0; $i < $lenght; $i++)