Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_fetch_assoc() Loop question 2

Status
Not open for further replies.

jsteiner87

Programmer
Oct 9, 2003
76
0
0
US
I have my mysql recordset and I want to loop thru the recordset and display the data in a table with a set of condtions. I want to then loop thru the recordset again and display the data again but with a different set of conditions.

Here is basically what I have:

$query_rsMysql = "SQL Statemet";
$rsMysql = mysql_query($query_rsMysql, $database) or die(mysql_error());
$row_rsMysql = mysql_fetch_assoc($rsMysql);

//Then I want to loop thru the recordset twice.

do {
if (some condition is true) {
//Display something here from databse table
}
}while ($row_rsMysql = mysql_fetch_assoc($rsMysql));


do {
if (some other condition is true) {
//Display something here from database table
}
}while ($row_rsMysql = mysql_fetch_assoc($rsMysql));


I don't have the code in front of me so I wrote it out with the same structure and everything.
 
It won't work as written. The first loop will quit when there are no more records to fetch from the result handle, so at best your second loop will only run once, with the last record fetched.

If you must start fetching records all over again, you may have to use mysql_data_seek() on the result handle to move the result handle's record pointer back to the beginning.

But instead of looping through the result array twice, you might consider populating two arrays with selected data from the query, then displaying from the arrays:

Code:
$query_rsMysql = "SQL Statemet";
$rsMysql = mysql_query($query_rsMysql, $database) or die(mysql_error());

$a = array();
$b = array();

while ($row_rsMysql = mysql_fetch_assoc($rsMysql))
{
  if (some condition is true) {
    // push the record to the end of array $a
  }

  if (some other condition is true) { 
     // push the record to the end of array $a
  }
}

foreach ($a as $record)
{
	//display records
}

foreach ($b as $record)
{
	//display records
}



Want the best answers? Ask the best questions! TANSTAAFL!
 
Oops. Hit "submit" rather than "preview"

(I changed your do...while() loops to while() loops because while() loops are more appropriate for this application. A do...while() loop must always run once but a result set can return zero records.)

Whether or not my suggestion is practical depends on the number of records you're retrieving and other factors.



Want the best answers? Ask the best questions! TANSTAAFL!
 
And you're question is?

Anyway, If you are going to loop through the results several times (although why you would need to that is beyond me, as any filtering should be done through the query) you can just put all your results into an array, and then you can re-use it as many times as you need.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita, i usually do filtering thru the query but I am trying to seperate the information in the database into two different tables on the php page.

Table 1 - Medium Devices

Table 2 - Low Devices

The information is stored in the same MySQL table but I was wondering if there was a way to seperate them out to display them. I think I will just use two queries.
 
You can separate them, As i Said, instead of trying to run the Dowhile loop twice on the mysql_fetch_assoc , run it once and filter into two arrays.

like:

Code:
do{
if(condition one is met){
[green]//Put results for this condition in one array[/green]
}
if(condition two is met)
[green]//Put results for this condition in second array[/green]
}
else{ [green]if none is met do something else[/green]}

}while($rows=mysql_fetch_assoc...);

Once that is done you can loop through the arrays, and display the info contained therein, wherever you want.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top