多维会话数组 [英] multi dimension session array

查看:184
本文介绍了多维会话数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种将两个MySQL数据库中的数据提取到单个动态页面中的表单.当用户单击添加到购物车时,我想将该数据存储在多维会话数组中,以供以后在单击查看购物车时调用.我想知道当从添加到购物车表单中添加新项目时,如何自动增加该项目的子集标识符(数组键?).这是我到目前为止的内容:

I am using a form that is pulling data from two MySQL databases into a single dynamic page. When a user clicks add to cart I want to store that data in a multi dimensional session array to call up later when they click view cart. I am wondering how to auto increment the subset identifier(array key?) of the item when a new item is added from the add to cart form. This is what I have so far:

$newitem = array ($row_getimages['icon'],$row_getimages['title'],$row_getshoppingcart['medium'],$row_getshoppingcart['size'],$row_getshoppingcart['price'],$row_getshoppingcart['shipping']);


session_start();

if(isset($_SESSION['item'][1]))
$_SESSION['item'][1] = $_SESSION['item'][1]+ 1;
else
$_SESSION['item'][1] = 1;

对于以后调出数据的任何帮助,也将不胜感激.由于用户可能在会话中存储了1或20个项目,因此我不确定如何确保无论添加多少项目都将回显所有项目.

Also any help for calling out the data later would be appreciated. As a user may have 1 or 20 items stored in the session I am not sure how to make sure all items would be echoed no matter how many they have added.

这是我第一次参加多维数组和会话.显然,由于图像页面是动态的并且购买价格是基于多个因素的,因此像过去一样仅使用MySQL的可用商品数据库就不可能了.

This is my first time at a multi dimensional array and sessions. Obviously because the image page is dynamic and purchase price is based on several factors, just using a MySQL database of available items as I have in the past is out of the question.

提前感谢您的时间.

推荐答案

$newitem = array ('id' => $row_getshoppingcart['id'] , 'icon' => $row_getimages['icon'],'title' => $row_getimages['title'],'medium' => $row_getshoppingcart['medium'],'size' => $row_getshoppingcart['size'],'price' => $row_getshoppingcart['price'],'shipping' => $row_getshoppingcart['shipping']);


session_start();

$_SESSION['item'][] = $newitem;

如果我正确理解您的系统,这就是您要做的一切.

That is all you have to do, if I understand your system correctly.

更新

我更新了$newitem数组以包含数组键.您可以使用以下数组来引用新商品信息:

I updated the $newitem array to include array keys. You can reference the new item info with arrays like this:

$ _ SESSION ['item'] [(num)] ['id']

$_SESSION['item'][(num)]['id']

或者您可以像这样遍历结果:

Or you can loop through the results like this:

foreach ( $_SESSION['item'] AS $item ) 
{  
 echo 'id: ' . $item['id'] . '<br />'; 
 echo 'title: ' . $item['title'];  
 // and so on 
}

这篇关于多维会话数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆