Jump to content

issue with code, not working quite right............... :'(


googlit

Recommended Posts

Hi

 

i am creating a shopping basket and am running into a few problems, the main one that for the world in me i cant figure out is why when i add two different items to cart/basket does my sub-total not pick up the second item, it works perfectly with one product (and multiples of same product through qty) but not with two or more...........

 

i've included the code from my functions which handles most of the math etc and my cart.php

 

functions.inc.php

 

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>Your basket is empty.</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping basket</a></p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
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 width="780" border="0">';
            foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM Products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();

            extract($row);
		$output[] = '<tr>';
    				//$output[] = '<td> </td>';
    				//$output[] = '<td>Model No</td>';
                    //$output[] = '<td>Name</td>';
    				//$output[] = '<td>Price (Ex-VAT)</td>';
                    //$output[] = '<td>VAT</td>';
                    //$output[] = '<td>Qty</td>';
    				//$output[] = '<td>Total</td>';
  				//$output[] = '</tr>
  				//<tr>';
    				$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';//remove from cart
                    $output[] = '<td>'.$Model_No.'</td>';//echo model number
                    $output[] = '<td>'.$Name.'</td>';//echo name
                    $output[] = '<td>'.round($Price / 1.175, 2).'</td>';//echo price ex VAT
    				$output[] = '<td>'.round(($Price / 47)*7, 2).'</td>';//echo VAT
    				$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';//Qty box
                    $output[] = '<td>£'.($Price * $qty).'</td>';//line total
              $output[] = '</tr>

              <tr>';
              	$output[] = '<td> </td>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                }
                $output[] = '<td><div align="right">Sub Total:</div></td>';
                $output[] = '<td> </td>';
                $output[] = '<td>£'.$Price.'</td>';//Sub-total formula to follow
              $output[] = '</tr>
              <tr>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                
                $output[] = '<td><div align="right">VAT Total:</div></td>';
                $output[] = '<td> </td>';
                $output[] = '<td>£'.round(($price_ex_VAT = $Price * 40 / 47) * $qty, 2).'</td>';//echo total VAT amount for order
              $output[] = '</tr>
              <tr>';
		  
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                $output[] = '<td> </td>';
                
                $output[] = '<td><div align="right">Grand Total:</div></td>';
                $output[] = '<td> </td>';
			$total += $Price * $qty;//method for total
                $output[] = '<td><strong>£'.$total.'</strong></td>';//display grand total
              $output[] = '</tr>';
	$output[] = '</table>';
        $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

 

<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $item) {
				if ($newcart != '') {
					$newcart .= ','.$item;
				} else {
					$newcart = $item;
				}
			}
		}
		$cart = $newcart;
	}
	break;
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;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Shopping Basket</title>
<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingbasket">
<div id="sb-top">
<h1>Your Shopping Basket</h1>

<?php
echo writeShoppingCart();
?>
</div>
<div id="sb-mid">


<h1>Please check quantities...</h1>
<table width="780" border="0"><tr><td width="90"> </td>
		  <td width="117">Model No</td>
		  <td width="123">Name</td>
        <td width="116">Price (Ex-VAT)</td>
		  <td width="139">VAT</td>
              <td width="50">Qty</td>
              <td width="115">Total</td>
		  </tr></table>

<?php
echo showCart();
?>
</div>
<p><a href="member-index.php">Back to products...</a></p>

</div>

</body>
</html>

 

any help would be once again greatly appreciated.....

Link to comment
https://forums.phpfreaks.com/topic/206213-issue-with-code-not-working-quite-right/
Share on other sites

You need to use a variable to store the "running Total" of your loop, everytime your loop occurs it overwrites the variables it set before. (Except the strings you are "Appending" to the ouput array).

 

Add a runningtotal variable to the end of the loop that adds the current total price to whatever is in it at the time - this way it will "count" the price, eg:

			$runningtotal += $Price * $qty;

 

Then your "Sub-Total" would be under $runningtotal - im sure you can rework the rest once you realise this.

 

-cb-

You need to use a variable to store the "running Total" of your loop, everytime your loop occurs it overwrites the variables it set before. (Except the strings you are "Appending" to the ouput array).

 

Add a runningtotal variable to the end of the loop that adds the current total price to whatever is in it at the time - this way it will "count" the price, eg:

			$runningtotal += $Price * $qty;

 

Then your "Sub-Total" would be under $runningtotal - im sure you can rework the rest once you realise this.

 

-cb-

 

would the line already in it not do this??

 

	

$total += $Price * $qty;//method for total

 

man im confused........ish

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.