I had posted the code before but had asked it differently as I didn’t really know what was going on.
I have several while loops fetching data from the database to form a feed of the latest information. Before you ask I will use mysqli but am just using mysql for now so i can get the loops sorted because i feel more comfortable this way.
Each sql query can but not definitely have several rows of data so a while loop is needed. I also must organize all the data into an array at the end so it can all be ordered by timestamp (timestamp not in sql query yet).
The problem I am having is that to organize it all into the array, with the way it is done now, all the closing curly brackets must be after the array so that every bit of data can be put into it. Due to this some variables could be outputted more than once because there can be different numbers of rows in each sql query. The brackets are not arranged like this at the moment by the way.
In the code below the brackets for each of the second lot of while loops are straight after the variables. This means only one row in the table is outputted into the array.
I hope that that explains it. It’s the best I could do. How can I arrange the while loops so that ALL data from EACH of the second lot of querys can be outputted into the array?
<?php
//fetch favourited artist(s)
$fetchartistFavourite = mysql_query("SELECT * FROM artistfavourites WHERE username = '$username' AND password = '$pass';")or die(mysql_error());
while ($artistFavourite = mysql_fetch_array($fetchartistFavourite)){
$favouritedArtist = $artistFavourite['artistname'];
$favouritedArtistUrl = $artistFavourite['artisturl'];
//fetch favourite track(s)
$fetchtrackFavourite = mysql_query ("SELECT * FROM trackfavourites WHERE username = '$username' AND password = '$pass'")or die(mysql_error());
while ($trackFavourite = mysql_fetch_array($fetchtrackFavourite)){
$favouritedTrack = $trackFavourite['artistname'];
$favouritedTrackUrl = $trackFavourite['artisturl'];
//Get news from favourited artist(s)
//Get updates to bio
$fetchupdatedBio = mysql_query ("SELECT bio FROM members WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($updatedBio = mysql_fetch_array($fetchupdatedBio)){
$updatedBio = $updatedBio['bio'];
}
//Get any new pictures
$fetchPic = mysql_query ("SELECT picurl FROM pictures WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($pic = mysql_fetch_array($fetchPic)){
$pic = $pic['picurl'];
}
//Get any new tracks
$fetchTracks = mysql_query ("SELECT * FROM tracks WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($tracks = mysql_fetch_array($fetchTracks)){
$trackurl = $tracks['trackurl'];
$trackname = $tracks['trackname'];
}
//Get any new gigs
$fetchGigs = mysql_query ("SELECT * FROM gigs WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($gigs = mysql_fetch_array($fetchGigs)){
//arrange gig data into format to be echoed
$gig = $favouritedArtist.' is playing for the gig ' .$gigs['gigname'].' at ' .$gigs['venue'].' on the '.$gigs['day'].'th of '.$gigs['month'].', '.$gigs['year'];
}
//Get any new sessions
$fetchSessions = mysql_query ("SELECT title FROM sessions WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($sessions = mysql_fetch_array($fetchSessions)){
$sessionName = $sessions ['title'];
}
//Get new tracks from favourited tracks(s)if the artist has not been favourited
$fetchnewTrack = mysql_query ("SELECT * FROM tracks WHERE artistname = '$favouritedTrack' AND artistname !='$favouritedArtist'")or die(mysql_error());
while ($newTrack = mysql_fetch_array($fetchnewTrack)){
$trackname2 = $newTrack['trackname'];
$trackurl2 = $newTrack['trackname'];
}
//asign all variables into an array
//echo array
}
}
?>
Your code should have been indented since the beginning.
In order to get help, please at least make yourself helpable.
Now let’s see where the problem is.
If I did correctly get your problem, just use temporary arrays for each of your requests.
But, as said, PDO gives you a powerful tool named fetchAll. You should give it a look.
( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers )