Forum Moderators: coopster
<?php
if ($totalCost>=2.00 && $totalCost<=4.99){
echo ("£2.50");}
if ($totalCost>=5.00 && $totalCost<=9.99){
echo ("£3.50");}
if ($totalCost>=10.00 && $totalCost<=44.99){
echo ("£4.95");}
if ($totalCost>45.00){
echo ("Free");}
?>
So I have the $totalCost of all the products
I have the delivery charges from above based on $totalCost.
But now I need to get my $orderValue
I don't know how to put the above If statment into the var $delivery so I can do somthing like...
$orderValue = $totalCost + $delivery;
As you can see this is for a shopping cart
Thanks Guys.
Ski442
<?php
if($totalCost>=2.00 && $totalCost<=4.99){
echo("£2.50");
$orderValue = $totalCost + 2.50;
}
elseif. . .
elseif. . .
elseif. . .
But I would probably go a step further and create an array with your levels of delivery charges at the beginning of the code, that way you can change the delivery prices in one place. . .
e.g.
$delivery[0] = 2.50;
$delivery[1] = 3.50;
or use the array function to set the values in your array.
<?php
header("content-type:text/html");
if ($_POST['subtotal'] and ($_POST['subtotal'] > 0)) {
$subtotal = $_POST['subtotal']; // Not really a total, is it? :-)
// Might even want to put these at the top, outside this block
// so you can dsplay them on the order form. Better yet,
// they can be accessed as database values,
// editable from the cart admin area.
$priceRanges = Array(
Array (2.00,4.99),
Array (5.00,9.99),
Array (10.00,44.99)
);
$shipCosts = Array(2.50,3.50,4.95);
$free_shipping = 45.00;
$shipping=NULL;
if ($subtotal > $free_shipping) { $shipping = 0.00; }
else {
for ($i = 0; $i < count($priceRanges); $i++) {
if (($subtotal >= $priceRanges[$i][0]) and ($subtotal <= $priceRanges[$i][1])) {
$shipping = $shipCosts[$i];
}
if ($shipping) { break; }
}
}
$total = $subtotal+$shipping;
echo '
<p><strong>Subtotal:</strong> £' . sprintf("%.2f",$subtotal) . '</p>
<p><strong>Shipping:</strong> £' . sprintf("%.2f",$shipping) . '</p>
<p><strong>Total:</strong> £' . sprintf("%.2f",$total) . '</p>
';
}
else {
$form = '
<form method="post" action="total.php">
<p>Enter a subtotal.</p>
<p>£<input type="text" size="6" name="subtotal" id="subtotal" value=""></p>
<p><input type="submit" value="Total It">
</form>
';
echo $form;
}
?>
// Increment the total cost of all items
$price=$row['price'];
$onoffer=$row['onoffer'];
$final_price = ($onoffer > 0)?$onoffer:$price;
$subtotal += $row["qty"] * $final_price;
?>
<tr>
<td width="110" height="20" align="center">
<font face="verdana" size="1" 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 align="center" width="70" height="50" align="center">
<a href="<? echo $SITEscripturl ?>/tvtesting.php?id=<? echo $row["stockcode"] ?>"/><img src="<? echo $SITEscripturl ?>/pictures/<? echo $row["stockcode"] ?>_nail.jpg" height="50" width="50" alt="<? echo $row["title"]?>"/> </a></td>
<td align="center" width="511" height="20">
<font face="verdana" size="1" color="black">
<?php echo $row["stockcode"]?> <?php echo $row["title"]; ?></font></td>
<td width="145" height="20" align="center">
<font face="verdana" size="1" color="black">
<?php if ($row["onoffer"]) {echo $row["onoffer"]; }else{ ?>
£<?php echo number_format($row["price"], 2, ".", ",");} ?></font></td>
<td width="90" height="20" align="center">
<font face="verdana" size="1" color="black">
<a href="tvcart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a></font></td>
</tr>
<?php $Sstockcode = $row["stockcode"] ?>
<?php }?>
<tr>
<td ></td>
</tr>
<tr>
<td>
</td>
<td></td>
<td>
</td>
<td></td>
</tr>
</table>
<hr />
<table width="806">
<tr>
<td width="343" align="center"> </td>
<td width="303" align="left"><?php $priceRanges = Array(
Array (0.01,4.99),
Array (5.00,9.99),
Array (10.00,44.99)
);
$shipCosts = Array(2.50,3.50,4.95);
$free_shipping = 45.00; $shipping=NULL;
if ($subtotal > $free_shipping) { $shipping = 0.00; }
else {
for ($i = 0; $i < count($priceRanges); $i++) {
if (($subtotal >= $priceRanges[$i][0]) and ($subtotal <= $priceRanges[$i][1])) {
$shipping = $shipCosts[$i];
}
if ($shipping) { break; }
}
} $total = $subtotal+$shipping;
?><strong>Goods Total</strong> <font size="-1" color="#666666">(inc vat)</font></td>
<td align="right" width="144">£<? echo sprintf("%.2f",$subtotal) ?> </td>
</tr>
<tr>
<td> </td>
<td align="left"><strong>Delivery Charge</strong><font size="-1" color="#666666"> (inc vat) Mainland UK</font></td>
<td align="right">£<? echo sprintf("%.2f",$shipping) ?></td>
</tr>
<tr>
<td> </td>
<td><font size="+1">Total Amount Of Your Order</font> <font size="-1" color="#666666">(inc vat)</font> </td>
<td align="right"><font size="+1">£<? echo sprintf("%.2f",$total) ?></font></td>
</tr>
<tr>
<td></td>
<td> </td>
<td></td>
</tr>
</table>
Is this script OK. Sorry about all the <td>'s I have tested on firefox, IE and safari and render as it should, do I need to do any more testing?
Thanks for all your help
Ski442
Is this script OK.
Does it work? Then it's "ok." However there is much to be learned in terms of security and ease of maintenance. Don't have time today for a "full review" but I'll knock a couple off for you.
As your scripts get larger and larger, you will find you waste a lot of time "mining." This is my pet name for digging through 1500 lines of spaghetti code looking for a single value to change. For this reason, you will often see something like
require_once('config.php');
in which all variables for the program are either extracted from a database or set all in one place, so you can find them easily. An alternate method is to put all your configuration variables at the top. So move this
$priceRanges = Array.....$shipping=NULL;
to the top.
The second thing is cleansing your input. Sure, none of your customers will do anything nasty, but it's not them you have to worry about. Second, any time you allow a customer to make an error, they will. So you need to validate input and make sure it's not something they can manipulate. Although this particular script doesn't show any input, you have
<a href="tvcart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">
In tvcart.php, for example, you want to make sure $_GET['id'] is what you expect. If I can send this
tvcart.php?action=remove_item&id=1%20or%201=1
It would cause this select to be issued,
delete from table where id=1 or 1=1
And as you know, 1=1 is always true . . . so it deletes all items.
A simple fix is in that script,
if ($_GET['id'] > 0) {
//ok, do it
}
else { die("something fishy"); }
Many use is_numeric() here, but zero is also numeric. And zero will never be a database record ID. :-)
Last comment is non-PHP but important. It's something you may want to address before you get too far along.
I have tested on firefox, IE and safari and render as it should
But if you run the output of this script through the validator [validator.w3.org], it will fail miserably. For starters, <font>, align= and background= are all deprecated. Why is validation important?
If you validate, it runs in Standards Compliance mode; if not, it triggers Quirks mode. The problem with this is browsers will render oddly in Quirks mode, causing you to add all sorts of "hacks" to get them to play nicely. If you always validate, it will give you tons fewer problems in this regard and move much closer to cross browser compatibility - even with table layout. Too many programmers pass this off as "not my problem - I'm a programmer, not a designer." But we're creating code for page output, it's up to us to make sure it can be validated.
Choosing the best doctype for your site [webmasterworld.com]
WebmasterWorld Search: Standards Compliance Mode [google.com]
Easy fixes: where you have <font>, you can apply a class selector to the entire td's or a <span>. All your backgrounds can be pulled out and applied via CSS. For align="left", left is the default so you can just removed these, then create a right-aligning selector for the right-aligned cells:
<td class="right-align">
....
.right-align { text-align: right; }
(To many, the class selector name "right-align" would be considered a bad one as it's name defines the presentation, not the function of the selector in the document, but this will at least validate . . . )
This is the only page in my site that has a table layout, it's my first as i was finding it hard to postion my <div>'s when the user would add items to the cart, so needed most of the items in the code that you have virtually wrote (php anyway) to move down the page. This page is live but in my testing section still.
I have been doing some digging on table layout, and seems to me that these are hard to maintain and with the comments you have made i will be changing it when i have more input on virtical liquid layout.
I have ran all my other pages through w3 validate and would have found the errors that your very keen eye can see already, as i to am very keen on building compliant code. Unfortunatly because i can't write (or spell) code off the top of my head much yet, i am having to take bits of scripts from around the web and put them together and try to rewrite the best i can. I did find about quirks mode after i built my site and had to rebuild a lot of pages, my fault lack of research.
Safety and security.
I have an unanswered post here at WM
[webmasterworld.com...] on security about mysql real escape and sql injection. This is another section i need to learn, oh, how my head hurts.
Once again thanks very much for your time
All of your comments and code have been of great use to me.
ski442