1,105,214 Community Members

PHP And if or else ? What to use to get code to work properly

Member Avatar
minimee120
Junior Poster in Training
86 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello all, I'm fairly new to using PHP, and for my assignment I've coded it all but I'm trying to go the extra step, and code something that if the item amount = 0 it doesn't print it out on the order form

The items are $LAPTOP $MONITOR $KEYBOARD $PRINTER

I'm trying to figure out how to get it that if one of them or all of them or a few of them are equal to 0, how can I get it to not print them on the order form

I tried using if and else if and && but it was not working for me

for instance i was taking a space before laptop and doing

if($LAPTOP==0){
print"";
else{

then have the code I had to print if the person ordered a laptop.

How do I get this to work if I have more than one item quantity as 0? or a select few as 0, or 3 of them as 0?

here is original code//

print "<br> <font size=10>Thank You $NAME</font <br>";
print"<br><br>";
print "We appreciate your business with us! Your Customer ID is: $CUST_ID<br>";
print"<br>";
print "Your order # is: $ORDER_ID. Order placed on "; print strftime('%c'); print " entails the following purchase: <br>";
print"<br>";
print "Number of Laptops: $LAPTOP Cost of laptop $750.23. <b>Total cost of laptops:</b> \$$LaptopCost<br>";
print"<br>";
print "Number of Monitors: $MONITOR Cost of monitor $275.84 <b>Total cost of monitors:</b> \$$MonitorCost<br>";
print"<br>";
print "Number of Keyboards: $KEYBOARD Cost of keyboard $80.11. <b>Total cost of keyboards:</b> \$$KeyboardCost<br>";
print"<br>";
print "Number of Printers Ordered: $PRINTER |Cost of printer $125.28.| <b>Total cost of printers:</b> \$$PrinterCost<br>";
print"<br>";
print "Shipping & Handling - $DELIVERY: \$$DeliveryCharge<br>";
print"<br>";
print "Final Amount: \$$AMOUNT <br>";
Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 
if($LAPTOP==0){
    print "";
}else{
     ...
}

However I don't see much point in the positive test, why not just use the negative:

if($LAPTOP!=0){
    ...
}

For multiples:

if($LAPTOP!=0 || $MONITOR!=0 || $SOMETHING_ELSE!=0){
    ...
}
Member Avatar
minimee120
Junior Poster in Training
86 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I was using that method diafol with the

if($LAPTOP==0){
print"";
else{
print"Number of $LAPTOP";

etc etc:

but it was not working when I used the other quantities, it worked when I implemented just for laptop when I selected 0 laptops but had a quantity for the others

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

Sorry, you lost me.

Member Avatar
drjohn
Posting Pro
526 posts since Mar 2010
Reputation Points: 50 [?]
Q&As Helped to Solve: 106 [?]
Skill Endorsements: 4 [?]
 
0
 

Use the above code for just the laptop,
then a separate if below that for the monitor, then a separate if for each of the others.

You could use an array and loop through the array, with the same sort of print logic (only print if !=0 or if greater than 0

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

I'm trying to figure out how to get it that if one of them or all of them or a few of them are equal to 0, how can I get it to not print them on the order form

try using different combinations for the variable's conditions so you can show specific results
e.g

if($LAPTOP!=0 && ( $MONITOR!=0 || $SOMETHING_ELSE!=0 ) ){
    echo "Laptop's value is not zero and either monitor or somethings else's value is not 0";
}
else if ( ($LAPTOP!=0 &&  $MONITOR!=0 ) || $SOMETHING_ELSE!=0 ) ){
    echo "either both monitor and Laptop's value is not zero or somethings else's value is not 0";
}

etc.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

Just a thought - you're hardcoding all these items, which is difficult code when it comes to maintenance - e.g. adding or deleting an item as your program matures.

As Dr. John suggests, keeping your data in an array. Are you storing product info and orders in a DB?

Member Avatar
minimee120
Junior Poster in Training
86 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry for the late reply diafol, I apoligize for any confusion I've caused you. Yes, storing on a database, phpmyadmin. Just hardcoding this php code that is running on a public_html file for my school that when we enter this info it gets loaded into a database on phpmyadmin I have. We haven't really learned arrays yet, but I'm assuming that for what I wanted to I would need an array so it could go back and check to make sure it adds the items thats quantity isn't 0

Member Avatar
minimee120
Junior Poster in Training
86 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

@zeroliken I tried working with that format of the code, however, when I ordered my items on my website and the order form came up it just showed 0 lines of what I purchased. I'm guessing I might need an array for this to work.

Member Avatar
minimee120
Junior Poster in Training
86 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

@diafol again sorry for confusing you, let me try and explain what I meant where I lost you.

so for my variables I had, $LAPTOP $KEYBOARD etc:

in my print statement they are all shown in the order form, but I didn't want to add a line that showed an item with whatever information that someone didn't order which is why I asked

if($LAPTOP==0){
print"";
else{
print"Number of $LAPTOP";

example is that there. so I have sliders(on my site) for laptop, keyboard, monitor, and printer.

If I order 0 laptops, and 1 monitor, 1 printer, and 0 keyboards, I wanted my order form after I submitted it to say only what I ordered and not that I ordered 0 laptops and 0 keyboards. When I tried using the code above for $LAPTOP, that worked, but when I tried to add in a second if statement for the next variable such as $MONITOR, then nothing would print on the final order form receipt. I hope that somewhat clears it up for you, and again apologize as I'm not yet proficient with PHP, just learning the roots.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

I'd be more inclined to have an order array, something like:

$order = array("orderid" => 12328, "userid" => NULL, "created" => 1361818953, "last_mod" => 1361819093, "discount" => 0, "promo_code" => 0, products=> array(array("product_id" => 35467, "qty" => 3), array("product_id" => 8197, "qty" => 1), array("product_id" => 257, "qty" => 20)));

That can be passed from page to page via SESSION variable or parsed and stored in a DB table.
This holds the bare minimum of data - the product type (laptop, keyboard...) can be extracted from the DB products and product_type table (INNER JOIN). The product name, cost and description can be taken from the products table. The username here is NULL as the user is surfing as a guest, but if logged in, you'd store the user id (from users table) here.

Just a thought.

Member Avatar
minimee120
Junior Poster in Training
86 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

While that's out of the assignment, I do understand what you're explaining, and in time, I probably will come across this, so cool thanks for the help :).

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

OK, if you want to post more code - e.g. where do these variables come from? what do they contain?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: