Page 2 of 2

Posted: Wed Mar 14, 2007 4:08 pm
by Weirdan
Yeah, it's ok to leave such gaps.

Posted: Wed Mar 14, 2007 4:23 pm
by s.dot
That's a great idea.

Posted: Sat May 19, 2007 12:46 am
by s.dot
Sorry for returning to this old topic so many times. I read the article posted in the first response, and took the function in the article and mangled it to display a users UPLINE, up to ten levels up.

This works for the five accounts I have programmed manually into the database. I want to see if it looks right with you devnetters to correctly display up to a 10 level upline, *correctly*, *every time*. I can envision it in my head with only 5 users, but with 5,000 users, I can't begin to picture it. Perhaps some of you more savvy people can tell me if this will work right all the time.

Code: Select all

<?php
require 'includes/php/db_connect.php';

function get_upline_array($root)
{
	//return value
	$ret = array();
	
	// retrieve the left and right value of the $root node
	$result = mysql_query('SELECT `left_boundary`, `right_boundary` FROM `downline` WHERE `username` = "'.$root.'";') or die(mysql_error());
	$row = mysql_fetch_array($result);

	// start with an empty $left stack
	$left = array();

	// now, retrieve all ascendants of the $root node
	$result = mysql_query('SELECT `username`, `left_boundary`, `right_boundary` FROM `downline` WHERE `left_boundary` < '.$row['right_boundary'].' AND `right_boundary` > '.$row['right_boundary'].' GROUP BY `username` ORDER BY `left_boundary` DESC LIMIT 0,10;') or die(mysql_error());
	
	// display each row
	while($row = mysql_fetch_assoc($result))
	{
		// only check stack if there is one
		if(count($left)>0)
		{
			// check if we should remove a node from the stack
			while($left[count($left)-1] < $row['left_boundary'])
			{
				array_pop($left);
			}
		}

		// display indented node title
		if($row['username'] != $root)
		{	
			$ret[] = $row['username'];
		}

		// add this node to the stack
		$left[] = $row['left_boundary'];
	}
	
	return $ret;
}

echo '<pre>';
print_r(get_upline_array('scott'));
echo '</pre>';

?>
Output:

Code: Select all

Array
(
    [0] => jack
    [1] => jim
    [2] => scrotaye
)
Which is correct, in order.