Okay, so I finally figured it out, but SURELY there must be a better way.
Basically what I'm trying to do is have the ouput result (in the foreach) have the same order as the first query
Code: Select all
// This is the original query to select the id, and whoviewed
$getlistofnamesquery = mysql_query("SELECT id,whoviewed FROM viewedprofile WHERE whosprofile = '$u' ORDER BY id desc LIMIT 25");
$namesnumber2 = mysql_num_rows($getlistofnamesquery);
if($namesnumber2 < 1)
{
echo "Not Available";
} ELSE
{
// Here I'm storing the username (whoviewed) and id into an array
// with the username as the KEY and the id as the VALUE
while($getnamesarray17 = mysql_fetch_assoc($getlistofnamesquery))
{
$names[$getnamesarray17['whoviewed']] = $getnamesarray17['id'];
}
// $names2 is simply selecting the usernames (the keys) for use in the sql query
$names2 = array_keys($names);
$namelist = implode("','",$names2);
$online = mysql_query("SELECT username, user_last_visit FROM users WHERE username IN ('$namelist')") or die(mysql_error());
// Now I'll store the results of the above query in an array with the id from $names as the KEY and the time_last_active as the VALUE
while($onlinearray = mysql_fetch_assoc($online))
{
$timeonline[$names[$onlinearray['username']]] = $onlinearray['user_last_visit'];
}
// Now I want to sort the keys from highest to lowest
// ksort will do the opposite
ksort($timeonline, SORT_NUMERIC);
// so reverse it, and I'll have it in the order I want
$highesttolowestkeys = array_reverse($timeonline, TRUE);
// now I want the ID as KEYS and usernames as VALUES
$names3 = array_flip($names);
$timediff = time()-300;
$i=1;
// in the foreach I will select the value of the ID in $names3 (which is the username)
foreach($highesttolowestkeys AS $id => $time)
{
if($time > $timediff)
{
echo "<small>$i.</small> <a href=\"showme.php?u=".$names3[$id]."\"><font color=\"#FF0000\">".$names3[$id]."</font></a>, ";
} ELSE
{
echo "<small>$i.</small> <a href=\"showme.php?u=".$names3[$id]."\">".$names3[$id]."</a>, ";
}
$i++;
}
The whole purpose of doing this this way was to avoid having a query in a while loop. Is there a better way to go about this? (It gave me a headache figuring it out this way) Would I be better off using a query inside a while loop (that will iterate 25 times) ? (ideally this is saving 24 queries)
Currently, it works pretty good. I just feel like I did it buttbackwards.
*sigh* hope I explained this good.