Northern Flame Posted June 1, 2008 Share Posted June 1, 2008 I am developing a social networking site and i am finishing up the user profiles. I am currently working on the friends list of the user, but I am having trouble finding a way to have the friends table and on grabbing the data. Currently the table is like this: id friends sent pending id - the id of the user friends - list of ids of users on friends list, ex: 1,34,65,32,11 sent - list of ids of users that current user has sent friend requests to pending - list of ids of users that have requested this user as a friend the problem I am having is with grabbing the information of each friend on the friends list when displaying the current users friends. The page will have pagination, it will only display about 20 users per page. I am wondering if I should do something like this: <?php $q = mysql_query("SELECT `friends` FROM `friends` WHERE `id` = '$id'")or die(mysql_error()); $f = mysql_fetch_object($q); $array = explode(",", $f->friends); foreach($array as $key => $fid){ $query = mysql_query("SELECT `display`, `img`, `username` FROM `users` WHERE `id` = '$fid'"; $object = mysql_fetch_object($query); echo ''; // here i echo the users info } now, is this the best way to do this? run 20 queries in the foreach() loop or is there a better way? Please help, ive also considered rearranging my table but I cant find anything that will really work out better than this solution. Link to comment https://forums.phpfreaks.com/topic/108206-solved-social-networking-site-friends-list-help/ Share on other sites More sharing options...
DarkerAngel Posted June 1, 2008 Share Posted June 1, 2008 ok since I'm assuming the array is getting auto assigned keys that should always end up the same I would suggest maybe having the loop create the SQL Query rather than have it run 20 querys <?php /* Replace foreach to end */ $perpage = 20; //Amount of items to show per page $start = (($_GET['page'] - 1) * $perpage); $end = $start + $perpage; $query = "SELECT `display`, `img`, `username` FROM `users` WHERE"; while($start>$end) { $query .= " `id`='".$array['$start']."'"; $start++; if($start != $end) $query .= " AND"; } $sql = mysql_query($query); while($rows = mysql_fetch_array($sql)) { /* Displays Data */ echo("<pre>"); print_r($rows); echo("</pre>"); } ?> Link to comment https://forums.phpfreaks.com/topic/108206-solved-social-networking-site-friends-list-help/#findComment-554647 Share on other sites More sharing options...
Northern Flame Posted June 1, 2008 Author Share Posted June 1, 2008 wow thanks, that whole while statement in the middle of the sql really helped! I would have never thought of that! thanks! Link to comment https://forums.phpfreaks.com/topic/108206-solved-social-networking-site-friends-list-help/#findComment-554666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.