Php Foreach中的Foreach,而不是在HTML表中显示它
你好。我现在尝试创建来自sql表的细节 以下是我的PHP:-Php Foreach中的Foreach,而不是在HTML表中显示它,php,foreach,Php,Foreach,你好。我现在尝试创建来自sql表的细节 以下是我的PHP:- public function export() { $query = $this->input->cookie("cookie_invent_query"); $data['header'] = $this->modelmodel->showdata($query); foreach($data['header'] as $header){
public function export()
{
$query = $this->input->cookie("cookie_invent_query");
$data['header'] = $this->modelmodel->showdata($query);
foreach($data['header'] as $header){
$data['detail'][] = $this->modelmodel->showdata("select * from DeliveryOrderDetail
where deliveryordertransno = '".$header->TransactionNo."' ");
}
echo "<pre>"; print_r($data['detail']);
$this->load->view("do_mutasi/export",$data);
}
来自$data['detail']的结果
<?php foreach($header as $hdr) { ?>
Header
<table class="table">
<thead>
<tr>
<th>Trnsaction No</th>
<th>Document No</th>
<th>To Customer</th>
<th>Delivery Order Date</th>
</tr>
</thead>
<tbody>
<td><?=$hdr->TransactionNo;?></td>
<td><?=$hdr->DocumentNo;?></td>
<td><?=$hdr->ToCustomerCode;?></td>
<td><?=$hdr->DeliveryOrderDate;?></td>
</tbody>
</table>
Detail
<table>
<thead>
<tr>
<th>Transaction No</th>
<th>Item Code</th>
<th>Quntity</th>
<th>Uom Code</th>
</tr>
</thead>
<tbody>
<?php
foreach($detail as $rsltdt =>$key) { ?>
<tr>
<td><?=$rsltdt['TransactionNo'];?></td>
<td><?=$rsltdt['ItemCode'];?></td>
<td><?=$rsltdt['Quantity'];?></td>
<td><?=$rsltdt['UOMCode'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
然后在我把它发送到我的视图之后,这就是我现在能做的
public function export()
{
$query = $this->input->cookie("cookie_invent_query");
$data['header'] = $this->modelmodel->showdata($query);
foreach($data['header'] as $header){
$data['header']['details'] = $this->modelmodel->showdata("select * from DeliveryOrderDetail
where deliveryordertransno = '".$header->TransactionNo."' ");
}
}
标题
交易编号
文件编号
给顾客
交货单日期
细节
交易编号
项目代码
数量
计量单位代码
你可以看到。我在标题
循环中循环细节
。因为在一些记录中有多个细节。我循环标题是因为我想显示多条记录
,而不仅仅是一条记录
这是目前为止的结果
我不能透露细节。所以我的问题是。如何显示细节取决于标题
[TransactionNo]=[DeliveryOrderTransNo]
最好将所有详细信息放在相应的标题中:
foreach($hdr['details'] as $rsltdt) { ?>
<tr>
<td><?=$rsltdt->TransactionNo;?></td>
<td><?=$rsltdt->ItemCode;?></td>
<td><?=$rsltdt->Quantity;?></td>
<td><?=$rsltdt->UOMCode;?></td>
</tr>
<?php } ?>
然后您可以简单地编写第二个循环:
$data['header']['details'] = $this->modelmodel->showdata("select * f....
而不是:
foreach($detail as $rsltdt) {
foreach ($rsltdt as $rsl){ ?>
<tr>
<td><?=$rsl->TransactionNo;?></td>
<td><?=$rsl->ItemCode;?></td>
<td><?=$rsl->Quantity;?></td>
<td><?=$rsl->UOMCode;?></td>
</tr>
<?php } } ?>
检查一次:-
foreach($rsltdt详细信息){
外汇($rsltdt为$rsl){?>
foreach($rsltdt为详细信息){foreach($rsltdt为$rsl){?>我收到这个错误无法使用stdClass类型的对象作为数组
@YVS1102更新了答案,试试看!
$data['header']['details'] = $this->modelmodel->showdata("select * f....
$data['header']['details'][] = $this->modelmodel->showdata("select * f....
foreach($detail as $rsltdt) {
foreach ($rsltdt as $rsl){ ?>
<tr>
<td><?=$rsl->TransactionNo;?></td>
<td><?=$rsl->ItemCode;?></td>
<td><?=$rsl->Quantity;?></td>
<td><?=$rsl->UOMCode;?></td>
</tr>
<?php } } ?>