Jump to content

Variables


ivalea

Recommended Posts

Hi -

I'm building a site with a shopping cart and on the checkout success page I would like to check and see what model number they purchased and then run a query based on which model number it was.  What I'm trying to find out is how would I assign my variable for this?  Right now I have this:

[code]
if ($partno = 'B-100'){

//then run query
}
[/code]

Basically all part no's will begin with B-.  I tried the || w/o any success:

[code]
if ($partno = 'B-100' || 'B-200') {
//then run query
}
[/code]

Any easy ways to accomplish this?  Thanks in advance!  :)
Link to comment
Share on other sites

That if statement is all kinds of wrong for what you want.

Let's start with the syntax of if:
if([b]expr[/b]){
[i]stmts[/i]
}
If [b]expr[/b] is true, then do [i]stmts[/i].

Now, what you want to do is check if $partno is equal to any of multiple possibilities.  Let's say it in english, if $partno is equal to XXX OR $partno is equal to YYY OR $partno is equal to ZZZ, then do something.
if($partno == 'XXX' || $partno == 'YYY' || $partno == 'ZZZ'){
[i]something[/i]
}

There are a couple difference between that and what you wrote.  The first is the usage of [b]==[/b] instead of =.  Also, you have to check $partno against each possible value separately.

Here is what happens with the original [b]$partno = 'B-100' || 'B-200'[/b].  Remember, if is trying to evaluate an expression as true or false.  We will also need to look at PHP's operator precedence, http://aspn.activestate.com/ASPN/docs/PHP/language.operators.precedence.html

From the manual, we can see that [b]||[/b] has a higher precedence than [b]=[/b].  So here's what happens:
[list]
 [*]'B-100' || 'B-200' evaluates first.  A string is non-null so evalutes to true.  true OR true gives true.
 [*]$partno = true evaluates next.  $partno is [b]reassigned[/b] to the value true.
 [*]$partno is evaluated next.  $partno now has the value true, and the expression passes the true / false test.
[/list]

I just wanted to clear up the way in which PHP evaluates expressions for you because without knowing that you'll run into a lot of trouble later.

Let's think about the process of a shopping cart from the user's perspective:
1) User clicks a shopping link
2) User might filter their shopping categories, repeatable step
3) User adds items to their cart, repeatable step
4) User submits order
5) User prints or saves order confirmation

Now let's think about the process from your side of things:
1)  Each time the user filters the products or views a shopping cart page, you're going to have to run some sort of database query to get the proper products.
2)  For each product, that query is going to return information such as if it's in stock, the unit price, the product name, the model number, the product description, etc.
3)  You will then have to format all of these products into an HTML form so the user can actually shop.
4)  When the user submits the order, all of their selections will be available in $_POST
5)  You look at the $_POST data and create new orders in your site's database as necessary.

Basically, what I'm trying to understand is why do you need to branch on the confirmation page based off of what was selected?  Most, if not all, of the necessary information should already be contained within $_POST when the buyer submits the order.
Link to comment
Share on other sites

Thanks roopert!  Basically what I'm doing is building a menu based upon which product a member has purchased.  There should be up to ten model #'s that once purchased give the user access to the corresponding directory.  So on this success page, it should automatically enter the user in a separate members table - since there are other products other than these 10 - later, on their main account page, I use a switch to build the menu based upon the information included in the members table.

Unfortunately, I tried the script you provided and it didn't work.  I totally get what you were trying to explain so I'll try something else. 
Thanks again! :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.