购物车没有价值

时间:2017-11-30 20:09:00

标签: php

https://kopy.io/6Ud9J https://kopy.io/7tFRb

在第一段代码中,我有view_cart.php,但它没有显示值。

Viewcart.php

<form method="post" action="cart_update.php">
<table width="100%"  cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>pret</th><th>Total</th><th>Remove</th></tr></thead>
  <tbody>
    <?php
    if(isset($_SESSION['prodcos'])) //check session var
    {
        $total = 0; //set initial total value
        $b = 0; //var for zebra stripe table 
        foreach ($_SESSION['prodcos'] as $cart_itm)
        {
            var_dump($cart_itm);
            //set variables to use in content below
            $titlu = $cart_itm['titlu'];
            $cantitate = $cart_itm['cantitate'];
            $pret = $cart_itm['pret'];
            $id = $cart_itm['id'];
            $subtotal = ($pret * $cantitate); //calculate pret x Qty
            var_dump($titlu);
            var_dump($pret);
            var_dump($titlu);
            echo '<tr>';
            echo '<td><input type="text" size="2" maxlength="2" name="cantitate['.$id.']" value="'.$cantitate.'" /></td>';
            echo '<td>'.$titlu.'</td>';
            echo '<td>'.$pret.$currency.'</td>';
            echo '<td>'.$currency.$subtotal.'</td>';
            echo '<td><input type="checkbox" name="remove_code[]" value="'.$id.'" /></td>';
            echo '</tr>';
            $total = ($total + $subtotal); //add subtotal to total var
        }

        $grand_total = $total + $shipping_cost; //grand total including shipping cost
        foreach($taxes as $key => $value){ //list and calculate all taxes in array
                $tax_amount     = round($total * ($value / 100));
                $tax_item[$key] = $tax_amount;
                $grand_total    = $grand_total + $tax_amount;  //add tax val to grand total
        }

        $list_tax       = '';
        foreach($tax_item as $key => $value){ //List all taxes
            $list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
        }
        $shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';
    }
    ?>
    <tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
    <tr><td colspan="5"><a href="index.php" class="button">Add More Items</a><button type="submit">Update</button></td></tr>
  </tbody>
</table>
<input type="hidden" name="return_url" value="<?php 
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</form>

update_cart.php

<?php
session_start();
include_once("includes/config.php");

//add product to session or create new one
if(isset($_POST['type']) && $_POST['type']=='add' && $_POST['cantitate']>0)
{
    foreach($_POST as $key => $value){ //add all post vars to new_product array
        $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
    }
    //remove unecessary vars
    unset($new_product['type']);
    unset($new_product['return_url']); 
    $id = $_POST['id'];
    $sth = $db->prepare("SELECT titlu, pret FROM produse WHERE id = :id");
    $sth->bindParam(':id', $id);
    $sth->execute();
//    $statement = $db->query('SELECT titlu, pret FROM produse WHERE id="$id"');
    while($sth->fetch(PDO::FETCH_ASSOC)){

        //fetch product name, pret from db and add to new_product array
        $new_product['titlu'] = $titlu; 
        $new_product['pret'] = $pret;

        if(isset($_SESSION['prodcos'])){  //if session var already exist
            if(isset($_SESSION['prodcos'][$new_product['id']])) //check item exist in products array
            {
                unset($_SESSION['prodcos'][$new_product['id']]); //unset old array item
            }           
        }
        $_SESSION['prodcos'][$new_product['id']] = $new_product; //update or create product session with new item  
    } 
}


//update or remove items 
if(isset($_POST['cantitate']) || isset($_POST['remove_code']))
{
    //update item quantity in product session
    if(isset($_POST['cantitate']) && is_array($_POST['cantitate'])){
        foreach($_POST['cantitate'] as $key => $value){
            if(is_numeric($value)){
                $_SESSION['prodcos'][$key]['cantitate'] = $value;
            }
        }
    }
    //remove an item from product session
    if(isset($_POST['remove_code']) && is_array($_POST['remove_code'])){
        foreach($_POST['remove_code'] as $key){
            unset($_SESSION['prodcos'][$key]);
        }   
    }
}

//back to return url
$return_url = (isset($_POST['return_url']))?urldecode($_POST['return_url']):''; //return url
header('Location:'.$return_url);

?>

当我添加产品时,在购物车中,它没有显示详细信息..

0 个答案:

没有答案