-2

我正在为我的网站制作购物车。在购物车区域,我从数据库中提取运输详细信息,包括取决于邮政编码的价格。当用户点击“更新购物车”时。我正在尝试使用运费更新总数。如果有人能指出我解决这个问题的正确方向。

这是我显示购物车的功能。

function showCart() {
$cart = $_SESSION['cart'];
$zip = $_SESSION['zipcode'];
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">';
    $output[] = '<table>';
    foreach ($contents as $id=>$qty) {
        $sql = 'SELECT * FROM products WHERE id = '.$id;
        $result = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        extract($row);
        $output[] = '<tr>';
        $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
        $output[] = '<td>'.$title.' by '.$author.'</td>';
        $output[] = '<td>$'.$price.'</td>';
        $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
        $output[] = '<td>$'.($price * $qty).'</td>';
        $total += $price * $qty;
        $output[] = '</tr>';
    }
    $output[] = '</table>';

    $sql  = 'SELECT * FROM zipcodes WHERE zipcode = '.$zip;
        $result = mysql_query($sql) or die(mysql_error());
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

        $output[] = '<label>Price: </label><select name="delivery" id="delivery" class="long-field">';
        $output[] = '<option value="">Please Select</option>';
        $output[] = '<option value='.$row['free_dev'].'>$'.$row['free_dev'].'</option>';
        $output[] = '<option value='.$row['next_day_dev'].'>$'.$row['next_day_dev'].'</option>';
        $output[] = '</select>';
        }

    $output[] = '<p>Delivery Charge: <strong>$'.$delivery.'</strong></p>';          
    $output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
    $output[] = '<div><button type="submit">Update cart</button></div>';
    $output[] = '</form>';
} else {
    $output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);

}

这是通过 cart.php 更新的部分

case 'update':
if ($cart) {
    $newcart = '';
    foreach ($_POST as $key=>$value) {
        if (stristr($key,'qty')) {
            $id = str_replace('qty','',$key);
            $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) {
                if ($id != $item) {
                    if ($newcart != '') {
                        $newcart .= ','.$item;
                    } else {
                        $newcart = $item;
                    }
                }
            }
            for ($i=1;$i<=$value;$i++) {
                if ($newcart != '') {
                    $newcart .= ','.$id;
                } else {
                    $newcart = $id;
                }
            }
        }
    }
}
$cart = $newcart;
break;

谢谢你。任何帮助是极大的赞赏!PS我正在使用这里的购物车教程http://v3.thewatchmakerproject.com/journal/276/

4

2 回答 2

0

鉴于您提供的代码对这个问题相当不可知,因此不清楚您在哪里遇到了困难。但是为了让你开始:

case 'update':
if ($cart) {
    $newcart = '';
    foreach ($_POST as $key=>$value) {
        ... //same thing as before
    }
    $newcart .= $shipping_price; //or however you're encoding this. It's unclear.
}
$cart = $newcart;
break;

并替换:

$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';

和:

$output[] = '<p>Grand total: <strong>$'.$total+ $shipping_price.'</strong></p>';
于 2012-05-11T23:36:04.910 回答
0

如果我是你,我会搜索一个 PHP 类购物车,该代码让我想起了一些 PHP4(它应该是因为示例来自 2005 年)。

我没有找到一个很好的示例链接,我将尝试在下面描述这个想法。

您创建了两个类,一个代表商品,另一个代表购物车(第一个可以只是带有 mysql_fetch_object 的数据库的结果或类似 Pdo 之类的东西,它将把它变成来自 php 的 stdClass

class someItem 

{公共$id; 公共$数量;公开的$价格;公共 $totalprice;

public function __get($var)
{
    if ($var == 'totalPrice') {
        return $this->price*$this->qty;
    }

    return $this->$var;
}

}

类购物车{

private $items;

public function addItem($someItem)
{
    if (array_key_exists($someItem->id, $this->items)) {
        $this->items[$someItem->id]->qty = $this->items[$someItem->id]->qty + $someItem->qty;
    } else {
        $this->items[$someItem->id] = $someItem;
    }
}

public function removeItem($someItem)
{
    unset($this->items[$someItem->id]);
}

public function getTotal()
{
    $total = 0;
    foreach($this->items as $item){
        $total += $item->totalPrice;
    }
    return $total;
}

}

将信息序列化到会话中 $_SESSION['user']['cart'] = serialize($Shoppingcart);

用户登录时设置一个新的购物车,当他退出时清理它。添加和删​​除项目可以在一切之前完成

// 根据您的 HTML 表单执行任何操作 foreach ($_POST['cart'] as $itemId ) { $ItemObj = $db->getitemAsObject($itemId); // 使用 PDO 什么的 $shoppingcart->addItem($itemObj); }

发布后将购物车重新保存到会话 $_SESSION['user']['cart'] = serialize($Shoppingcart);

于 2012-05-11T23:50:01.823 回答