Jump to content

One Query, Two While Loops?


son

Recommended Posts

I have a query and would like to go twice through returned data (show data in different format). Is this not possible with same query? The first while shows nicely all info, the second one does not. The code is:

$query = "SELECT finish_id, finish, img FROM finishes WHERE range_id  = $prange";		$result = mysqli_query ($dbc, query);					if (mysqli_num_rows($finish_result) > 0)			{			echo "<select name=\"fn\" id=\"fn\">";			echo "<option value=\"0\">Select finish</option>";				while ($row = mysqli_fetch_array ($finish_result, MYSQLI_ASSOC))				{				$finish_id = $row['finish_id'];				$finish = $row['finish'];				echo "<option value=\"" . $finish_id . "\"";				echo ">" . $finish . "</option>";				}				echo "</select> ";				while ($row = mysqli_fetch_array ($finish_result, MYSQLI_ASSOC))				{				$finish_id = $row['finish_id'];				$finish = $row['finish'];				$img = $row['img'];				$finishImgPath = "assets/finishes/preview/";				$size = getimagesize("assets/finishes/preview/{$img}");				echo "<img src=\"" . $mainPath . $finishImgPath . $img . "\" " . $size[3] . " alt=\"" . $finish . "\" title=\"" . $finish . "\" /></a>";				}			}

Do I have to run the query twice?Son

Link to comment
Share on other sites

$data = array();while ($row = mysql_fetch_assoc($result))  $data[] = $row;print_r($data);

Be careful about memory requirements, if you have tens of thousands of records you'll probably hit the memory limit before you're able to store everything. For nicer memory usage, you can use this to reset the result pointer to the beginning so you can loop through it again:http://www.php.net/manual/en/function.mysql-data-seek.php

Link to comment
Share on other sites

$data = array();while ($row = mysql_fetch_assoc($result))  $data[] = $row;print_r($data);

Be careful about memory requirements, if you have tens of thousands of records you'll probably hit the memory limit before you're able to store everything. For nicer memory usage, you can use this to reset the result pointer to the beginning so you can loop through it again:http://www.php.net/manual/en/function.mysql-data-seek.php

Will have a go...Many thanks,Son
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...