PHP can't echo for each array
I am trying to echo out an array using for each but it's not displaying any values. The only values i can echo out is within a second for each.
Can anyone tell me what i'm doing wrong?
What i am trying to echo is the price, item, description etc but i am getting nothing.
If you need to see the output of the array it's here http://operationbraveheart.org.uk/jcart/testshop.php
while ($row = $result->fetch()) {
$superitem[$row['itemid']][] = $row;
}
foreach($superitem AS $subitem) {
list($prodid,$item,$size,$description,$price) = $subitem[0];
if ($count % NUMCOLS == 0) echo "<tr>"; # new row
echo '<td>';
var_dump($subitem);
//Your normal code up until the select box...
echo '<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="'.$_SESSION['jcartToken'].'" />
<input type="hidden" name="my-item-id" value="'.$subitem['prodid'].'" />
<input type="hidden" name="my-item-price" value="'.$subitem['price'].'" />
开发者_StackOverflow中文版 <input type="hidden" name="my-item-url" value="http://yahoo.com" />';
if(count($subitem) > 1) {
echo '<li><select name="my-item-name" id="foo">';
foreach($subitem AS $subsubitem) {
echo "<option value='".$subsubitem['size']."'>".$subsubitem['size']."</option>";
}
echo "</select></li>";
}
else {
echo '<input type="hidden" name="my-item-name" value="'.$subitem['item'].'" />';
}
echo'<li>Price: $<span class="price">'.$subitem['price'].'</span></li>
<li>
<label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label>
</li>
</ul>
<input type="submit" name="my-add-button" value="add to cart" class="button" />
</fieldset>
</form>';
echo '</td>';
$count++;
$counter++;
if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row
}
Current it looks like $subitem contains an array of length 1 where the first index is a row. Change...
$superitem[$row['itemid']][] = $row;
should be...
$superitem[$row['itemid']] = $row;
and...
list($prodid,$item,$size,$description,$price) = $subitem[0];
should be...
list($prodid,$item,$size,$description,$price) = $subitem;
精彩评论