Im building a simple album and track webapp but im getting into a pickle with retrieving the correct information and making it efficient. I've got two tables - album has about 500 items and track with about 5000.
So I start off with a the normal while loop:
$albumQuery = "SELECT * FROM `album` ORDER BY album ASC";
$albumDatabase = mysql_query($albumQuery, $admin) or die(mysql_error());
while($albumRow = mysql_fetch_array($albumDatabase)){
$list = "";
$list.="build some html for showing later";
this is where I get into an efficency problem:
$trackQuery = "SELECT track, album FROM `track` WHERE album = '{$album}' ORDER BY filename ASC";
$trackDatabase = mysql_query($trackQuery, $admin) or die(mysql_error());
while($trackRow = mysql_fetch_array($trackDatabase)){
$list.="track html info";
}
$listarray[] = $list;
}
natcasesort($listarray);
I then run a foreach later in the body
foreach ($listarray as $v){
$first_letter = strtoupper(mb_substr($v,9,1));
if($tmp!==$first_letter){
$tmp = $first_letter;
echo '<div class="alphaHolder">'.$tmp.'</div>';
}
echo $v;
}
This works perfectly, apart from probably being possibly massively inefficent.
I can get it to regurgitate all the info in the right way and order.
As an experiment I placed of the second while loop outside the parent loop and stuck this info into its own array. Then ran this:
foreach($trackArray as $k => $v){
if(in_array($v['album'], $albumRow)){
$list.="track html info";
}
}
But then I realized that every time it runs through the parent loop its rerunning the foreach over the 5000 items then doing the in_array 500 times. This obviously takes ages and normally crashes the browser. Leading me to the original nested while loop being slightly more efficient and convoluted.
I have started thinking that array_intersect might be the solution but I don't want to bark up the wrong tree.
There's probably too many loops and what have you going on but I need the natural sort and the alpha/numerical heading regurgitation.
Screenie: http://ink361.com/#/photos/133115598501394804_9688917