|
 |
|
 |
08-30-2005, 01:55 PM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
|
Beware the multiple option shopping cart . . .
Some extra eyes needed on this . . .
I'm writing my first shopping cart, and using a table in mysql that will store the orders until they are finalized.
I have two kinds of products to deal with -- one is the standard, no variables kind of product, and that's been easy.
I decided to have it run a different block of code if it's the second kind of product, however -- in this type there is a price break based on the quantity of that item that you order . . . AND it has variables. So, you could order a blanket (part number 0001), and get a price break at 12 . . . but if you order 6 blue ones, then go back and add another 6 PINK blankets, the price needs to be lessened for the existing item and the new one.
I came up with a way of storing the different price rules and amounts, and that part works fine. Additionally, you can add items with the same color and part number, and the price breaks correctly.
When you add a like item with a DIFFERENT color, however, my code gets wacky. I'm not sure if this is too much to post here (I've edited out the way it determines the price amount), but I've got a feeling it may just be some stupid logical PHP grammar error I'm missing and just can't see. Any comments at all would be appreciated.
PHP Code:
// pull all temp_orders with same session and item number; get total of existing items with same item_number
$total_exis_items=0;
$query_pre = "SELECT item_number,quantity,sessionid,variableone,variabl etwo FROM temp_orders WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
$result_pre = mysql_db_query($dbname, $query_pre);
while ($row_pre = mysql_fetch_array($result_pre)) {
$total_exis_items=($row_pre['quantity'] + $total_exis_items);
}
// check for no items matching (therefore new item entered into the order)
if ($total_exis_items == 0) {
if ($cust_quantity <= $base_amount)
======EXTERNAL FUNCTION THAT GIVES ME $cust_orderprice========
$query5="INSERT temp_orders SET item_name='$cust_item_name', quantity='$cust_quantity', item_number='$cust_item_number', orderprice='$cust_orderprice', variableone='$cust_variableone', variabletwo='$cust_variabletwo', pricetype='$cust_pricetype', sessionid='$cust_session'";
mysql_query($query5) or die(mysql_error());
echo("<meta http-equiv=\"refresh\" content=\"0;URL=../cart/display_cart.php\">");
} else {
$query_ninety = "SELECT item_number,quantity,sessionid,variableone,variabl etwo FROM temp_orders WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
$result_ninety = mysql_db_query($dbname, $query_ninety);
while ($row_ninety=mysql_fetch_array($result_ninety)) {
$ravvarone=$row_ninety['variableone'];
$ravvartwo=$row_ninety['variabletwo'];
// check for items matching with same variables
if ($ravvarone = $cust_variableone && $ravvartwo = $cust_variabletwo) {
$final_exis_items=($total_exis_items + $cust_quantity);
======EXTERNAL FUNCTION THAT GIVES ME $cust_orderprice========
$updated_quantity=($row_ninety['quantity'] + $cust_quantity);
$query6="UPDATE temp_orders SET orderprice='$cust_orderprice', quantity='$updated_quantity' WHERE sessionid='$cust_session' AND item_number='$ravvarone' AND variableone='$ravvartwo' AND variabletwo='$cust_variabletwo'";
mysql_query($query6) or die(mysql_error());
$querysix="UPDATE temp_orders SET orderprice='$cust_orderprice' WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
mysql_query($querysix) or die(mysql_error());
echo("<meta http-equiv=\"refresh\" content=\"0;URL=../cart/display_cart.php\">");
}
else {
$final_exis_items=($total_exis_items + $cust_quantity);
======EXTERNAL FUNCTION THAT GIVES ME $new_orderprice========
$query5="INSERT temp_orders SET item_name='$cust_item_name', quantity='$cust_quantity', item_number='$cust_item_number', orderprice='$new_orderprice', variableone='$ravvarone', variabletwo='$ravvartwo', pricetype='$cust_pricetype', sessionid='$cust_session'";
mysql_query($query5) or die(mysql_error());
$queryextra3="UPDATE temp_orders SET orderprice='$new_orderprice' WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
mysql_query($queryextra3) or die(mysql_error());
echo("<meta http-equiv=\"refresh\" content=\"0;URL=../cart/display_cart.php\">");
}
}
}
And don't laugh at my variable names, or I'll cry like a little girl.
Last edited by redhead; 08-30-2005 at 02:12 PM.
Reason: Indentation fixed to an extend
|
|
|
08-30-2005, 02:06 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
wow, your code is almost as hard to read as the solid block paragraph post above it  .. line spaces and indentations are your friend .. especially when you want someone else to look at your code.
Quote:
|
When you add a like item with a DIFFERENT color, however, my code gets wacky.
|
it would help if you can elaborate on 'wacky' as well. we don't have the db setup to really test your code.
__________________
Mike
|
|
|
08-30-2005, 02:18 PM
|
#3 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
ack, .. i was gonna try to look at it but your statements are not complete. i'm missing a few '}' and if i start adding things in, i'm just making guesses that this is what you are trying to do.
i'd highly recommend spending a little more time narrowing the problem down to a smaller section of code, and try to ask a more specific quesiton. also explain what is your code doing and what should your code be doing as detailed as possible.
__________________
Mike
|
|
|
08-30-2005, 02:23 PM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,710
|
What is this ???
PHP Code:
..
if ($ravvarone = $cust_variableone && $ravvartwo = $cust_variabletwo) {
...
This will result in a true statement no matter what, assigning a variable some value will allways result in the value beeing assigned to the variable, in this case, unless $cust_variableone or $cust_variabletwo holds a value of 0, this will allways be true..
Shouldn't there be a == operator in use here ???
|
|
|
08-30-2005, 02:29 PM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,710
|
Quote:
|
ack, .. i was gonna try to look at it but your statements are not complete. i'm missing a few '}'
|
On the contrary, it's just the way his if()/else clauses is started that confuses the eye, in order to figure out the indentation.
But yes, I have a suggestion, keep a nice indentation/naming scheme/structure which makes your code easy to read, or atleast easier to understand for the reader.
Since you describe a wacky performance, a description of this would be helpfull, or atleast perhaps a test-bed where we can see what exactly is happening..
Quote:
|
Originally Posted by metazai
(I've edited out the way it determines the price amount)
|
Hmm.. unless this isn't exactly where it goes wrong I would like to see what that part does, it could be a previus error in that, which results in an error further down the code path.
|
|
|
08-30-2005, 02:41 PM
|
#6 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
|
oops . . .
You are absolutely right, and I apologize . . . I should know better.
Hopefully it's a little clearer below. The problem I have is this -- sending an item through with two descriptive variables (such as size and color), the item being sent survives the first check for no items matching part, session, or either variable, and if there are none, it executes perfectly.
PHP Code:
// pull all temp_orders with same session and item number; get total of existing items with same item_number; set case
$total_exis_items = 0;
$query_pre =
"SELECT item_number,quantity,sessionid,variableone,variabletwo FROM temp_orders WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
$result_pre = mysql_db_query($dbname, $query_pre);
while ($row_pre = mysql_fetch_array($result_pre))
{
$total_exis_items = ($row_pre['quantity'] + $total_exis_items);
}
// check for no items matching (therefore new item entered into the order)
if ($total_exis_items == 0)
======EXTERNAL FUNCTION THAT GIVES ME $cust_orderprice========
{
$query5 =
"INSERT temp_orders SET item_name='$cust_item_name', quantity='$cust_quantity', item_number='$cust_item_number', orderprice='$cust_orderprice', variableone='$cust_variableone', variabletwo='$cust_variabletwo', pricetype='$cust_pricetype', sessionid='$cust_session'";
mysql_query($query5)or die(mysql_error());
echo(
"<meta http-equiv=\"refresh\" content=\"0;URL=../cart/display_cart.php\">");
}
Next, if the items match part, session AND BOTH variables, it again executes perfectly, adding the items to the existing quantity and adjusting the price accordingly:
PHP Code:
else
// pull items from temp_orders again for use on the next two checks
{
$query_ninety =
"SELECT item_number,quantity,sessionid,variableone,variabletwo FROM temp_orders WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
$result_ninety = mysql_db_query($dbname, $query_ninety);
while ($row_ninety = mysql_fetch_array($result_ninety))
{
$ravvarone = $row_ninety['variableone'];
$ravvartwo = $row_ninety['variabletwo'];
// check for matching items with both variables matching, if true, insert and adjust all prices that match the product number
if ($ravvarone = $cust_variableone && $ravvartwo = $cust_variabletwo)
{
======EXTERNAL FUNCTION THAT GIVES ME $cust_orderprice========
$final_exis_items = ($total_exis_items + $cust_quantity);
$updated_quantity = ($row_ninety['quantity'] + $cust_quantity);
$query6 =
"UPDATE temp_orders SET orderprice='$cust_orderprice', quantity='$updated_quantity' WHERE sessionid='$cust_session' AND item_number='$ravvarone' AND variableone='$ravvartwo' AND variabletwo='$cust_variabletwo'";
mysql_query($query6)or die(mysql_error());
$querysix =
"UPDATE temp_orders SET orderprice='$cust_orderprice' WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
mysql_query($querysix)or die(mysql_error());
echo(
"<meta http-equiv=\"refresh\" content=\"0;URL=../cart/display_cart.php\">");
}
Lastly, here is where it screws up. It never seems to even look at this code, preferring to run the last chunk even when $ravvarone does not equal $cust_variableone and/or $ravvartwo does not equal $cust_variabletwo):
PHP Code:
else
// assume variables don't match, i.e, enter a new product line on temp orders but adjust all prices that match the product number
{
$final_exis_items = ($total_exis_items + $cust_quantity);
======EXTERNAL FUNCTION THAT GIVES ME $new_orderprice========
$query5 =
"INSERT temp_orders SET item_name='$cust_item_name', quantity='$cust_quantity', item_number='$cust_item_number', orderprice='$new_orderprice', variableone='$ravvarone', variabletwo='$ravvartwo', pricetype='$cust_pricetype', sessionid='$cust_session'";
mysql_query($query5)or die(mysql_error());
$queryextra3 =
"UPDATE temp_orders SET orderprice='$new_orderprice' WHERE sessionid='$cust_session' AND item_number='$cust_item_number'";
mysql_query($queryextra3)or die(mysql_error());
echo($query5."<br><br>".$queryextra3);
//echo("<meta http-equiv=\"refresh\" content=\"0;URL=../cart/display_cart.php\">");
}
}
}
?>
Hope that helps. Feel free to blow this one off if I haven't been clear enough, I don't want anyone to waste as much time as I have!
=+O
|
|
|
08-30-2005, 02:46 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
ok, did you catch what redhead said?
PHP Code:
if ($ravvarone = $cust_variableone && $ravvartwo = $cust_variabletwo)
you are assigning values in your 'if' statement. when you assign values in an if statement, it is usually always going to return true.
as he suggested, didn't you mean to check for equality with == ?
in example:
PHP Code:
if ($ravvarone == $cust_variableone && $ravvartwo == $cust_variabletwo)
__________________
Mike
|
|
|
08-30-2005, 02:49 PM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,710
|
Like I did point out, the
PHP Code:
if ($ravvarone = $cust_variableone && $ravvartwo = $cust_variabletwo)
needs to be
PHP Code:
if ($ravvarone == $cust_variableone && $ravvartwo == $cust_variabletwo)
since you're not comparing the two, but instead assigning them, which will allways result in a true statement, thus never reaching the else clause you have to catch it.
//edit I see Mike cought it too 
|
|
|
08-30-2005, 04:43 PM
|
#9 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
 I'll add my usual line: don't re-invent the wheel. There are some good carts out there.. oscommerce, phpshop (you'll probably have to hack on it a bit) and various others..
-r
|
|
|
08-31-2005, 08:05 AM
|
#10 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 124
|
I am grateful to all of you. There were a few other bugaboos in the code, but I was staring at that one over and over without seeing it. Working fine now.
Oh, and idx . . . please understand that I MUST re-invent the wheel, over and over and over . . . how else will I learn PHP?
=+)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:20 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|