使用 foreach 传递关联数组以查看和访问其中的值
passing associative array to view and accessing values in it using foreach
这是我在控制器中的代码...
function print_dm($delivery_memo_id){
$data1=array();
$query=$this->db->query("select de.*, pr.* from phppos_delivery_memo de inner join
phppos_productdetails pr on de.delivery_memo_id=pr.delivery_memo_id WHERE
de.delivery_memo_id=$delivery_memo_id");
if($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data1[]=$row->product_id;
$data1[]=$row->product_name;
$data1[]=$row->quantity;
$data1[]=$row->unit;
$data1[]=$row->unit_rate;
}
}
else
{
$data1 = array();
}
$this->load->view('delivery_memo/print_layout',$data1);
}
当我 print_r $data1 我得到这个..
Array ( [0] => 1 [1] => Zinc Sulphate [2] => 1 [3] => 1 [4] => 100 [5] => 4 [6] => Copper
Sulphate [7] => 1 [8] => 12 [9] => 100 [10] => 9 [11] => Sulphur 80% WDG [12] => 1 [13] => 1
[14] => 100 )
我传给上面的查看。但是我如何在那里访问它或者我应该更改控制器中的代码??
我在那个数组中得到了多条记录
显然您不了解如何访问传递给视图的数据。您正在将一个数组传递给您的视图,视图中不会有 $data1
变量,在您传递的数组中会有像键一样命名的变量。所以你应该像这样传递数据:
$this->load->view('delivery_memo/print_layout',array('data1' => $data1));
之后 $data1
将可以在视图中访问:
var_dump($data1);
查看更多信息:https://ellislab.com/codeigniter/user-guide/general/views.html
直接将查询结果传递给查看:
function print_dm($delivery_memo_id){
$data1=array();
$query=$this->db->query("select de.*, pr.* from phppos_delivery_memo de inner join
phppos_productdetails pr on de.delivery_memo_id=pr.delivery_memo_id WHERE
de.delivery_memo_id=$delivery_memo_id");
$this->load->view('delivery_memo/print_layout', array('data1' => $query->result()));
}
然后您应该能够在视图中访问您的 $data1
变量,例如:
<?php foreach ($data1 as $row) { echo $row->product_id . "\n"; } ?>
这是我在控制器中的代码...
function print_dm($delivery_memo_id){
$data1=array();
$query=$this->db->query("select de.*, pr.* from phppos_delivery_memo de inner join
phppos_productdetails pr on de.delivery_memo_id=pr.delivery_memo_id WHERE
de.delivery_memo_id=$delivery_memo_id");
if($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data1[]=$row->product_id;
$data1[]=$row->product_name;
$data1[]=$row->quantity;
$data1[]=$row->unit;
$data1[]=$row->unit_rate;
}
}
else
{
$data1 = array();
}
$this->load->view('delivery_memo/print_layout',$data1);
}
当我 print_r $data1 我得到这个..
Array ( [0] => 1 [1] => Zinc Sulphate [2] => 1 [3] => 1 [4] => 100 [5] => 4 [6] => Copper
Sulphate [7] => 1 [8] => 12 [9] => 100 [10] => 9 [11] => Sulphur 80% WDG [12] => 1 [13] => 1
[14] => 100 )
我传给上面的查看。但是我如何在那里访问它或者我应该更改控制器中的代码??
我在那个数组中得到了多条记录
显然您不了解如何访问传递给视图的数据。您正在将一个数组传递给您的视图,视图中不会有 $data1
变量,在您传递的数组中会有像键一样命名的变量。所以你应该像这样传递数据:
$this->load->view('delivery_memo/print_layout',array('data1' => $data1));
之后 $data1
将可以在视图中访问:
var_dump($data1);
查看更多信息:https://ellislab.com/codeigniter/user-guide/general/views.html
直接将查询结果传递给查看:
function print_dm($delivery_memo_id){
$data1=array();
$query=$this->db->query("select de.*, pr.* from phppos_delivery_memo de inner join
phppos_productdetails pr on de.delivery_memo_id=pr.delivery_memo_id WHERE
de.delivery_memo_id=$delivery_memo_id");
$this->load->view('delivery_memo/print_layout', array('data1' => $query->result()));
}
然后您应该能够在视图中访问您的 $data1
变量,例如:
<?php foreach ($data1 as $row) { echo $row->product_id . "\n"; } ?>