Php 在会话变量中存储数组并在codeigniter中使用foreach检索它
这是我在控制器中的代码Php 在会话变量中存储数组并在codeigniter中使用foreach检索它,php,codeigniter,session,Php,Codeigniter,Session,这是我在控制器中的代码 class placeorder_ajax extends CI_controller { function __construct() { parent::__construct(); } //change text to check line endings //new line endings function index() { //echo "hii"; //echo "<script>alert('dasdas');
class placeorder_ajax extends CI_controller
{
function __construct()
{
parent::__construct();
}
//change text to check line endings
//new line endings
function index()
{
//echo "hii";
//echo "<script>alert('dasdas');</script>";
//unset($_SESSION['cart']);
$data = array('product_id'=>$this->input->post('product_id'),
'quantity'=>$this->input->post('quantity'),
'unit'=>$this->input->post('unit'),
'unit_rate'=>$this->input->post('unit_rate'));
$this->session->set_userdata($data);
print_r($data);
?>
<table>
<tr>
<th>Item Name</th>
<th>Quantity</th>
<th>Amount</th>
<th>Action</th>
</tr>
<?php
$i=0;
foreach($_SESSION['cart'] as $cart)
{
//echo "<pre>"; print_r($cart); echo "</pre>";
$product_name = $this->db->query("SELECT product_name FROM product WHERE
product_id='".$cart['product_id']."'");
echo "<tr>";
echo "<td>".$product_name."</td>";
echo "<td>".$cart['quantity']."</td>";
echo "<td>".$cart['unit']."</td>";
echo "<td>".$cart['unit_rate']."</td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_cart'><img
src='assets/img/delete.png'/></a></td>";
echo "</tr>";
$i++;
}
?>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table><?php
}
}
}
class placeorder\u ajax扩展了CI\u控制器
{
函数_u构造()
{
父项::_构造();
}
//更改文本以检查行尾
//新行结尾
函数索引()
{
//回声“hii”;
//回显“警报('dasdas');”;
//取消设置($_会话['cart']);
$data=array('product\u id'=>$this->input->post('product\u id'),
“数量”=>$this->input->post('quantity'),
“单位”=>this->input->post('unit'),
‘单价’=>this->input->post(‘单价’);
$this->session->set_userdata($data);
打印(数据);
?>
项目名称
量
数量
行动
您的数组是关联数组。因此,请使用foreach循环将其打印为键值对
$myarray = $_POST;
$_SESSION['myarray_insession'] = $myarray;
foreach($\u会话['cart']作为$key=>$value){
//$key将是产品id、数量等
//$value将是相应的值
echo“key:$key
value:$value”;
//仅用于格式化输出
//这只会再插入一个中断
回声“
”;
}
试着做这样的事情
$myarray = array('product_id'=>$this->input->post('product_id'),
'quantity'=>$this->input->post('quantity'),
'unit'=>$this->input->post('unit'),
'unit_rate'=>$this->input->post('unit_rate'));
$_SESSION['myarray_insession'] = $myarray;
或
然后像这样使用foreach
问题是什么?我不知道如何在会话中存储该数组,以及如何使用foreach loop检索该数组值,无法使其工作的意思是什么
有错误吗?@KedarB:检查这里如何在会话中存储数据-@Napster我做了这个$this->session->set_userdata($data);但我应该将其存储在哪里,如何检索?
$myarray = array('product_id'=>$this->input->post('product_id'),
'quantity'=>$this->input->post('quantity'),
'unit'=>$this->input->post('unit'),
'unit_rate'=>$this->input->post('unit_rate'));
$_SESSION['myarray_insession'] = $myarray;
foreach($mysession as $row=>$value){
}