[SOLVED] Caculating Shipping Costs??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

[SOLVED] Caculating Shipping Costs??

Post by mmc01ms »

Im trying to get the value of the shipping cost returned to a checkout form. I have functions that caculate shipping costs and return values the only problem is that i get the following error

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sites/manpreetsandhu.com/public_html/shopping_basket_fns.php on line 206
Not sure why must be doing something wrong with the sql??Any one help?

code is below:

Code: Select all

<?php
	
	session_start(); //must start a session for cart
	
	$qty = $_GET&#1111;'qty'];
	
		//checkout.php written by Manpreet Sandhu 26/01/2005
	require('page.inc'); //page class	
	require_once('shopping_basket_fns.php');
	
	$checkout = new Page();//create new page object
	
	$checkout -> Display();//use function display from page class
	
	display_main_menu(); //display side bar menu
	
	
	$link_id = db_Connect();//connect the database
	
	//query the database to retrieve all information out of the database regarding the current customer using the 
	//registered session variable which is their customer id.
	
	$query = mysql_query("SELECT * FROM customers WHERE customer_id='".$_SESSION&#1111;'customer_id']."'",$link_id); 
	$c = mysql_fetch_array($query);//fetch the results of the query and for them in an array and assign the value $c
	//to pass through to the display_checkout_form function.
	print '<table width="600">';
	print '<center><th align="center" colspan="6">';
	print "Checkout";
	print '</th></center>';
	print '<tr>';
	print '<td>';
	print "&#1111;<a href='show_cart.php'>Back</a>]";
	print "&#1111;<a href='vinyls.php'>Search for more vinyls</a>]";
	print '</td>';
	print '</tr>';
	print '<table>';
	print '<p></p>';
	
	if($_SESSION&#1111;'cart']&&array_count_values($_SESSION&#1111;'cart']))
	&#123;//if session values exist then display cart with the items and the checkout form else echo message
		display_cart($_SESSION&#1111;'cart'], false, 0);
		display_shipping(calculate_shipping_cost($qty));
		display_checkout_form($c);
	&#125;
	else 
		
		print 'There are no items in your cart';
	
	do_html_footer();
	
?>

Code: Select all

<?php
	
	
		//shopping_basket_fns.php written by Manpreet Sandhu 12/01/2005
	
	
	require_once('news_fns.php');
	
	function display_cart($cart, $change = true, $images = 1)
	&#123;
		//display what is in the shopping basket
		
		$cart = $_SESSION&#1111;'cart'];
		
		
		echo '<table border="0" width="100%" cellspacing="0">
			<form action="show_cart.php" method="post">
			<tr><th colspan="'. (1+$images) .'" bgcolor="#7b9815">Item</th>
			<th bgcolor="#7b9815">Price</th><th bgcolor="#7b9815">Qty</th>
			<th bgcolor="#7b9815">Total</th></tr>';
			
			//display each item as a table row
			
			foreach ($cart as $catno => $qty)
		&#123;
			$vinyl = get_vinyl_details($catno);
			print '<tr>';
			if($images == true)
			&#123;
				print '<td align="left">';
				if(file_exists("images/$catno.jpg"))
				&#123;
					$size = GetImageSize('images/'. $catno. '.jpg');
					if($size&#1111;0] > 0 && $size&#1111;1] > 0)
					&#123;
						print '<img src="images/'. $catno. '.jpg" ';
						print 'width='. $size&#1111;0]/3 .'height = '.$size&#1111;1]/3 . '>';
					&#125;
				&#125;
				else
					print '&nbsp;';
				print '</td>';
			&#125;
			print '<td align="left">';
			print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinyl&#1111;'title'].'</a> - '.$vinyl&#1111;'artist_id'].'</font>';
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price'], 2).'</font>';
			print '</td><td align="center">';
			
			//allowing change of qty
			
			if($change == true)//if change is true then show input box for qty
				echo'<input type="text" name="'.$vinyl&#1111;'cat_no'].'" value="'.$qty.'" size="1">';
			else
				echo $qty;//else just show qty and total price
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price']*$qty, 2).'</font></td></tr>';
			
		&#125;
		
		//display of total row
		
		echo '<tr>
					<th colspan="'. (2+$images) .'" bgcolor="#7b9815">&nbsp;</td>
					<th align="center" bgcolor="#7b9815">'.$_SESSION&#1111;'items'].'</th>
					<th align="center" bgcolor="#7b9815">£'.number_format($_SESSION&#1111;'total_price'], 2).'</th>
			 </tr>';
			
			//offer save feature
			
			if($change == true)
			&#123;
				echo '<tr>
						<td colspan="'. (2+$images) .'"&nbsp;</td>
						<td align="center">
						<input type="hidden" name="save" value="true">
						<input type="hidden" name="cat_no" value="$catno">
						<input type="hidden" name="qty" value="$qty">
						<input type="submit" value="Update Changes" alt="Update Changes">
						</td>
						<td></td>
					 </tr>';
				&#125;
			echo '</form></table>';
	&#125;
	
	function calculate_price($cart)
	&#123;
		// sum total price for all items in shopping cart
		$price = 0.0;
		if(is_array($cart))
		&#123;
			$link_id = db_connect();
			foreach($cart as $catno => $qty)
			&#123;  
				$query = "select price from vinyls where cat_no='".$catno."'";
				$result = mysql_query($query, $link_id);
				if ($result)
				&#123;
					//$row = mysql_fetch_row($result);
					$item_price = mysql_result($result, 0, 'price');
					//$item_price = $row&#1111;'price'];
					
					$price +=$item_price*$qty;
				&#125;
			&#125;
		&#125;
		return $price;
	&#125;
	
	
	function calculate_items($cart)
	&#123;
		//total items in the cart which goes through the cart and adds up the quantities of each item to get the total number og items
		
		$items = 0;
		if(is_array($cart))
		&#123;
			foreach($cart as $catno => $qty)
			&#123;
				$items += $qty;
			&#125;
		&#125;
		return $items;
	&#125;
	
	function get_customer_details($customer_id)
	&#123;
		$link_id = db_connect();
		$query = "select first_name, surname, postcode, address_line_one , address_line_two, city, county, initial  from customers where customer_id = '$customer_id'";
		
		$result = @mysql_query($query, $link_id) or die ("There was a problem with this query.");
		
		if (!$result)
			return false;
		$num_cust = @mysql_num_rows($result);
		if($num_cust == 0)
			return false;
		
		$result = db_result_to_array($result);
		return $result;
	&#125;
	
	function display_checkout_form($c)&#123;	
		//here we create a form where the users current address details in the database are brought up. If these are diffrent then the user
		//can edit them to the required shipping address.
?>

<br />
<table border="0" width="100%" cellspacing="0" cellpadding="0">
	<form  method="post" action="purchase.php" enctype="multipart/form-data">
		<tr><th colspan="2" bgcolor="CCCCCC">Shipping Address (only change if delivering to another address)</th></tr>
		<p></p>
		<tr>
			<td align="left">First Name: </td>
			<td><input name="ship_name" type="text" size="40" maxlength="20" value="<?php echo $c&#1111;'first_name']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Last Name: </td>
			<td><input name="ship_surname" type="text" size="40" maxlength="20" value="<?php print $c&#1111;'surname']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Title: </td>
			<td><input name="ship_initial" type="text" size="40"maxlength="4" value="<?php echo $c&#1111;'initial']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Address Line One: </td>
			<td><input name="ship_address_1" type="text" size="40" maxlength="25" value="<?php echo $c&#1111;'address_line_one']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Address Line Two: </td>
			<td><input name="ship_address_2" type="text" size="40" maxlength="25" value="<?php echo $c&#1111;'address_line_two']; ?>"></td>
		</tr>
		<tr>
			<td align="left">City:  </td>
			<td><input name="ship_city" type="text" size="40" maxlength="30" value="<?php echo $c&#1111;'city']; ?>"></td>
		</tr>
		<tr>
			<td align="left">County:  </td>
			<td><input name="ship_county" type="text" size="40" maxlength="30" value="<?php echo $c&#1111;'county']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Postcode:  </td>
			<td><input name="ship_postcode" type="text" size="40" maxlength="8" value="<?php echo $c&#1111;'postcode']; ?>"></td>
		</tr>
	</form>
</table>

<?
	&#125;
	
	function calculate_shipping_cost($qty)
	&#123;
		
		
		//if the qty of items is greater then the qty number in the database then we have a max charge of £20.00 delivery
		//if it is less then 15 then we check where the $qty of items is the same as qty in the table and retireive the correct
		//postage cost for that item.
		if($qty  > 15)
		&#123;
			$shipping_cost = 20.00;
		&#125;else
		$link_id = db_connect();
		
		//check the database to get the price of delivery depending on the number of items brought.
		$query = 'select * from shipping_costs where qty ="'.$qty.'"';
		$sc = mysql_fetch_array($query, $link_id);
		
		&#123;
			$shipping_cost = $sc&#1111;'delivery_charge'];
		&#125;
		
		return $shipping_cost;
	&#125;
	
	function display_shipping($shipping_cost)
	&#123;
		//displays the shipping cost in then checkout and payment forms
	
?>
<br />
<table width="600">
	<tr>
		
	    <th align="left">Shipping</th><th align="left"><?php echo number_format($shipping_cost, 2); ?></th>
		<th><?php echo $shipping_cost; ?></th>
		<th align="right">£<?php echo number_format($shipping_cost+$_SESSION&#1111;'total_price'], 2); ?></th>
	</tr>
</table><br />
<?php
	&#125;
	
	
	
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you didn't perform a query on or before that line to pass in a result.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

i've passed in a result and it does indeed get rid of the error but still doesn't return a value that has been calculated for shipping. new code foe shopping basket functions

Code: Select all

<?php
	
		//shopping_basket_fns.php written by Manpreet Sandhu 12/01/2005
	
	
	require_once('news_fns.php');
	
	function display_cart($cart, $change = true, $images = 1)
	&#123;
		//display what is in the shopping basket
		
		$cart = $_SESSION&#1111;'cart'];
		
		
		echo '<table border="0" width="100%" cellspacing="0">
			<form action="show_cart.php" method="post">
			<tr><th colspan="'. (1+$images) .'" bgcolor="#7b9815">Item</th>
			<th bgcolor="#7b9815">Price</th><th bgcolor="#7b9815">Qty</th>
			<th bgcolor="#7b9815">Total</th></tr>';
			
			//display each item as a table row
			
			foreach ($cart as $catno => $qty)
		&#123;
			$vinyl = get_vinyl_details($catno);
			print '<tr>';
			if($images == true)
			&#123;
				print '<td align="left">';
				if(file_exists("images/$catno.jpg"))
				&#123;
					$size = GetImageSize('images/'. $catno. '.jpg');
					if($size&#1111;0] > 0 && $size&#1111;1] > 0)
					&#123;
						print '<img src="images/'. $catno. '.jpg" ';
						print 'width='. $size&#1111;0]/3 .'height = '.$size&#1111;1]/3 . '>';
					&#125;
				&#125;
				else
					print '&nbsp;';
				print '</td>';
			&#125;
			print '<td align="left">';
			print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinyl&#1111;'title'].'</a> - '.$vinyl&#1111;'artist_id'].'</font>';
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price'], 2).'</font>';
			print '</td><td align="center">';
			
			//allowing change of qty
			
			if($change == true)//if change is true then show input box for qty
				echo'<input type="text" name="'.$vinyl&#1111;'cat_no'].'" value="'.$qty.'" size="1">';
			else
				echo $qty;//else just show qty and total price
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price']*$qty, 2).'</font></td></tr>';
			
		&#125;
		
		//display of total row
		
		echo '<tr>
					<th colspan="'. (2+$images) .'" bgcolor="#7b9815">&nbsp;</td>
					<th align="center" bgcolor="#7b9815">'.$_SESSION&#1111;'items'].'</th>
					<th align="center" bgcolor="#7b9815">£'.number_format($_SESSION&#1111;'total_price'], 2).'</th>
			 </tr>';
			
			//offer save feature
			
			if($change == true)
			&#123;
				echo '<tr>
						<td colspan="'. (2+$images) .'"&nbsp;</td>
						<td align="center">
						<input type="hidden" name="save" value="true">
						<input type="hidden" name="cat_no" value="$catno">
						<input type="hidden" name="qty" value="$qty">
						<input type="submit" value="Update Changes" alt="Update Changes">
						</td>
						<td></td>
					 </tr>';
				&#125;
			echo '</form></table>';
	&#125;
	
	function calculate_price($cart)
	&#123;
		// sum total price for all items in shopping cart
		$price = 0.0;
		if(is_array($cart))
		&#123;
			$link_id = db_connect();
			foreach($cart as $catno => $qty)
			&#123;  
				$query = "select price from vinyls where cat_no='".$catno."'";
				$result = mysql_query($query, $link_id);
				if ($result)
				&#123;
					//$row = mysql_fetch_row($result);
					$item_price = mysql_result($result, 0, 'price');
					//$item_price = $row&#1111;'price'];
					
					$price +=$item_price*$qty;
				&#125;
			&#125;
		&#125;
		return $price;
	&#125;
	
	
	function calculate_items($cart)
	&#123;
		//total items in the cart which goes through the cart and adds up the quantities of each item to get the total number og items
		
		$items = 0;
		if(is_array($cart))
		&#123;
			foreach($cart as $catno => $qty)
			&#123;
				$items += $qty;
			&#125;
		&#125;
		return $items;
	&#125;
	
	function get_customer_details($customer_id)
	&#123;
		$link_id = db_connect();
		$query = "select first_name, surname, postcode, address_line_one , address_line_two, city, county, initial  from customers where customer_id = '$customer_id'";
		
		$result = @mysql_query($query, $link_id) or die ("There was a problem with this query.");
		
		if (!$result)
			return false;
		$num_cust = @mysql_num_rows($result);
		if($num_cust == 0)
			return false;
		
		$result = db_result_to_array($result);
		return $result;
	&#125;
	
	function display_checkout_form($c)&#123;	
		//here we create a form where the users current address details in the database are brought up. If these are diffrent then the user
		//can edit them to the required shipping address.
?>

<br />
<table border="0" width="100%" cellspacing="0" cellpadding="0">
	<form  method="post" action="purchase.php" enctype="multipart/form-data">
		<tr><th colspan="2" bgcolor="CCCCCC">Shipping Address (only change if delivering to another address)</th></tr>
		<p></p>
		<tr>
			<td align="left">First Name: </td>
			<td><input name="ship_name" type="text" size="40" maxlength="20" value="<?php echo $c&#1111;'first_name']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Last Name: </td>
			<td><input name="ship_surname" type="text" size="40" maxlength="20" value="<?php print $c&#1111;'surname']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Title: </td>
			<td><input name="ship_initial" type="text" size="40"maxlength="4" value="<?php echo $c&#1111;'initial']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Address Line One: </td>
			<td><input name="ship_address_1" type="text" size="40" maxlength="25" value="<?php echo $c&#1111;'address_line_one']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Address Line Two: </td>
			<td><input name="ship_address_2" type="text" size="40" maxlength="25" value="<?php echo $c&#1111;'address_line_two']; ?>"></td>
		</tr>
		<tr>
			<td align="left">City:  </td>
			<td><input name="ship_city" type="text" size="40" maxlength="30" value="<?php echo $c&#1111;'city']; ?>"></td>
		</tr>
		<tr>
			<td align="left">County:  </td>
			<td><input name="ship_county" type="text" size="40" maxlength="30" value="<?php echo $c&#1111;'county']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Postcode:  </td>
			<td><input name="ship_postcode" type="text" size="40" maxlength="8" value="<?php echo $c&#1111;'postcode']; ?>"></td>
		</tr>
	</form>
</table>

<?
	&#125;
	
	function calculate_shipping_cost($qty)
	&#123;
		
		
		//if the qty of items is greater then the qty number in the database then we have a max charge of £20.00 delivery
		//if it is less then 15 then we check where the $qty of items is the same as qty in the table and retireive the correct
		//postage cost for that item.
		if($qty  > 15)
		&#123;
			$shipping_cost = 20.00;
		&#125;else
		$link_id = db_connect();
		
		//check the database to get the price of delivery depending on the number of items brought.
		$query = 'select * from shipping_costs where qty ="'.$qty.'"';
		$result = mysql_query($query, $link_id);
		$sc = mysql_fetch_array($result);
		
		&#123;
			$shipping_cost = $sc&#1111;'delivery_charge'];
		&#125;
		
		return $shipping_cost;
	&#125;
	
	function display_shipping($shipping_cost)
	&#123;
		//displays the shipping cost in then checkout and payment forms
	
?>
<br />
<table width="600">
	<tr>
		
	    <th align="left">Shipping</th><th align="left"><?php echo number_format($shipping_cost, 2); ?></th>
		<th>Total including shipping</th>
		<th align="right">£<?php echo number_format($shipping_cost+$_SESSION&#1111;'total_price'], 2); ?></th>
	</tr>
</table><br />
<?php
	&#125;
	
	
	
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there appears to be a possible logic error involving the else just before that query. Specifically, lack of braces to block in the query. However, the query may retrieve zero results as well. Make sure to check what query you are sending and what the database is really passing back.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

one problem i have noticed is that it wasn't picking up $qty so it couldn't do the logic. I've sorted that by passing the $SESSION['items'] which is the number of items in the basket. I've also used some braces to lock in that query but still no luck. Any other debugging ideas?Query works fine as expected i echo the statement to see. changed code is

Code: Select all

<?php
	
	
	
	//shopping_basket_fns.php written by Manpreet Sandhu 12/01/2005
	session_start();
	
	require_once('news_fns.php');
	
	function display_cart($cart, $change = true, $images = 1)
	&#123;
		//display what is in the shopping basket
		
		$cart = $_SESSION&#1111;'cart'];
		$qty = $_SESSION&#1111;'items'];
		
		
		echo '<table border="0" width="100%" cellspacing="0">
			<form action="show_cart.php" method="post">
			<tr><th colspan="'. (1+$images) .'" bgcolor="#7b9815">Item</th>
			<th bgcolor="#7b9815">Price</th><th bgcolor="#7b9815">Qty</th>
			<th bgcolor="#7b9815">Total</th></tr>';
			
			//display each item as a table row
			
			foreach ($cart as $catno => $qty)
		&#123;
			$vinyl = get_vinyl_details($catno);
			print '<tr>';
			if($images == true)
			&#123;
				print '<td align="left">';
				if(file_exists("images/$catno.jpg"))
				&#123;
					$size = GetImageSize('images/'. $catno. '.jpg');
					if($size&#1111;0] > 0 && $size&#1111;1] > 0)
					&#123;
						print '<img src="images/'. $catno. '.jpg" ';
						print 'width='. $size&#1111;0]/3 .'height = '.$size&#1111;1]/3 . '>';
					&#125;
				&#125;
				else
					print '&nbsp;';
				print '</td>';
			&#125;
			print '<td align="left">';
			print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinyl&#1111;'title'].'</a> - '.$vinyl&#1111;'artist_id'].'</font>';
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price'], 2).'</font>';
			print '</td><td align="center">';
			
			//allowing change of qty
			
			if($change == true)//if change is true then show input box for qty
				echo'<input type="text" name="'.$vinyl&#1111;'cat_no'].'" value="'.$qty.'" size="1">';
			else
				echo $qty;//else just show qty and total price
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price']*$qty, 2).'</font></td></tr>';
			
		&#125;
		
		//display of total row
		
		echo '<tr>
					<th colspan="'. (2+$images) .'" bgcolor="#7b9815">&nbsp;</td>
					<th align="center" bgcolor="#7b9815">'.$_SESSION&#1111;'items'].'</th>
					<th align="center" bgcolor="#7b9815">£'.number_format($_SESSION&#1111;'total_price'], 2).'</th>
			 </tr>';
			
			//offer save feature
			
			if($change == true)
			&#123;
				echo '<tr>
						<td colspan="'. (2+$images) .'"&nbsp;</td>
						<td align="center">
						<input type="hidden" name="save" value="true">
						<input type="hidden" name="cat_no" value="$catno">
						<input type="hidden" name="qty" value="$qty">
						<input type="submit" value="Update Changes" alt="Update Changes">
						</td>
						<td></td>
					 </tr>';
				&#125;
			echo '</form></table>';
	&#125;
	
	function calculate_price($cart)
	&#123;
		// sum total price for all items in shopping cart
		$price = 0.0;
		if(is_array($cart))
		&#123;
			$link_id = db_connect();
			foreach($cart as $catno => $qty)
			&#123;  
				$query = "select price from vinyls where cat_no='".$catno."'";
				$result = mysql_query($query, $link_id);
				if ($result)
				&#123;
					//$row = mysql_fetch_row($result);
					$item_price = mysql_result($result, 0, 'price');
					//$item_price = $row&#1111;'price'];
					
					$price +=$item_price*$qty;
				&#125;
			&#125;
		&#125;
		return $price;
	&#125;
	
	
	function calculate_items($cart)
	&#123;
		//total items in the cart which goes through the cart and adds up the quantities of each item to get the total number og items
		
		$items = 0;
		if(is_array($cart))
		&#123;
			foreach($cart as $catno => $qty)
			&#123;
				$items += $qty;
			&#125;
		&#125;
		return $items;
	&#125;
	
	function get_customer_details($customer_id)
	&#123;
		$link_id = db_connect();
		$query = "select first_name, surname, postcode, address_line_one , address_line_two, city, county, initial  from customers where customer_id = '$customer_id'";
		
		$result = @mysql_query($query, $link_id) or die ("There was a problem with this query.");
		
		if (!$result)
			return false;
		$num_cust = @mysql_num_rows($result);
		if($num_cust == 0)
			return false;
		
		$result = db_result_to_array($result);
		return $result;
	&#125;
	
	function display_checkout_form($c)&#123;	
		//here we create a form where the users current address details in the database are brought up. If these are diffrent then the user
		//can edit them to the required shipping address.
?>

<br />
<table border="0" width="100%" cellspacing="0" cellpadding="0">
	<form  method="post" action="purchase.php" enctype="multipart/form-data">
		<tr><th colspan="2" bgcolor="CCCCCC">Shipping Address (only change if delivering to another address)</th></tr>
		<p></p>
		<tr>
			<td align="left">First Name: </td>
			<td><input name="ship_name" type="text" size="40" maxlength="20" value="<?php echo $c&#1111;'first_name']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Last Name: </td>
			<td><input name="ship_surname" type="text" size="40" maxlength="20" value="<?php print $c&#1111;'surname']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Title: </td>
			<td><input name="ship_initial" type="text" size="40"maxlength="4" value="<?php echo $c&#1111;'initial']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Address Line One: </td>
			<td><input name="ship_address_1" type="text" size="40" maxlength="25" value="<?php echo $c&#1111;'address_line_one']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Address Line Two: </td>
			<td><input name="ship_address_2" type="text" size="40" maxlength="25" value="<?php echo $c&#1111;'address_line_two']; ?>"></td>
		</tr>
		<tr>
			<td align="left">City:  </td>
			<td><input name="ship_city" type="text" size="40" maxlength="30" value="<?php echo $c&#1111;'city']; ?>"></td>
		</tr>
		<tr>
			<td align="left">County:  </td>
			<td><input name="ship_county" type="text" size="40" maxlength="30" value="<?php echo $c&#1111;'county']; ?>"></td>
		</tr>
		<tr>
			<td align="left">Postcode:  </td>
			<td><input name="ship_postcode" type="text" size="40" maxlength="8" value="<?php echo $c&#1111;'postcode']; ?>"></td>
		</tr>
	</form>
</table>

<?
	&#125;
	
	function calculate_shipping_cost($items)
	&#123;
		
		//$qty = $_GET&#1111;'qty'];
		
				
		//if the qty of items is greater then the qty number in the database then we have a max charge of £20.00 delivery
		//if it is less then 15 then we check where the $qty of items is the same as qty in the table and retireive the correct
		//postage cost for that item.
		if($items > 15)
		&#123;
			$shipping_cost = 20.00;
		&#125;else
		&#123;
			if($items <= 15)
			&#123;
				$link_id = db_connect();
				
				//check the database to get the price of delivery depending on the number of items brought.
				$query = 'select * from shipping_costs where qty ="'.$items.'"';
				echo $query;
				$result = mysql_query($query, $link_id);
				$sc = mysql_fetch_array($result);
				
				&#123;
					$shipping_cost = $sc&#1111;'delivery_charge'];
				&#125;
			&#125;
		&#125;
		
		return $shipping_cost;
		
	&#125;
	
	function display_shipping($shipping_cost)
	&#123;
		//displays the shipping cost in then checkout and payment forms
		
?>
<br />
<table width="600">
	<tr>
		
		<th align="left">Shipping</th><th align="left"><?php echo number_format($shipping_cost, 2); ?></th>
		<th><?php echo $qty; ?>Total including shipping</th>
		<th align="right">£<?php echo number_format($shipping_cost+$_SESSION&#1111;'total_price'], 2); ?></th>
	</tr>
</table><br />
<?php
	&#125;
	
	
	
	
?>

Code: Select all

<?php
	
	session_start(); //must start a session for cart
	
	//$qty = $_GET&#1111;'qty'];
	$items = $_SESSION&#1111;'items'];
	
	
	//checkout.php written by Manpreet Sandhu 26/01/2005
	require('page.inc'); //page class	
	require_once('shopping_basket_fns.php');
	
	$checkout = new Page();//create new page object
	
	$checkout -> Display();//use function display from page class
	
	display_main_menu(); //display side bar menu
	
	
	$link_id = db_Connect();//connect the database
	
	//query the database to retrieve all information out of the database regarding the current customer using the 
	//registered session variable which is their customer id.
	
	$query = mysql_query("SELECT * FROM customers WHERE customer_id='".$_SESSION&#1111;'customer_id']."'",$link_id); 
	$c = mysql_fetch_array($query);//fetch the results of the query and for them in an array and assign the value $c
	//to pass through to the display_checkout_form function.
	//echo $qty;
	//$items
	print '<table width="600">';
	print '<center><th align="center" colspan="6">';
	print "Checkout";
	print '</th></center>';
	print '<tr>';
	print '<td>';
	print "&#1111;<a href='show_cart.php'>Back</a>]";
	print "&#1111;<a href='vinyls.php'>Search for more vinyls</a>]";
	print '</td>';
	print '</tr>';
	print '<table>';
	print '<p></p>';
	
	if($_SESSION&#1111;'cart']&&array_count_values($_SESSION&#1111;'cart']))
	&#123;//if session values exist then display cart with the items and the checkout form else echo message
		display_cart($_SESSION&#1111;'cart'], false, 0);
		display_shipping(calculate_shipping_cost($items));
		display_checkout_form($c);
		
	&#125;
	else 
		
		print 'There are no items in your cart';
	
	do_html_footer();
	
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Sorry for not giving you concrete help, but I simply don't want to read through 300 lines of PHP mixed with HTML.

You would make your life much simpler (and your programs much less error-prone), if you thought about seperating logic and display. Having functions return HTML beats the purpose of using functions in all but one respect: the one project you're designing your functions for. Next project, different functions. Why not move more towards creating an application rather than a generating HTML pages? It will help you very much in the future. Maybe even start reading up on object oriented programming - which at first doesn't look different to using functions at all. Or maybe think about using a templating engine as well.

It would make life easier for everyone :)
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

I believe the problem is because you didn't make a mysql_query

You're suppose to do.

$query = mysql_query($query, $link_id);

//Then

$query = mysql_fetch_array($query);

Edit: Oops, didn't read that part where you passed the result.

Maybe if you change from single quotes to double quotes you get better results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

folks... the issue was resolved via IM. Consider this closed.

For anyone curious, the query was buggy.
Post Reply