简体   繁体   中英

PHP: Only List Items With Quantity Value Greater Than 0 In If Statement

'Ello

I'm trying to get the items from a client populated "order form" to ONLY list the items that the client has designated a quantity value of 1 or more to be sent as a Purchase Order receipt that is generated and sent via email once they hit Submit.

Here's what I have so far on that part:

$i = 1;
$imax = 4;

echo "Products<br />";
echo "-------------------------------------------------------------<br />";

while ($i <= $imax) {

$itemqty = ${'qty'.$i};
$itempn = ${'pn'.$i};
$itemdesc = ${'desc'.$i};
$itemprice = ${'value'.$i};
$itemtotalprice = ${'elinetotal'.$i};

    if ($itemqty !== 0) {
            echo $itemqty . " x " . $itemdesc . " (" . $itempn . ") @ $" . $itemprice . " ea. = $" . $itemtotalprice . "<br />";
    }
$i++;
}

It lists everything correctly, except it doesn't disregard the items with a value of 0. It'll list them like this:

Products
-------------------------------------------------------------
0 x Item #1 Description (HOSE-12) @ $155.00 ea. = $0.00
5 x Item #2 Description (GAUGE-2) @ $51.00 ea. = $255.00
0 x Item #3 Description (PTC) @ $0.70 ea. = $0.00
10 x Item #4 Description (PT-234R) @ $15.94 ea. = $159.40

It may be the easiest fix, but can anyone shed some light on this? I'd greatly appreciate it!

My initial guess is $itemqty is the string '0' . Try using the not equal operator, eg

if ($itemqty != 0)

instead of the not identical operator, ie !==

Otherwise, you could try casting the $itemqty assignment as an integer, eg

$itemqty = (int) ${'qty'.$i};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM