Logic Help

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Yeah, it's ok to leave such gaps.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

That's a great idea.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply