2012-08-31 56 views
0

我嘗試從我的另一頁的數據打印出來給我的新的一頁,當我點擊提交的cart.php檢索數據輸入到一個新的頁面在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> 
    } 
} 

我試圖用這個代碼,我嘗試retrie打印出從功能車()

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

如果我正確理解你的問題,你只是想把這個:'Array([it em_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'。是對的嗎? – Travesty3

+0

是的......這就是我想要的 –

+0

你知道該怎麼做嗎......我被困在這個問題上超過10個小時......對我來說太糟糕了。 –

回答

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」,「量」,「數量」

+1

err ...我只是複製上面的代碼粘貼到bill.php,對嗎?還是我需要將我的代碼更改爲您的代碼? –

+0

我已經嘗試了你的代碼..只是複製粘貼到我的bill.php ....沒有不同的結果 –

+0

不,不,複製粘貼是不夠的,這更像是一般指南,你知道如何用PHP或其他編程語言編寫嗎? – bodi0

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 />"; 
} 
+0

但是item_number,item_name等等是來自用戶的輸入... –

+0

HTML 元素的名稱是從用戶輸入的? – Travesty3

相關問題