Jump to content


Photo

store echo value on a variable


Best Answer Jacques1, 21 March 2017 - 07:37 PM

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.

Go to the full post


  • Please log in to reply
7 replies to this topic

#1 dardime

dardime
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 20 March 2017 - 10:02 PM

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!



#2 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 3,512 posts
  • LocationVoorheesville NY

Posted 20 March 2017 - 10:39 PM

Uh, Which line is not working as it is supposed to? Your question is a bit unusual and nothing in your code is.
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#3 dardime

dardime
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 20 March 2017 - 10:59 PM

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";


#4 Jacques1

Jacques1
  • Gurus
  • Turtles all the way down
  • 3,431 posts

Posted 20 March 2017 - 11:31 PM

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);


#5 dardime

dardime
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 21 March 2017 - 07:51 AM

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 by dardime, 21 March 2017 - 07:52 AM.


#6 Jacques1

Jacques1
  • Gurus
  • Turtles all the way down
  • 3,431 posts

Posted 21 March 2017 - 08:51 AM

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.



#7 dardime

dardime
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 21 March 2017 - 07:19 PM

Hi Thank you for the reply. I know its basic programming but WHY it outputs 0 (zero)?

 

 

wbumq8.jpg

 

If i use this code below it will work correctly:

$percentage = 0.1;
$result = (1.0 - $percentage) * 80;

echo 'The result is '.$result.'<br>';


#8 Jacques1

Jacques1
  • Gurus
  • Turtles all the way down
  • 3,431 posts

Posted 21 March 2017 - 07:37 PM   Best Answer

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users