need help figuring out shipping information

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

mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

need help figuring out shipping information

Post by mikewooten »

i am working on this ecommerce website and i am trying to figure out how to add the shipping information to the total.
can someone please help me to get this to work? thanks.
here is an example of what i'm trying to accomplish:
For example, if I buy three guitars, I pay $15 for shipping but if I buy two guitars, I pay $10 for shipping ($5 per item).
here's my website that i'm working on, i'm working on the shopping cart section, if you add a product to the cart, it'll take you to the cart page.

http://www.wootenmedia.com/wootenmusic8/

here's the code that i have that i'm working with:

Code: Select all

<?php

include("db.php");
	switch($_GET["action"])
	{
		case "add_item":
		{
			AddItem($_GET["id"], $_GET["qty"], $_GET["catid"], $_GET["proditems"]);
			ShowCart();
			break;
		}
		case "update_item":
		{
			UpdateItem($_GET["id"], $_GET["qty"], $_GET["catid"]);
			ShowCart();
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["id"], $_GET["catid"]);
			ShowCart();
			break;
		}
		case "delete_items":
		{
			DeleteItems($_GET["id"],  $_GET["qty"], $_GET["catid"]);
			ShowCart();
			break;
		}
		default:
		{
			ShowCart();
		}
	} 
	function AddItem($itemId, $qty, $catid)
	{
		global $dbServer, $dbUser, $dbPass, $dbName;
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId =' $itemId' and catid = '$catid'");
		$row = mysql_fetch_row($result);
		$numRows = $row[0];		
		if($numRows == 0)
		{
@mysql_query("insert into cart(cookieId, itemId, qty, catid) values('" . GetCartId() . "', $itemId, $qty, $catid)");
		}
		else
		{
			UpdateItem($itemId, $qty, $catid);
		}
	}   
    	function UpdateItem($itemId, $qty)
	{
		global $dbServer, $dbUser, $dbPass, $dbName;
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); 
		if($qty == 0)
		{
			RemoveItem($itemId);
		}
		else
		{
			mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
		}
	}
	
	function RemoveItem($itemId)
	{
		global $dbServer, $dbUser, $dbPass, $dbName;
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);  
		mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
	}
	function DeleteItems($itemId, $qty, $catid)
	{
		global $dbServer, $dbUser, $dbPass, $dbName;
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
        mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "'");
	}
	function ShowCart()
	{
	global $dbServer, $dbUser, $dbPass, $dbName;
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);		
		$totalCost = 0;
		$shipping = "3.49";
		$ship = "5.00";
		$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
?>
<form name="frmCart" method="get">
		<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="7%" height="25" bgcolor="#50535C">
					<font face="Arial" size="2" color="white">
						&nbsp;&nbsp;<strong>Qty</strong>
					</font>
				</td>         
                
                <td width="10%" height="25" bgcolor="#50535C">
					<font face="Arial" size="2" color="white">
						<strong>Products</strong>
					</font>
				</td>     
                
				<td width="10%" height="25" bgcolor="#50535C">
					<font face="Arial" size="2" color="white">
						<strong>Product</strong>
					</font>
				</td>     
				<td width="10%" height="25" bgcolor="#50535C">
					<font face="Arial" size="2" color="white">
						<strong>Price Each</strong>
					</font>
				</td>    
                
                <td width="10%" bgcolor="#50535C">
                <font face="Arial" size="2" color="white">
                <strong>Total</strong>  
                </font>
                </td>
                
				<td width="10%" height="25" bgcolor="#50535C">
					<font face="Arial" size="2" color="white">
						<strong>Remove?</strong>
					</font>
				</td>
			</tr>
			<?php
			
			while($row = mysql_fetch_array($result))
			{
				// Increment the total cost of all items
				$totalCost += ($row["qty"] * $row["itemPrice"]);
				?>
					<tr>
						<td width="7%" height="25" valign="top">
							<font size="2" color="black">
								<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
								<?php
								
									for($i = 1; $i <= 20; $i++)
									{
										echo "<option ";
										if($row["qty"] == $i)
										{
											echo " SELECTED ";
										}
										echo ">" . $i . "</option>";
									}
								?>
								</select>
							</font>                              
						</td> 
                        
                        <td width="10%" height="25" valign="top">
							<font size="2" color="black">
								<?php echo $row["prodItems"]; ?>
							</font>                            
						</td>  
                        
						<td width="10%" height="25" valign="top">
							<font size="2" color="black">
								<?php echo $row["itemName"]; ?>
							</font>                             
						</td>  
                       
						
            <td width="10%" height="25" valign="top"> <font size="2" color="black"> 
            $<?php echo number_format($row["itemPrice"], 2, ".", ","); ?></font> 
              <br>
							
						</td>
						
            <td width="10%" height="25" valign="top">&nbsp;</td>
              <td width="10%" height="25" valign="top">
               <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>"><img src="imglinks/remove.jpg" width="51" height="13" border="0"></a>
              </td>
					</tr>
				<?php
			}
			
			// Display the total
			?>    
            
            <tr>
            <td>&nbsp;</td>
             
            
            <td colspan="3" valign="top" width="10%" align="right"><font size="2">Shipping 
              Cost is $3.49 added to Total Amount</font>&nbsp;&nbsp;&nbsp;<br>

</td>
            <td colspan="2">
            <font size="2" color="black">       
								<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
							</font><br>
							<? 
							echo "Total + Shipping = <br>$$total";
							echo "<br><br>";
							//$ship2 = $row["itemPrice"];
							//$ship3 = $ship2 + $ship;
							//echo "$ship3";
							//$1 = number_format($totalCost, 2, ".", ",");
							//$3 = $1 + $ship;
							//echo "$3";
							echo $row["qty"];
							echo $row["itemPrice"];
							echo "<br><br>";
							$totalCost2 = $row["qty"] * $row["itemPrice"];
							echo "totalcost2 is $totalCost2 <br><br>";
							$sh = $totalCost2 + $ship;
							echo "NEWEST $sh <br><br>"; 
							
							
							
							$ship2 = $totalCost + $ship;
 							echo "new shipping is $5.00 per item <br><br> New shipping is $ship2";
							echo "<br><br>";
							?>
							<b>NEW Total: $<?php echo number_format($ship2, 2, ".", ","); ?></b>
							<br /><br />
            </td>
            </tr>
			<tr>
				<td width="100%" colspan="6">
							<hr size="1" color="#1B1987" NOSHADE>
				</td>
					</tr>
					<!--<tr>
						<td width="50%" colspan="6">&nbsp;</td>						
						<tr>-->
						
<td colspan="6" align="left">
<a href="index.php"><img src="images/keepshop.jpg" width="150" height="20" alt="" border="0"></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="login.php"><img src="images/checkout.jpg" width="110" height="20" alt="" border="0"></a>						
<br />
</td>
</tr>
<tr>

						<td width="50%" colspan="6" align="left">
<a href="cart.php?action=delete_items&id=<?php echo $row["itemID"]; ?>"><img src="images/removeall2.jpg" width="160" height="20" alt="" border="0"></a><br /><br />	
						</td> 
					</tr>
				</table>
				</form>
cgroup
Forum Newbie
Posts: 13
Joined: Tue Jun 03, 2003 11:51 pm
Location: PA

Post by cgroup »

Not sure about the rates of shipping but you could just multiply it. If each guitar is $5 then take the quantity times five.

Code: Select all

<?php
$quantity = 3;
$rate = 5;

$shipping = $quantity*$rate;

echo "$".$shipping;
?>
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

that is what i have tried before, but when i do it that way, then the shipping comes up the same every time, how can i get the shipping to change when i add products to the cart and also when i add a higher quantity to each item within the cart using the drop down?

here's my website that i'm working on: http://www.wootenmedia.com/wootenmusic8/
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mike, you really need to learn how bbtags work, so you can post correctly.
that is what i have tried before, but when i do it that way, then the shipping comes up the same every time, how can i get the shipping to change when i add products to the cart and also when i add a higher quantity to each item within the cart using the drop down?
each should have a shipping charge amount attached to them in the database or otherwise. You are given the quantity. Just multiply the two, and save the results for output later.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

so i would have to create a shipping column in the items table in the database, then multilply the shipping and product price, likethe example above?
thanks
sorry i posted incorrectly before, i looked at bbcode and now know how to use it for the next time.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

?

Post by mikewooten »

can anyone help me with this?
so i would have to create a shipping column in the items table in the database, then multilply the shipping and product price, likethe example above?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you even try it?
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

how would i multiply the shipping price and item price?
would i use the example above or would i have some example like this?

Code: Select all

$shipping = $row["shipping"] * $row["itemPrice"];
echo "$shipping";
i tried to multiply like the example above and got the result as 0.
how would i multiply the two?
or
would i need to use mysql_query to display the results?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you probably need to convert the results you are using there to numbers instead of numeric string representations.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i have the shipping charge attached to each of the products in the database.
if I used the example code above, how would i get it to change every time a product was added to the cart or changed the quantity from the drop down menu?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basic math..

total is the sum of all product prices plus their shipping cost, multiplied by the quantity requested.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

would it be something like this?

Code: Select all

$total = $totalCost + $row["shipping"] * $row["qty"];
or would it be something like this?

Code: Select all

$shipping = 5
$qty = 3
$total = $totalCost + $shipping * $qty;
if i used that example, then the shipping would always be the same. how would i get the shipping to change?
i'm trying to use the basic math, but i think i'm still confused on how the shipping would change or how the calulation would be.
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

does anyone know how to calulate the shipping?
the code is the same as above and i am also using

Code: Select all

$totalCost = 0;
$ship = 5;
$i = 5;
$total = number_format($totalCost, 0, ".", ",");
$sum = $total + $ship * $i;
echo number_format($sum, 2, ".", ",");
if I have $i as 2 and i have two products in the cart with a price of 500 and i am getting a quantity of 1 each, i am getting close to the shipping price that i want, but when i choose 1, then choose 2 from the dropdown, the total quantity won't change? What do i need to do for this to work?
can anyone help?
thanks
what would i need to do to have the shipping value of 5 everytime the price is updated?
how would i add shipping to the total?
i have a shipping column in the database with $5 for each of the products

thanks
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

can anyone help me with this?
to muliply quantity, would i have to multiply $qty or $i?
would the calculation be:

Code: Select all

<?
$sum = $totalCost + $ship * $i;
echo "sum is $sum";
 or 
$sum = $totalCost + $ship * $qty;
?>
thanks
Post Reply