Hello,
I’m trying to find out which users referred another user 5 levels deep. When I uncomment the code the function is getting the results. My problem is getting those results out of the function and able to display in different parts of the page.
I need the total number ($num_rows) of users at each level. I know I need to put the results into an array and return the array but I haven’t been able to get the correct code to make that happen. Any help is appreciated! The code is below:
function getDownline($id, $level=1) {
if ($level > 5) { break; }
// echo "<BR>level is $level<BR>";
$result=@mysql_query("SELECT id,r FROM users WHERE r='$id'");
$num_rows = mysql_num_rows($result);
while($row=@mysql_fetch_assoc($result))
{
$id = $row['id'];
$r = $row['r'];
// echo "$r is r and $id is id<BR>";
}
// echo "<BR>Level $level has $num_rows results<BR>";
if($num_rows > 0) {
getDownline($id, $level + 1);
}
}
// Get users downline
getDownline(44, '1');
See how this goes for you, a point i want to stress also is using break inside an IF statement won’t work as they are only designed for loops and switches.
function getDownline($id, $level=1) {
static $found = array();
if ($level > 5) {
return $found;
}
//echo "<br />level is $level<br />";
$result = mysql_query("SELECT id, r FROM users WHERE r='$id'");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
while($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$r = $row['r'];
$found[] = array('id' => $id, 'r' => $r);
//echo "$r is r and $id is id<br />";
}
//echo "<br />Level $level has $num_rows results<br />";
getDownline($id, $level + 1);
}
return $found;
}
// Get users downline
$downline = getDownline(44, '1');
echo '<pre>';
print_r($downline);
echo '</pre>';
That’s awesome! I’ve spent all day on that! Works great!
I’ve been reading about the Nested Set Model (http://dev.mysql.com/tech-resources/articles/hierarchical-data.html). Would it be worth it to use that method instead of the one here? Best case scenario I’d be dealing with a million records.
Thanks again!
Tim
No problem, i don’t like telling people what to use. I’m more comfortable saying use what you are comfortable with, if your database ends up with millions of records then i would say play around with the nested set model and later on you can integrate the updated code when your database grows.
Yeah, it’ll be better to wait and integrate that later should the need arise.
A friend of mine sent me some code to accomplish the same task. He doesn’t have a PHP background but tried to write it as closely to PHP as he could. I then tried to modify it and get it working but with no success. I have working code now so I don’t need his to work, but I’m curious to see what changes would be make to get it working. If you or anyone else feels up to it, this is his unedited code…
function getFriendsOfSingleUser($id)
{
$rows = "Select r from customers where id = $id";
returns rows;
}
function getCombinedFriendsOfManyUsers($array_of_ids)
{
$array_combined = empty array;
foreach($user in $array_of_ids)
{
$user_friends = getFriendsOfSingleUser($user);
$array_combined.append($user_friends);
}
return $array_combined;
}
$user = "44";
$level2 = getFriendsOfSingleUser($user);
$level3 = getCombinedFriendsOfManyUsers($level2);
$level4 = getCombinedFriendsOfManyUsers($level3);
$level5 = getCombinedFriendsOfManyUsers($level4);
Thanks again!
Tim
Just a few simple changes were needed, you will need to add the MySQL query but it should work.
function getFriendsOfSingleUser($id)
{
// MySQL query here
return $rows;
}
function getCombinedFriendsOfManyUsers($array_of_ids)
{
$array_combined = array();
foreach($array_of_ids as $user)
{
$user_friends = getFriendsOfSingleUser($user);
array_push($array_combined, $user_friends);
}
return $array_combined;
}