dardime Posted March 20, 2017 Share Posted March 20, 2017 Hi I need help to store echo value in php and store on a variable but i get empty result. Thank you <?php ob_start(); $str = $row_product["rrp"]; echo $str; $value = ob_get_contents(); $percentToGet = 10; $percentInDecimal = $percentToGet / 100; $result= $value * $percentInDecimal; ob_end_clean(); echo $result-$value; ?> Thank you! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 20, 2017 Share Posted March 20, 2017 Uh, Which line is not working as it is supposed to? Your question is a bit unusual and nothing in your code is. Quote Link to comment Share on other sites More sharing options...
dardime Posted March 20, 2017 Author Share Posted March 20, 2017 Hi Thank you for your reply. Here is the code im fetching but it is empty $str = $row_product["rrp"]; If i will put a static value it will work echo "80"; Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 20, 2017 Share Posted March 20, 2017 First, the code doesn't make any sense. Why on earth do you echo the content of a variable and capture the output in another variable? Why not just use the first variable? Secondly, you need to learn how to do basic debugging. Obviously your $row_product doesn't contain the data you think it does. So check how it actually looks like: var_dump($row_product); Quote Link to comment Share on other sites More sharing options...
dardime Posted March 21, 2017 Author Share Posted March 21, 2017 (edited) Hi There is a value into it. I fetch it from db i think i dont need to show that anymore echo $row_product["rrp"]; The value of that is 80. Edited March 21, 2017 by dardime Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 21, 2017 Share Posted March 21, 2017 Then what is even the problem? Forget about this crazy output buffering stuff and just use the variables: $percentage = 0.1; $result = (1.0 - $percentage) * $row_product["rrp"]; echo 'The result is '.$result.'<br>'; This is basic programming. Your math also seems to be off. When you calculate $result - $value, you get a negative value. I'm fairly sure you want the opposite direction, i. e. 90% of the original value. Quote Link to comment Share on other sites More sharing options...
dardime Posted March 21, 2017 Author Share Posted March 21, 2017 Hi Thank you for the reply. I know its basic programming but WHY it outputs 0 (zero)? If i use this code below it will work correctly: $percentage = 0.1; $result = (1.0 - $percentage) * 80; echo 'The result is '.$result.'<br>'; Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted March 21, 2017 Solution Share Posted March 21, 2017 Do you not understand that there's a difference between the number 80 and the string “$80”? Can't you see the dollar sign? This will fudge up any calculations, because PHP cannot convert that into a meaningful number (so it converts it to 0). You need to repair your database. The price must be stored in a numeric column (e. g. DECIMAL) without any currency symbols. If the currency is always the same, you don't need to store it at all. If there are different currencies, I recommend you store them in an additional column and then add the currency symbol in the application. Quote Link to comment 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.