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 for diafol
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){
    ...
}

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 for diafol

Sorry, you lost me.

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

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 for diafol

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?

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

@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.

@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 for diafol

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.

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 for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.