Keep variables outside while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KLM
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 2:55 pm

Keep variables outside while loop

Post 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?

:D
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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);
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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"...
KLM
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 2:55 pm

Post 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.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Instead of:

Code: Select all

$personid1 = $ID_array[0];
print $personid1;
Do:

Code: Select all

print $ID_array[0];
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Just print it inside the loop without the need to keep it in a new array.
Something like:

Code: Select all

echo $row['id'];
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).
KLM
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 2:55 pm

Post by KLM »

ok wrote:Instead of:

Code: Select all

$personid1 = $ID_array[0];
print $personid1;
Do:

Code: Select all

print $ID_array[0];
If i does this I only get the first persons ID.
KLM
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 2:55 pm

Post by KLM »

Oren wrote:Just print it inside the loop without the need to keep it in a new array.
Something like:

Code: Select all

echo $row['id'];
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 :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I've read your post but I don't get it. :?:
KLM
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 2:55 pm

Post 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 :)
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
Post Reply