You won't be able to use order/limit because this is generally a bad idea when storing multiple reference records - the friends of this friend should be somehow marked in a separate table. Then you could do all sorts of stuff more efficiently.
user
id | fname | lname | email | etc.
user_friends
id |user_id| friend_id
select user_friends.friend_id, user.fname, user.lname where user_friends.friend_id=user.id and user_id=$your_target_user order by user.lname, user.fname
... on which you can output results and use mysql_num_rows.
As is, aside from the counting, you can use one of the many
sort functions [php.net] on your friends array but it's not likely to give you the results you want as it appears all that's there are numeric id's, which will have nothing to do with sorting by name.
The only way to do it that comes to mind is to look up the names of the friends in that array, store it in yet another associative array, then execute one of the sort functions on that array to order by name (or whatever.) Something like
$tmp = array(); foreach($friends as $friend) {
// Don't use * unless you REALLY need everything, and
// numeric value selects do NOT need quotes $_query = mysql_query("SELECT fname,lname FROM users WHERE id=$friend")
or die (cannot get friend data"); $_row = mysql_fetch_array($_query);
$tmp[$friend] = $_row['lname'] . ', ' . $_row['fname'];
}
// now sort by value
$tmp = asort($tmp);
foreach ($tmp as $id => $name) { echo "<p>$name</p>"; }
} ?>
This is a really long way around the fence workaround, I'd just go back and structure the database correctly and fix it. The advantages will emerge in other areas as well - smaller databases (numeric as opposed to textual fields), enabling easier and better searches, faster database access, and a lot more intelligible programming.