vincej Posted May 4, 2012 Share Posted May 4, 2012 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/ Share on other sites More sharing options...
Jessica Posted May 4, 2012 Share Posted May 4, 2012 This can't be your actual code, it has significant syntax errors. Before this code, add print_r($pricelb); and see what you get. Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343069 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 Hi jesirose Once again your helpig me out - thanks ! here;'s the array : Post Weight lb Array ( [0] => 5.00 [1] => 0.00 ) Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343074 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 I have tried testing for 0.00 - maybe I did something wrong in the syntax ... Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343076 Share on other sites More sharing options...
Jessica Posted May 4, 2012 Share Posted May 4, 2012 Try if it's > 0. PHP will convert it for you, Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343077 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 Not sure why I would check if it;'s greater than 0. I'm checking to see if it is zero or empty. I'll try == 0 Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343080 Share on other sites More sharing options...
Jessica Posted May 4, 2012 Share Posted May 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343089 Share on other sites More sharing options...
MMDE Posted May 4, 2012 Share Posted May 4, 2012 This might be interesting to take a look at as well: http://www.php.net/manual/en/types.comparisons.php And this: http://www.php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343093 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343095 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 I must hallucinating ! .. I'm looking at the DB table and it appears to be working. Let me triple check ! Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343097 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 Nope false hope --- Now when there is a Pricelb present the expression which calculates order value is failing. Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343098 Share on other sites More sharing options...
Jessica Posted May 4, 2012 Share Posted May 4, 2012 Your code says if the price per lb at $i is less than 0, the price is the price per lb at $i. Is that what you want it to say? Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343099 Share on other sites More sharing options...
vincej Posted May 4, 2012 Author Share Posted May 4, 2012 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/262075-need-help-testing-a-post-variable/#findComment-1343106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.