Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-24-2006, 11:38 AM   PM User | #1
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
Dymanic Variables In PHP, possible? Need Help.

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 :
Quote:
$data = Result Data
For each row, and then after prints..
Quote:
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 ..
Quote:
$count = $count +1;
$data = Result Data
And then I would like to use that count to name each row of data, so instead of
Quote:
$count = $count +1;
$data = Result Data
I then get ..
Quote:
$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
sir pannels is offline   Reply With Quote
Old 08-24-2006, 12:24 PM   PM User | #2
Anthony2oo4
Regular Coder

 
Join Date: Jul 2004
Posts: 201
Thanks: 8
Thanked 1 Time in 1 Post
Anthony2oo4 is an unknown quantity at this point
cant understand what your trying to do. If your trying to give each one a id then why not try:

PHP Code:
$num_rows mysql_num_rows($result);
$i=0;
while (
$i $num_rows) {
$data Result Data
echo "$i:$data";
$i++

Anthony2oo4 is offline   Reply With Quote
Old 08-24-2006, 12:27 PM   PM User | #3
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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
sir pannels is offline   Reply With Quote
Old 08-24-2006, 12:30 PM   PM User | #4
ronverdonk
New Coder

 
Join Date: Aug 2006
Location: Netherlands
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
ronverdonk is an unknown quantity at this point
Have a look at the extensive explanation of variable variables at the php.net site.
Link: http://nl3.php.net/manual/en/languag...s.variable.php

Another matter: why don't you use an array to stroe your result, such as:
Quote:
$data= array();
$count = $count +1;
$data[$count] = Result Data;
That way, if you wanted data3, just address it as
Quote:
echo $data[3];
Ronald

Last edited by ronverdonk; 08-24-2006 at 12:34 PM..
ronverdonk is offline   Reply With Quote
Old 08-24-2006, 12:38 PM   PM User | #5
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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
sir pannels is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:55 PM.


Advertisement
Log in to turn off these ads.