PHP And if or else ? What to use to get code to work properly
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>";
Related Article: PHP/MySQL Zip Code Distance Locator
is a PHP discussion thread by AmieCutie that has 7 replies, was last updated 10 months ago and has been tagged with the keywords: zip, code, distance.
minimee120
Junior Poster in Training
76 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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){
...
}
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
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
minimee120
Junior Poster in Training
76 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
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.
zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 13
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?
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
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
minimee120
Junior Poster in Training
76 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.
minimee120
Junior Poster in Training
76 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.
minimee120
Junior Poster in Training
76 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
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 :).
minimee120
Junior Poster in Training
76 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
OK, if you want to post more code - e.g. where do these variables come from? what do they contain?
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57