Page 2 of 2

Posted: Sat Dec 30, 2006 4:40 pm
by arukomp
'xyz' will be returned to bar(1) function. bar(1) doesn't return anything, so i guess it will be Null or it will call the function again when echoing. i'm guessing because i'm not at my computer now.

ok, lets say that my function's elseif block will return 0. but i don't need $ref=0; , i need to find the nearest node, which has < 3 children nodes. that means, i need to run the function with other nodes' ids until i get the right number. but how to run a function to keep searching until the right node is found, and then return THAT node's id?

Posted: Sat Dec 30, 2006 4:48 pm
by volka
The first "child-search" that returns a valid refid stops the whole search and this value is what you're looking for. It has to bubble up the complete call stack of findref2.

Code: Select all

function findref2($refid) {
	$refid = mysql_real_escape_string($refid);
	$query = "SELECT id FROM users WHERE refid='$refid'";
	$result = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($result) < 3) {
		return $refid;
	}
	elseif (mysql_num_rows($result) >= 3) {
		while($row = mysql_fetch_array($result)) {
			$v = findref2($row['id']);
			/* The first "child-search" that returns a valid refid */
			if ( false!==$v ) {
				/* this value is what you're looking for. It has to bubble up the complete call stack of findref2 */
				return $v;
			}
		}
	}
	
	return false;
}

$ref2 = findref2(2);

Posted: Sun Dec 31, 2006 3:48 am
by arukomp
Thanks a lot! :D It works, but there's one problem...

Here's what I get when registering user 'arukomp4':

Code: Select all

arukomp (You)
      arukompas (Level 1)
             arunas (Level 2)
                   aras (Level 3)
                   arukomp4 (Level 3)  <<<
             aras2 (Level 2)
             arukomp3 (Level 2)
      arunas2 (Level 1)
      arukomp2 (Level 1)
As you see, 'arukomp4' is putted under 'arunas' in Level 3, but there are users in Level 1, who doesn't have any children: 'arunas2' and 'arukomp2'. When registering 'arukomp4' with arukomp's referral link, it should put 'arukomp4' under 'arunas2', because it's the nearest node which has less than 3 children. But this doesn't happen.

In this case, the result which i gave above, should be like that:

Code: Select all

arukomp (You)
      arukompas (Level 1)
             arunas (Level 2)
                   aras (Level 3)
             aras2 (Level 2)
             arukomp3 (Level 2)
      arunas2 (Level 1)
             arukomp4 (Level 2)  <<<
      arukomp2 (Level 1)
When registering again new users, lets say 5 of them, 2 should go under 'arunas2' and 3 should go under 'arukomp2'. Now when all 1st Level nodes are filled with 3 children, we can now look in Level 2 for the node, which has less than 3 children.

But the problem is that it doesn't put new user in the right place. It should fill Level 1 and only then search for a place in level 2. When it fills Level 2, it should only look for a place in Level 3 and so on...

Posted: Sun Dec 31, 2006 10:10 am
by volka
Instead of processing a child immediately you have to store it (or its id) until all the item of one level are tested.
You can do this e.g. with an array where you always process the first item and append children at the end.

Code: Select all

function findRefid($ancestorId, $mysql) {
	$ids = array($ancestorId);
	
	while ( !empty($ids) ) {
		$parent = array_shift($ids);
		
		$query = "SELECT
								id
							FROM
								users
							WHERE
								refid=$parent";
		$result = mysql_query($query, $mysql) or die(mysql_error().': '.$query);
		if ( 3 > mysql_num_rows($result) ) {
			return $parent;
		}
		else {
			while( $row=mysql_fetch_array($result, MYSQL_ASSOC) ) {
				$ids[] = $row['id'];
			}
		}
	}
	
	die('findRefid failed.');
}

$mysql = mysql_connect(...) or die(mysql_error());
mysql_select_db(..., $mysql) or die(mysql_error());
$ref2 = findRefid(2. $mysql);

Posted: Sun Dec 31, 2006 11:08 am
by arukomp
Thanks a lot! :D :D It works perfect now :D

Again, thanks a lot :D