Jump to content

[SOLVED] Loops...


dan2684

Recommended Posts

Hello all,

 

I'm fairly new to PHP so I might be asking a stupid question, but here goes...

 

- I'm using mysql_fetch_array() to pull in an array of everything I need from my database.

- Then I'm using a 'while' loop to display all the relevant information.

 

The problem is, I want to take something from the very first record and echo it out in all the other remaining loops.

 

For example:

 

while($myArray = mysql_fetch_array($sqlQueryResult)){
  echo "blah blah blah" . $myArray['1'] . "blah blah blah" . $myArray['2'] . "blah blah blah" . $myArray['3'];
}

 

If there are 10 records, it will run through this 10 times, dislpaying different information. But what can I do if want $myArray['3'] to be pulled out from the first record, and then duplicated throughout the other 9 loops?

 

Thanks in advance,

 

Dan

 

Link to comment
https://forums.phpfreaks.com/topic/114403-solved-loops/
Share on other sites

You need to store it in another variable -- only if it wasn't stored before. I would also recommend using mysql_fetch_assoc() so you use the field names as indices into your array.

 

Something like this:

<?php
$stored_var = '';
while ($row = mysql_fetch_assoc($result)) {
     if ($stored_var == '') $stored_var = $row['field_you_want_to_keep'];
     echo "blah blah blah" . $row['field2'] . "blah blah blah" . $row['field2'] . "blah blah blah" . $row['field3'] . ' xxxxxx ' . $stored_var;
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/114403-solved-loops/#findComment-588314
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.