我有一个名为purchase_details
的表,其中的某些值具有相同的invoice_no
。现在,我需要每个具有相同invoice_no
的数据,并将它们输出到一个与invoice_no
长度相同的输入字段中。
由于我是PHP新手,所以我尝试使用数组,但它显示的是最后一行的字母。
<table align="center" style="width:800px;">
<thead>
<tr>
<th>#</th>
<th style="text-align:center;">Item Name</th>
<th style="text-align:center;">Total Quantity</th>
<th style="text-align:center;">Quantity</th>
<th style="text-align:center;">Buy Price</th>
<th>Total</th>
</tr>
</thead>
<tbody id="invoice_item">
<?php
$query1="select * from purchase_details where invoice_no=$id";
$query1_run=mysqli_query($connect , $query1);
while($row=mysqli_fetch_array($query1_run)){
$product_name = $row['product_name'];
}
echo $product_name[0];
?>
<tr>
<td><b id="number"></b></td>
<td>
<select name="pid[]" class="form-control form-control-sm" required>
<option selected=""></option>
</select>
</td>
<td><input name="tqty[]" readonly type="text" class="form-control form-control-sm" ></td>
<td><input name="qty[]" type="text" class="form-control form-control-sm" required value=""></td>
<td><input name="price[]" type="text" class="form-control form-control-sm" readonly value=""></td>
<td></td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
您应该在表格数据之后结束while循环
<table align="center" style="width:800px;">
<thead>
<tr>
<th>#</th>
<th style="text-align:center;">Item Name</th>
<th style="text-align:center;">Total Quantity</th>
<th style="text-align:center;">Quantity</th>
<th style="text-align:center;">Buy Price</th>
<th>Total</th>
</tr>
</thead>
<tbody id="invoice_item">
<?php
$query1="select * from purchase_details where invoice_no=$id";
$query1_run=mysqli_query($connect , $query1);
while($row=mysqli_fetch_array($query1_run)){
$product_name = $row['product_name'];
echo $product_name[0];
?>
<tr>
<td><b id="number"></b></td>
<td>
<select name="pid[]" class="form-control form-control-sm" required>
<option selected=""></option>
</select>
</td>
<td><input name="tqty[]" readonly type="text" class="form-control form-control-sm" ></td>
<td><input name="qty[]" type="text" class="form-control form-control-sm" required value=""></td>
<td><input name="price[]" type="text" class="form-control form-control-sm" readonly value=""></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>