Variables not taking assignment

I am pulling my hair out with this one.
I have tried everything I can think of to make the following work.
First 5 lines work fine:
[php]
$Sdollars = mysql_query($sumsalestotal, $link);
$Sdollars = mysql_fetch_row($Sdollars);

$Sdollars = money_format('%i', $Sdollars[0]);
$Sdollars = sprintf("%10s", '$'.$Sdollars);
            echo "$Sdollars";

[/php]
The above works fine
[php]
$ndivideby = money_format(’%i’, $Sdollars[0]);
$ndivideby = sprintf("%10s", ‘$’.$ndivideby);

echo “$ndivideby”; doesn’t print anything
echo “$Sdollars[0]”; doesn’t print anything
[/php]

The real problem is that need to use an existing variable to create a new variable that will be the result of 2 other variables x/y.
It doesn’t work. I have done this in other languages with no effort. I have beat this thing to death with no resolve. Any help would be greatly appreciated.

Greg Hill

I finally got around this and found the following, I hope I am correct in what I concluded any input would be appreciated.

The solution was for me to store the value of $Sdollars[0]element to my preferred variable PRIOR to it be ran through the formatting functions.

The value contained in this array element $Sdollars[0] (or any), evaporates after the following code runs
$Sdollars = money_format(’%i’, $Sdollars[0]);
$Sdollars = sprintf("%10s", ‘$’.$Sdollars);

However the variable $Sdollars does retain its value but for unknown reasons, I could not use it in a math function even after doing the following to it:
$x = (int)$Sdollars;
$y = $x * 100;
$y now is null

So, the problem is related the to the indirect effect of an array element when processed through the formatting functions (money_format and sprint at minimum).

BTW, I have not found this to be a problem in the other programming languages I use, so I was not prepared to resolve such a thing.

To avoid this anomaly I will caution and or opt to never use an array element in a formatting function.

As I mentioned on the phone, I would update you on the results of your tips.

Sponsor our Newsletter | Privacy Policy | Terms of Service