问题描述
以上是我的意思
Above is what I'm trying achieve in my code.
* P = Product
*P=Product
下面是我的代码。我需要做的就是使用foreach显示'id'和'qty'。现在只显示键'id'及其值。
Below is my code. All I need to do is to display the 'id' as well as 'qty' using foreach.Now it only displays key 'id' and it's value.
$_SESSION['items'][]=array('id'=>$id,'qty'=>$qty);
而不是
$_SESSION['items']=array('id'=>$id,'qty'=>$qty);
但是我遇到此错误:问题出现:致命错误:[ 。运算符不支持字符串。.
BUT I'm getting this error:Problem with: Fatal error: [] operator not supported for string..
PHP
<?php
session_start();
?>
<?php
$id=$_GET['id'];
$qty=$_GET['qty'];
$_SESSION['items']=array('id'=>$id,'qty'=>$qty);
print_r($_SESSION['items']);
foreach($_SESSION['items'] as $items=>$value)
{
echo $items;
echo $value;
}
?>
编辑:
我设法解决问题。但是,现在,我正在尝试为每个产品添加以上内容。我的意思是,
I manage to solve the issue. However now,I'm trying to include the above for each product. I mean,
我正在获取如下数据:
$id = $_GET['id'];
$qty = $_GET['qty'];
$product_id=$_GET['product_id'];
//会话中的商店详细信息
//store details inside session
$_SESSION['items'][]= array('id'=>$id,'qty'=>$qty,'product_id'=>$product_id);
print_r($_SESSION['items']);
$item_id= explode(',',$_SESSION['items']['id']);
$item_qty= explode(',',$_SESSION['items']['qty']);
$item_product= explode(',',$_SESSION['items']['product_id']);
//从数据库中获取信息
//retrive info from database
mysql_select_db($database_wedding_conn, $wedding_conn);
$select="SELECT tbl_additional_info.*,tbl_productscontents.* FROM
tbl_additional_info,tbl_productscontents WHERE tbl_additional_info.productid='$product_id AND tbl_additional_info.product_id=tbl_productscontents.product_id GROUP BY tbl_productscontents.productid'";
$result=mysql_query($select)or die(mysql_error());
这就是它在表格中的显示方式
THis is how it displays in table
<table>
<tr>
<th>Product</th>
<th>Size</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
<?php
foreach($item_product as $p=>$p_value)
{
?>
<tr>
<td valign="top">
<?php
echo $p;
echo $p_value;
?>
</td>
<td>
<table>
<?php
foreach($item_id as $i)
{
?>
<tr>
<td>
<?php echo $i;?>
</td>
</tr>
<?php
}
?>
</table>
</td>
<td>
<?php
foreach($item_qty as $q)
{
//unit price
// echo $q;
}
?>
</td>
<td>
<table>
<?php
foreach($item_qty as $q)
{
?>
<tr>
<td>
<?php echo $q;?>
</td>
</tr>
<?php
}
?>
</table>
</td>
<td>
<?php
foreach($item_qty as $q)
{
//amount
//echo $q;
}
?>
</td>
</tr>
<?php
}
?>
</table>
问题:如何显示会话中存储的所有产品? for每个循环仅显示一个产品。它不会添加更多内容,如print_r($ _ SESSION ['items'])
Question: How to I display all the product stored in the session? The for each loop only shows one product.It doesn't add more as it shows in print_r($_SESSION['items'])
推荐答案
您正在使用的是一个关联数组,它只是一个维度。之所以具有关联性,是因为您可以通过关联(即您赋予它的字符串值)(即 id和 qty)来访问它们,而不是通过索引号(即索引数组)访问值。
What you're working with is an associative array and it's only one dimension. It's associative because rather than access the values by an index number (i.e. an index array) you can access them by an association which is the string value you're giving it (i.e. the 'id' and the 'qty').
如果您要做的只是尝试输出这些关联的值,则可以在没有foreach循环的情况下进行输出:
If all you're doing is trying to output the value of those assoications, you can do so without the foreach loop:
echo $_SESSION['items']['id'];
echo $_SESSION['items']['qty'];
您在这里说的是:
在关联数组中 $ _ SESSION
,给我键 items
的值,然后在它的关联数组中给我键 id
。重复相同的操作,但给我键数量
What you're doing here is saying:"In the associative array $_SESSION
, give me the value for the key items
and then in it's associative array give me the key id
. Repeat the same but give me the key qty
"
编辑:
因此,如果您期望包含数组值的查询字符串,例如:
So if you're expecting a query string containing array values, for example:
http://example.com?id[]=1&id[]=2&qty[]=10&qty[]=20
您可以使用for循环而不是foreach来访问值:
You could access the values with a for loop instead of a foreach:
<?php
$id=$_GET['id'];
$qty=$_GET['qty'];
for($i=0;$i < count($id) ; $i++){
echo "ID: " . $id[$i] . " has QTY: " . $qty[$i] . "<br />";
}
?>
这假定您将始终具有与数量值相同的id值数量,但是您可以添加逻辑处理不均匀的分组。
This assumes you'll always have the same number of id values as qty values, but you could add logic in to handle an uneven grouping.
这篇关于如何使用foreach显示多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!