Forum Moderators: coopster

Message Too Old, No Replies

Storing php in variable

         

adamnichols45

6:44 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



print_r($title); echo" x ";
print_r($qty); echo" = ";
$tot = ($price * $qty);
$num1 = number_format ($tot, 2);
echo ($num1);
echo "<br>";

How can I get this php code into 1 varible so I can output it using:-

<input type="text" value="<?php echo ($var1); ?>"

leadegroot

10:24 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$var1 = $title.' x '.$qty.' = '.number_format ($price * $qty, 2)."<br>\n";

Untested.
Will probably do what you want, although I don't know anything about your datatypes.
Occasionally I have had trouble concatenating function calls (ie the number_format), but this doesn't look like such an instance - worse case is you'd pull the number_format call into an an earlier variable and then concatenate that result into this string.

There isn't much to the concatenation operator, but the doco is here:
[php.net...]

adamnichols45

10:40 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does it matter that the varibles are arrays populated from a database?

leadegroot

11:53 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, more that I don't *know* that, say $title isn't actually an array or object.
Of course, calling an array or an object $title would be bizarre, but I don't like to assume :)

adamnichols45

8:01 am on May 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well that half works!

Just to clarify Im saving that in a session varable as follows:-

$_SESSION['var1'] = $title.' x '.$qty.' = '.number_format ($price * $qty, 2)."<br>\n";

Now that out puts on the same page as I want
6" pens x 6 = 59.94

But all I get on the next page is 6

adamnichols45

8:14 am on May 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I finally got it working how I want it and it was a really simple mistake.

<input type="text" name="amount" size="50" value="<?php echo $_SESSION['var1'];?>">

the "" either side of the value field. When change to

'' it works fine!

Thanks for all the help :)

adamnichols45

8:27 am on May 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cant belive it! when I test it using 2 products in the shopping cart it is only out putting the last product in to the text field.

If I echo the session varible though it outputs both sets of products!

This confusing.