Jump to content

Recommended Posts

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.

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>");
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.