2013-03-17 118 views
1

認爲我完成了這個完全錯誤的,但我試圖顯示在添加到購物車時項目的值到表中。我的代碼如下所示:在表中顯示購物車

<?php 
function minicart() { 
    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 products WHERE id='.mysql_real_escape_string((int)$id)); 
       while ($get_row = mysql_fetch_assoc($get)) { 
        $sub = $get_row['price']*$value; 
        '<table border = "1">' 
        '<tr>' 
        '<td>'<echo $get_row['name'].' x '.$value.' @ &pound;'.number_format($get_row['price'], 2).' = &pound;'.number_format($sub, 2).' <a href="minicart.php?remove='.$id.'">[-]</a> <a href="minicart.php?add='.$id.'">[+]</a> <a href="minicart.php?delete='.$id.'">[Delete]</a><br />''</tr>'; 
        '</tr>' 
        '</table>' 
       } 
      } 
      $total += $sub; 
     } 
    } 
    if ($total==0) { 
     echo "Your cart is empty"; 
    } 
    else { 
     echo '<br />Total: &pound;'.number_format($total, 2); 

    ?> 
+3

你的問題是什麼? – 2013-03-17 16:48:57

+0

你的問題是什麼? – 2013-03-17 16:50:21

+0

對不起我試圖顯示購物車的輸出到一個表我試着不同的變化,把等,不能很好地顯示 – Tom 2013-03-17 16:52:07

回答

0

試試這個:

function minicart() 
{ 
    $items = 0; 
    $tbl = array(); 
    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 products WHERE id='.mysql_real_escape_string((int)$id)); 
       $tbl[] = '<table border="1"><thead><tr>' 
        . '<th>Item</th>' 
        . '<th>Quantity</th>' 
        . '<th>Unit Price</th>' 
        . '<th>SubTotal</th>' 
        . '<th>Action</th>' 
        . '</tr></thead><tbody>' 
       ; 
       while ($get_row = mysql_fetch_assoc($get)) { 
        $items++; 
        $sub = $get_row['price'] * $value; 
        $tbl[] = '<tr>' 
         . '<td>' . $get_row['name'] . '</td>' 
         . '<td>' . $value . '</td>' 
         . '<td>&pound;' . number_format($get_row['price'], 2) . '</td>' 
         . '<td>$pound;' . number_format($sub, 2) . '</td>' 
         . '<td>' 
         . ' <a href="minicart.php?remove=' . $id . '">[-]</a> ' 
         . ' <a href="minicart.php?add=' . $id . '">[+]</a> ' 
         . ' <a href="minicart.php?delete=' . $id . '">[Delete]</a>' 
         . '</td>' 
         . '</tr>' 
        ; 
       } 
       $tbl[] = '</tbody>'; 
      } 
      $total += $sub; 
     } 
    } 
    if ($items==0) 
    { 
     echo "Your cart is empty"; 
    } 
    else 
    { 
     $tbl[] = '<tfoot><tr>' 
       . '<td colspan="3" style="text-align:right; font-weight:bold">Total:</td>' 
       . '<td>&pound;' . number_format($total, 2) . '</td></tr></tfoot></table>'; 
     echo implode("\n", $tbl); 

    } 
} 

我改變了邏輯,這樣如果項目數爲零,則顯示「空購物車」的消息,允許零成本項目。

0

我試圖重新寫你的表格式是否正確:

echo '<table border = "1">'; 
while ($get_row = mysql_fetch_assoc($get)) { 
    $sub = $get_row['price']*$value; 
    echo ' 
    <tr> 
    <td>'. 
    $get_row['name'].' x '.$value.' @ &pound;'.number_format($get_row['price'], 2).' = &pound;'.number_format($sub, 2).' 
    <a href="minicart.php?remove='.$id.'">[-]</a> 
    <a href="minicart.php?add='.$id.'">[+]</a> 
    <a href="minicart.php?delete='.$id.'">[Delete]</a><br /> 
    </td> 
    </tr> 
    '; 
} 
echo '</table>'; 

請參閱PHP Strings

0

我想你要更換的if語句與以下。

這會給你一個表,每行一行,可能是你在找什麼。

 if (substr($name, 0, 5)=='cart_') { 
      $id = substr($name, 5, (strlen($name) -5)); 
      $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id)); 
      echo '<table border = "1">'; 
      echo '<tr>'; 
      while ($get_row = mysql_fetch_assoc($get)) { 
       $sub = $get_row['price']*$value;      
       echo '<td>'; 
       echo $get_row['name'].' x '.$value.' @ &pound'; 
       echo number_format($get_row['price'], 2).' = &pound'; 
       echo number_format($sub, 2); 
       echo '<a href="minicart.php?remove='.$id.'">[-]</a> '; 
       echo '<a href="minicart.php?add='.$id.'">[+]</a> '; 
       echo '<a href="minicart.php?delete='.$id.'">[Delete]</a><br />'; 
      } 
      echo'</tr>'; 
      echo'</table>'; 
     }