我有以下 php 脚本:
<?php
session_start();
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$total=0;
echo 'you have following books';
echo '</br>';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = "SELECT * FROM books WHERE bcode ='$id'";
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$a = $output[] = '<td> '.$bname.'</td>';
$output[] = '<td>'.$price.'rs.</td>';
$output[] = '<td>*'.$qty.'</td>';
'</br>';
$output[] = '<td>Rs.'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>Rs.'.$total.'</strong></p>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
?>
有没有办法将foreach
循环的结果存储在变量中?.ie $a 将包含书名,但如果有两本书 $a 的值会被下一本书覆盖?