当我点击cart.php中的提交时,我尝试将我的另一个页面中的数据打印到我的新页面
这是我的代码
cart.php
function paypal_items(){
$num = 0;
foreach ($_SESSION as $name => $value){
if ($value !=0){
if (substr($name, 0, 5) == "cart_"){
$id = substr ($name, 5, strlen($name)-5);
$get = mysql_query ('SELECT id, name, price FROM menu WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$num++;
echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">';
echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
echo '<input type="hidden" name="amount_'.$num.'" value="'.$get_row['price'].'">';
echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';
}
}
}
}
}
function cart() {
$total = 0;
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5) == 'cart_'){
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM menu WHERE id=' .mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)){
$sub = $get_row['price'] * $value;
$target_data = $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
$_SESSION['target_data'] = $target_data;
echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />' ;
}
}
$total += $sub;
}
}
if ($total == 0){
echo "Your Cart Is Empty";
}
else {
echo "<p>Total : $".number_format($total, 2).'</p>';
<p>
<form action="bill.php" method="post">
<?php paypal_items(); ?>
<input type="submit" value="submit" />
</form>
</p>
}
}
我尝试从功能购物车()
打印出这些数据 echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />' ;
使用此代码我尝试检索所有数据输入
bill.php
<?php
session_start();
echo $_SESSION['target_data'] .'<br />';
print_r($_POST);
?>
然而我得到的输出就像(当我点击提交时我有两个数据显示)
Milk Tea x 9 @ $1.99 = $17.91
Array ( [item_number_1] => 2 [item_name_1] => Ayam Penyet [amount_1] => 6.99 [quantity_1] => 8 [item_number_2] => 1 [item_name_2] => Milk Tea [amount_2] => 1.99 [quantity_2] => 9 )
我想要的预期输出就像
Milk Tea x 9 @$1.99 = $17.91
Ayam Penyet x 8 @6.99 = $55.92
任何人都知道如何解决这个问题?
谢谢:)
答案 0 :(得分:0)
尝试使用 foreach()循环:
foreach ($_POST as $key=>$value) {
$printed .= $value .' ';
}
echo $printed;
上面的代码将连接POST
请求中的所有元素和空格''。您可以进行其他过滤,以便根据需要提取和排序发布的数据。例如:
foreach ($_POST as $key=>$value) {
if (in_array($key, array('item_name','amount','quantity')))
$printed .= $value .' ';
}
上述代码只有在属于'item_name','amount','quantity'时才会连接已发布的元素值。
答案 1 :(得分:0)
我会更改输入的名称:
while ($get_row = mysql_fetch_assoc($get)) {
$num++;
echo '<input type="hidden" name="items['.$num.'][item_number]" value="'.$id.'">';
echo '<input type="hidden" name="items['.$num.'][item_name]" value="'.$get_row['name'].'">';
echo '<input type="hidden" name="items['.$num.'][amount]" value="'.$get_row['price'].'">';
echo '<input type="hidden" name="items['.$num.'][quantity]" value="'.$value.'">';
}
使用这些项目名称,PHP将接收值作为关联数组,看起来更像是这样:
Array (
[0] => Array (
[item_number] => 2,
[item_name] => "Ayam Penyet",
[amount] => 6.99,
[quantity] => 8
),
[1] => Array(
[item_number] => 1,
[item_name] => "Milk Tea",
[amount] => 1.99,
[quantity] => 9
)
)
然后你可以像这样循环:
foreach ($_POST["items"] as $item)
{
echo "{$item["item_name"]} x {$item["quantity"]} @\${$item["amount"]} = \$". number_format($item["quantity"] * $item["amount"], 2) ."<br />";
}