Page 1 of 1
Keep variables outside while loop
Posted: Wed May 31, 2006 3:00 pm
by KLM
When I print INSIDE the loop I get all the values from the database.
When I print OUTSIDE the loop i only get the LAST value from the database
while( $row = $db->sql_fetchrow($result) )
{
$person = $row['person_id'];
}
print $person;
// prints the last persons id
How can I get all the persons id without printing them inside the loop?

Posted: Wed May 31, 2006 3:11 pm
by bdlang
The while() loop will iterate through the entire resultset returned from the SQL statement, i.e. all records returned. Once there are no more records to iterate through, the last record is assigned to $row and available outside the loop, as you've indicated. If you don't want to print the ID values inside the loop, you can instead assign the value of $row['person_id'] to a new array, e.g.
Code: Select all
// declare a blank array
$ID_array= array();
while( $row = $db->sql_fetchrow($result) )
{
// each loop through will assign a new ID value to an array element
$ID_array[] = $row['person_id'];
}
$firstID= $ID_array[0]; // first person's ID
$last= count($ID_array) -1;
$lastID= $ID_array[$last]; // last person's ID
// testing
print_r($ID_array);
Posted: Wed May 31, 2006 3:27 pm
by ok
... OR if you want the whole line:
Code: Select all
$ID_array= array();
while( $row = $db->sql_fetchrow($result) )
{
// each loop through will assign a new ID value to an array element
$ID_array[] = $row;
}
print_r($ID_array[0]); //will output the first line, i.e:["first_col"]=>"value", ["second_col"]=>"value"...
Posted: Thu Jun 01, 2006 9:28 am
by KLM
Perfect!
But now i want to print these values:
$personid1 = $ID_array[0];
print $personid1;
//nice, it outputs the first persons id.
$personid2 = $ID_array[1];
print $personid2;
//prints out the next persons id.
Is there a smart functions to print all the IDs without making a variable for each array? I don't even know how many IDs it will output from time to time.
Posted: Thu Jun 01, 2006 9:32 am
by ok
Instead of:
Code: Select all
$personid1 = $ID_array[0];
print $personid1;
Do:
Posted: Thu Jun 01, 2006 10:17 am
by Oren
Just print it inside the loop without the need to keep it in a new array.
Something like:
I assumed that your class uses the mysql_fetch_assoc() function, if it uses something else then just replcae the
'id' with whatever it should be (e.g with
0).
Posted: Thu Jun 01, 2006 10:31 am
by KLM
ok wrote:Instead of:
Code: Select all
$personid1 = $ID_array[0];
print $personid1;
Do:
If i does this I only get the first persons ID.
Posted: Thu Jun 01, 2006 10:33 am
by KLM
Oren wrote:Just print it inside the loop without the need to keep it in a new array.
Something like:
I assumed that your class uses the mysql_fetch_assoc() function, if it uses something else then just replcae the
'id' with whatever it should be (e.g with
0).
As you can read in my first post, I don't want to print inside the loop

I just vant to give all the arrays a unique variable because I'm using the value for later

Posted: Thu Jun 01, 2006 2:09 pm
by Oren
I've read your post but I don't get it.

Posted: Thu Jun 01, 2006 3:12 pm
by KLM
The result of a while loop is different values.
If i print INSIDE the loop it would output the persons id:
1st loop <b>print $row['person'];</b> = 3
2nd loop <b>print $row['person'];</b> = 6
3rd loop <b>print $row['person'];</b> = 20
4th loop <b>print $row['person'];</b> =24
If I print OUTSIDE the loop i have to write:
print $ID_array[0]; = 3
print $ID_array[1]; = 6
print $ID_array[2]; = 20
print $ID_array[3]; = 24
to get the same output.
I would like a script that makes it easy for me to set variables on all the arrays outside the loop.
A workaround is:
$person1 = $ID_array[0];
$person2 = $ID_array[1];
$person3 = $ID_array[2];
$person4 = $ID_array[3];
print $person1;
print $person2;
print $person3;
print $person4;
But in this case I don't know if i have set up enough variables. What if the loop finds twenty persons??
I know it seems odd and it's hard to explain, but I think this is the way I want it to be

Posted: Thu Jun 01, 2006 3:25 pm
by PrObLeM
Code: Select all
$n = count($ID_array);
for($i=0;$i<$n;$i++) {
${"persion" . $i} = $ID_array[$i];
}
--OR--
Code: Select all
extract($ID_array, EXTR_PREFIX_ALL, 'person');
extract()
Google: PHP dynamic variables
Posted: Fri Jun 02, 2006 6:37 am
by Oren
KLM wrote:I know it seems odd and it's hard to explain, but I think this is the way I want it to be

No offense, but it seems that you have no idea what you are doing. If I'm wrong then please tell me why can't you just print inside the loop and why you want to set a new variable for each ID.