Page 1 of 1
Doing Multiple While Loops with the Same Query
Posted: Sun Feb 07, 2010 4:30 pm
by carleihar
Using this code, how would I end the first while loop and then do different code and the loop through that same loop again?
Code: Select all
$q = "SELECT something FROM whatever WHERE something='$id'";
$r = @mysqli_query ($dbc, $q); // Run the query.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
blah blah blah
}
Thanks!
Re: Doing Multiple While Loops with the Same Query
Posted: Sun Feb 07, 2010 5:08 pm
by klevis miho
to end the first loop insert a break; after blah blah blah
Re: Doing Multiple While Loops with the Same Query
Posted: Sun Feb 07, 2010 7:38 pm
by carleihar
That's not really what I was talking about...I want to start a loop, echo some something from each iteration of the loop, stop the loop to put some HTML in, then continue the loop.
Re: Doing Multiple While Loops with the Same Query
Posted: Mon Feb 08, 2010 3:11 am
by klevis miho
yeah i know, but i just posted what i do know

Re: Doing Multiple While Loops with the Same Query
Posted: Mon Feb 08, 2010 4:36 am
by papa
Save the data in an array and reuse that array.
Code: Select all
while ($row = mysqli_fetch_assoc($r)) {
$data[] = array('id'=>$row['id'], 'name'=>$row['name']);
}
print_r($data);
Re: Doing Multiple While Loops with the Same Query
Posted: Mon Feb 08, 2010 11:04 am
by carleihar
Yeah, that works... (sorry, I'm still a little new). It shows
Array ( [horse_id] => 12 [date_created] => 2009-11-16 )
for each array. How can I get just that information (the 12 and the date) and not all the Array([' stuff?
Thank you for your help!
Re: Doing Multiple While Loops with the Same Query
Posted: Tue Feb 09, 2010 1:58 am
by papa
Code: Select all
foreach($data as $d)
{
echo $d['horse_id'];
echo $d['date_created'];
echo "<br />\n";
}