单个数据库表中具有3个不同行的Array Explode值如何在表中打印?

时间:2019-02-13 09:11:45

标签: mysql mysqli explode mysqlimport

你好,我有一个爆炸功能问题。

在我的数据库中,我有一个没有行的表,是否有一些行数据以逗号分隔保存,而有些行却以逗号分隔保存。

我的问题是爆炸值,一些空值也在查看如何停止该值。

源代码如下

<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>

在这里我将分享数据库和正视图的屏幕截图

database look like this

front end look lie this

删除空格并继续值

2 个答案:

答案 0 :(得分:1)

您的问题出在您的Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index循环中,for是类别数,但是您从0循环到$lenght,而不是从0循环到$lenght在空白行中。将您的$lenght-1语句更改为:

for

答案 1 :(得分:1)

<?php for($i=0; $i< $lenght; $i++) i will change thar <= replace to < then output will came 

{?>

做点事情

}?>