View Full Version : Dymanic Variables In PHP, possible? Need Help.

sir pannels
08-24-2006, 12:38 PM
Hi there,

Having some issues with a variable.
I have a query running that returns 10 results of data, in a while loop.
During the while is open it does something like :

$data = Result Data

For each row, and then after prints..

PRINT $data;

That works however I'm now trying to give dymanic variables, so I can count the rows. So id have a count in the row return ..

$count = $count +1;
$data = Result Data

And then I would like to use that count to name each row of data, so instead of

$count = $count +1;
$data = Result Data

I then get ..

$count = $count +1;
$data.$count = Result Data
// This should be called using PRINT $data1;

That's what I'm after, however stuggilin ammending the count to the end of the variable name, tried a few ways but not got anywhere..

Is this not possible? Or is there special syntax to assign another variable to the variable name?

Or should I be using another while or foreach statement?
Any information be apperciated.

Cheers all
Sir P :D

08-24-2006, 01:24 PM
cant understand what your trying to do. If your trying to give each one a id then why not try:

$num_rows = mysql_num_rows($result);
while ($i < $num_rows) {
$data = Result Data
echo "$i:$data";

sir pannels
08-24-2006, 01:27 PM
hi thanks for your reply...

I need to be able to call the data rows individually later on in the script though.. so for example, i have ran my query and had 10 results returned.. later on in the script I want to call the 3rd row of data... doing something like

print $data3

or something..
am I explaining badly? could be the case ... sorry :(

08-24-2006, 01:30 PM
Have a look at the extensive explanation of variable variables at the php.net site.
Link: http://nl3.php.net/manual/en/language.variables.variable.php

Another matter: why don't you use an array to stroe your result, such as:

$data= array();
$count = $count +1;
$data[$count] = Result Data;

That way, if you wanted data3, just address it as

echo $data[3];

Ronald :cool:

sir pannels
08-24-2006, 01:38 PM
Thanks for the link.

However none of the information there can be applied to this situation at the best of my knowledge, I just read though it and didn't see anything about changing variable names and what not ...

That being said, your array solution will work perfectly, thanks very much for your help :)