IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> One Query, Two While Loops?
son
post Oct 28 2009, 11:52 AM
Post #1


Invested Member
***

Group: Members
Posts: 619
Joined: 21-May 08
Member No.: 22,185



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:
CODE
$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
Go to the top of the page
 
+Quote Post
justsomeguy
post Oct 28 2009, 06:00 PM
Post #2


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



Loop through and dump the results into an array, then you can loop through the array as much as you want.
Go to the top of the page
 
+Quote Post
son
post Oct 30 2009, 09:11 AM
Post #3


Invested Member
***

Group: Members
Posts: 619
Joined: 21-May 08
Member No.: 22,185



QUOTE (justsomeguy @ Oct 28 2009, 06:00 PM) *
Loop through and dump the results into an array, then you can loop through the array as much as you want.


How can I dump the results in an array?

Son
Go to the top of the page
 
+Quote Post
justsomeguy
post Oct 30 2009, 04:11 PM
Post #4


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



CODE
$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
Go to the top of the page
 
+Quote Post
son
post Nov 4 2009, 08:23 PM
Post #5


Invested Member
***

Group: Members
Posts: 619
Joined: 21-May 08
Member No.: 22,185



QUOTE (justsomeguy @ Oct 30 2009, 04:11 PM) *
CODE
$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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 23rd November 2009 - 05:19 AM