[SOLVED] Caculating Shipping Costs??
Posted: Wed Jan 26, 2005 2:16 pm
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
Not sure why must be doing something wrong with the sql??Any one help?
code is below:
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 206code is below:
Code: Select all
<?php
session_start(); //must start a session for cart
$qty = $_GETї'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ї'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 "ї<a href='show_cart.php'>Back</a>]";
print "ї<a href='vinyls.php'>Search for more vinyls</a>]";
print '</td>';
print '</tr>';
print '<table>';
print '<p></p>';
if($_SESSIONї'cart']&&array_count_values($_SESSIONї'cart']))
{//if session values exist then display cart with the items and the checkout form else echo message
display_cart($_SESSIONї'cart'], false, 0);
display_shipping(calculate_shipping_cost($qty));
display_checkout_form($c);
}
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)
{
//display what is in the shopping basket
$cart = $_SESSIONї'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)
{
$vinyl = get_vinyl_details($catno);
print '<tr>';
if($images == true)
{
print '<td align="left">';
if(file_exists("images/$catno.jpg"))
{
$size = GetImageSize('images/'. $catno. '.jpg');
if($sizeї0] > 0 && $sizeї1] > 0)
{
print '<img src="images/'. $catno. '.jpg" ';
print 'width='. $sizeї0]/3 .'height = '.$sizeї1]/3 . '>';
}
}
else
print ' ';
print '</td>';
}
print '<td align="left">';
print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinylї'title'].'</a> - '.$vinylї'artist_id'].'</font>';
print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinylї'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ї'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ї'price']*$qty, 2).'</font></td></tr>';
}
//display of total row
echo '<tr>
<th colspan="'. (2+$images) .'" bgcolor="#7b9815"> </td>
<th align="center" bgcolor="#7b9815">'.$_SESSIONї'items'].'</th>
<th align="center" bgcolor="#7b9815">£'.number_format($_SESSIONї'total_price'], 2).'</th>
</tr>';
//offer save feature
if($change == true)
{
echo '<tr>
<td colspan="'. (2+$images) .'" </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>';
}
echo '</form></table>';
}
function calculate_price($cart)
{
// sum total price for all items in shopping cart
$price = 0.0;
if(is_array($cart))
{
$link_id = db_connect();
foreach($cart as $catno => $qty)
{
$query = "select price from vinyls where cat_no='".$catno."'";
$result = mysql_query($query, $link_id);
if ($result)
{
//$row = mysql_fetch_row($result);
$item_price = mysql_result($result, 0, 'price');
//$item_price = $rowї'price'];
$price +=$item_price*$qty;
}
}
}
return $price;
}
function calculate_items($cart)
{
//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))
{
foreach($cart as $catno => $qty)
{
$items += $qty;
}
}
return $items;
}
function get_customer_details($customer_id)
{
$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;
}
function display_checkout_form($c){
//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ї'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ї'surname']; ?>"></td>
</tr>
<tr>
<td align="left">Title: </td>
<td><input name="ship_initial" type="text" size="40"maxlength="4" value="<?php echo $cї'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ї'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ї'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ї'city']; ?>"></td>
</tr>
<tr>
<td align="left">County: </td>
<td><input name="ship_county" type="text" size="40" maxlength="30" value="<?php echo $cї'county']; ?>"></td>
</tr>
<tr>
<td align="left">Postcode: </td>
<td><input name="ship_postcode" type="text" size="40" maxlength="8" value="<?php echo $cї'postcode']; ?>"></td>
</tr>
</form>
</table>
<?
}
function calculate_shipping_cost($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($qty > 15)
{
$shipping_cost = 20.00;
}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);
{
$shipping_cost = $scї'delivery_charge'];
}
return $shipping_cost;
}
function display_shipping($shipping_cost)
{
//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ї'total_price'], 2); ?></th>
</tr>
</table><br />
<?php
}
?>