Jump to content


Photo

Variables


  • Please log in to reply
4 replies to this topic

#1 ivalea

ivalea
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 05 October 2006 - 07:34 PM

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:

if ($partno = 'B-100'){ 

//then run query
}

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

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

Any easy ways to accomplish this?  Thanks in advance!  :)

#2 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 05 October 2006 - 08:01 PM

You need to use double ='s.

e.g
if ($partno == "B-100"){ 
//query here
}


#3 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 05 October 2006 - 08:09 PM

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

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

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'){
something
}

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

Here is what happens with the original $partno = 'B-100' || 'B-200'.  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.activest...precedence.html

From the manual, we can see that || has a higher precedence than =.  So here's what happens:
  • '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 reassigned to the value true.
  • $partno is evaluated next.  $partno now has the value true, and the expression passes the true / false test.

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.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#4 ivalea

ivalea
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 05 October 2006 - 10:55 PM

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! :)

#5 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 06 October 2006 - 06:04 PM

So what you're saying is that by purchasing a product they gain access to a menu?
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users