Doing Multiple While Loops with the Same Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Doing Multiple While Loops with the Same Query

Post 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!
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Doing Multiple While Loops with the Same Query

Post by klevis miho »

to end the first loop insert a break; after blah blah blah
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Re: Doing Multiple While Loops with the Same Query

Post 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.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Doing Multiple While Loops with the Same Query

Post by klevis miho »

yeah i know, but i just posted what i do know :)
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Doing Multiple While Loops with the Same Query

Post 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);
 
 
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Re: Doing Multiple While Loops with the Same Query

Post 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!
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Doing Multiple While Loops with the Same Query

Post by papa »

Code: Select all

 
foreach($data as $d)
{
 echo $d['horse_id'];
 echo $d['date_created'];
 echo "<br />\n";
}
 
Post Reply