Jump to content

Need help testing a POST variable


vincej

Recommended Posts

HI - I'm trying to write an expression which tests the existence of a value within a POST variable. I have tried every which way ie isset, empty, ==0.00, NULL and yet I'm doing something wrong as nothing works. I hope you can help this student of PHP !  The expression is testing whether a price per lb exists ie the price of chicken per pound. But some products are not priced this way eg a jar of jam has no $pricelb[$i].  Here's some detail. The expression I am testing is:

 


if (!isset($pricelb[$i])){           // FIRST HALF FAILS 
$ordervalue =  $price[$i];
$weight = '0.00';}
else	                                      // SECOND HALF WORKS 
$pricelb = $pricelb[$i];{
$weight = $weight[$i];  
$ordervalue = $pricelb * $weight;

 

 

 

The $pricelb[$i] refers to an array of product weights coming off of the array $pricelb =  $_POST['pricelb'] which I then cycle through using a for Loop, hence $pricelb[$i].

 

Bottom  Line: if there is no pricelb then the then the ordervalue for the purchase is the same as the starting unit price.

 

Suppose I have 2 products on the order. One with a pricelb of 5 and one with none. Then I do print_r on $_POST['pricelb'] I get:

Array ( [0] => 5 )

  I.E. the second value is absent. YET, if I look into the DB table I see pricelb 0.00 for the second value  - very confusing !

 

 

MANY MANY thanks ! for all  your help !

 

 

 

 

 

 

Link to comment
Share on other sites

It looks like you're doing an if else. If it's 0 do one thing, otherwise do another. The same logic applies if you want to do if it's not 0, do that other thing, otherwise do that first one.

Link to comment
Share on other sites

Ok - I get it, so I flipped the logic around the other way and I now get succes where I had failure and failure where i had success

 

 

for ($i = 0; $i < $numloops ; $i++) {

if ($pricelb[$i] < 0 ){
$pricelb = $pricelb[$i];
$weight = $weight[$i];  
$ordervalue = $pricelb * $weight;}
else
$ordervalue =  $price[$i];

 

 

When I echo the outputs of this I get:

 

 

line 214   Expected Price Array ( [0] => 60.00 [1] => 15.00 )
line 215  PriceLB 5.00
line 216  Weight 5
line 217 OrderValue 60.00
line 218  ProdID 17
line 219  Order ID JAC8846

//********************************************//

line 214   Expected Price Array ( [0] => 60.00 [1] => 15.00 )
line 215  PriceLB 0.00
line 216  Weight 
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: models/mpos.php

Line Number: 225

line 217 OrderValue 15.00
line 218  ProdID 28
line 219  Order ID JAC8846

 

 

I'm not surprised that I get an offset error on the second pass as now the variable will be empty for $pricelb[$i]

 

 

 

Thanks for sticking with this !

Link to comment
Share on other sites

OOPS .. finger trouble should  have been > 0 !  AND IT NOW WORKS !!

 

Weird thing though - how flipping the logic caused it to work whilst, testing for if ($pricelb[$i] == 0 ) or NULL  should fail. I suppose that  the >0 expression is less specific and therefore has a better chance of passing ??

 

MANY MANY Thanks you are a super star !

 

Have a good weekend - till next time !  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.