Jump to content

Check this out recursive


Cardale

Recommended Posts

        public function getBonusChildren($userID)
        {
	if($userID != NULL)
	{
		$sql = 'SELECT COUNT(*) AS count, userID FROM jos_backoffice_users WHERE parentID= ' . $userID . ';';
		$stmt = conn::getInstance()->prepare($sql);
		$stmt->execute();
                	$obj = $stmt->fetchAll(PDO::FETCH_ASSOC);
		if(is_object($obj))
		{
			$obj->count += $this->getBonusChildren($obj->userID);
		}
	}
                return $obj->count;
}

 

I get no error.  It just always return null.  How can I get this done?

Link to comment
Share on other sites

Here is another version that only displays the first level.... any help is appreciated.

        public function getBonusChildren($userID)
        {
	// retrieve all children of $parent
	$sql = 'SELECT userID FROM jos_backoffice_users WHERE parentID ="'. $userID .'";';
	$stmt = conn::getInstance()->prepare($sql);
	$stmt->execute();

        	while($row = $stmt->fetchObject())
	{
                	$memStatus = checkUserMemStatus($row->userID);

                	if($memStatus == true)
                	{
                    		$memberCounter++;
			$this->getBonusChildren($row->userID);
                	}
	}
	return $memberCounter;
}

Link to comment
Share on other sites

I don't understand why you're going to so much trouble for a count() of users? You can do it with the query you had already, minus the where condition:

 

SELECT count(userID) FROM jos_backoffice_users

 

Slightly off-topic..

 

Just to let you know, the way you're using prepared statements is wrong. The variables in a statement should be bound to the statement object separately, and then executed. This means you can then re-execute or bind new variables to the same statement, re-using the server's execution plan with a huge improvement in efficiency. The way you're doing it right now is constructing a new statement for each unique query, completely bypassing the point.

Link to comment
Share on other sites

I think this will do what you want.

 

public function getBonusChildren($userID)
{
    // retrieve all children of $parent
    $sql = <<<SQL
SELECT userID, (SELECT count(userID) FROM jos_backoffice_users T2 WHERE T2.parentID = T1.userID) child_count
  FROM jos_backoffice_users T1
-- WHERE mem_status = ?
SQL;
    $rows = conn::getInstance()->query($sql)->fetchAll();
    return $rows;
}

Link to comment
Share on other sites

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.