PHP计算二叉树下线数 [英] PHP Calculate Number of downline in binary tree
问题描述
为了计算二叉树中的下线数量,我正在尝试通过创建 2 个用于注册和成员下线结构的数据库来执行以下脚本.它实际上有效.
For calculating numbers of downline in binary tree, I am trying the following script by making 2 databases for registration and members downline structure. It works actually.
但是成员结构数据库增长非常快.导致二叉树中的n级会为单用户注册生成n条记录.我只是想知道如果用户在 1000 级注册,那么它会在单用户注册中创建 1000 条记录.
But the members structure database grow very fast. Caused n level in binary tree will generate n records for single user registration. I am just wondering if user register at level 1000 then it will create 1000 record in single user registration.
该系统还有其他解决方案吗?
Any other solution for this system?
完整的长脚本是:
CREATE TABLE IF NOT EXISTS `member` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`username` varchar(55) CHARACTER SET utf8 NOT NULL,
`upline` varchar(55) CHARACTER SET utf8 NOT NULL,
`position` varchar(10) NOT NULL,
`sponsor` varchar(55) CHARACTER SET utf8 NOT NULL,
`_left` varchar(55) CHARACTER SET utf8 NOT NULL,
`_right` varchar(55) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
制作下线结构表:
CREATE TABLE IF NOT EXISTS `net_downline` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(60) NOT NULL,
`upline` varchar(60) NOT NULL,
`level` int(7) NOT NULL,
`position` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
从简单或基本的注册表中获取:
As get from simple or basic registration form :
$newuser = htmlentities(trim($_POST['user']));
$sponsor = htmlentities(trim($_POST['sponsor']));
$upline = htmlentities(trim($_POST['upline']));
$position = htmlentities(trim($_POST['position']));
在注册过程中,执行此树步骤:
During registration process, this tree steps is executed :
// 1. register new member
$users->dummyRegister($newuser, $sponsor, $upline, $position);
// 2. update upline
$users->dummyUpdateUpline($newuser, $upline, $position);
// 3. create donwline structure of binary tree
$level=0;
$memberid=$newuser;
do{
$getdata=$users->dummyGetUpline($memberid);
$uplne=$getdata[2];
$posi=$getdata[3];
$level++;
if($uplne!==''){
$users->dummyInsert_NetDownline($newuser, $uplne, $posi, $level);
}
$memberid=$uplne;
}
while($memberid!='');
该脚本的用户"类:
<?php // start class
class Users{
private $db;
public function __construct($database) {
$this->db = $database;
}
public function dummyRegister($username, $sponsor, $upline, $position, $today){
$query = $this->db->prepare("INSERT INTO `member` (`username`, `sponsor`, `upline`, `position`, `entry_date` ) VALUES (?, ?, ?, ?, ?) ");
$query->bindValue(1, $username);
$query->bindValue(2, $sponsor);
$query->bindValue(3, $upline);
$query->bindValue(4, $position);
$query->bindValue(5, $today);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
public function dummyUpdateUpline($username, $upline, $position){
if ($position=='left') {
$query = $this->db->prepare("UPDATE `member` SET `_left`=? WHERE username=? ");
}elseif ($position=='right') {
$query = $this->db->prepare("UPDATE `member` SET `_right`=? WHERE username=? ");
}
$query->bindValue(1, $username);
$query->bindValue(2, $upline);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
public function dummyGetUpline($newuser) {// for demo
$query = $this->db->prepare("SELECT * FROM `member` WHERE `username`= ?");
$query->bindValue(1, $newuser);
try{
$query->execute();
$rows = $query->fetch();
return $rows;//['upline'];
} catch(PDOException $e){
die($e->getMessage());
}
}
public function dummyInsert_NetDownline($newuser, $upline, $posi, $level){// for demo
$query = $this->db->prepare("INSERT INTO `net_downline` (`username`, `upline`, `position`, `level` ) VALUES (?, ?, ? ,?) ");
$query->bindValue(1, $newuser);
$query->bindValue(2, $upline);
$query->bindValue(3, $posi);
$query->bindValue(4, $level);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
}// endclass
创建initial.php并放在顶部注册php脚本:
create initial.php and put on top regitration php script :
<?php
if(!isset($_SESSION)) { session_start(); }
require 'conn/database.php'; // in folder conn
require 'clas/users.php'; // in folder class
$users = new Users($db);
}
?>
用于处理数据库连接(class/database.php 脚本):
this to handle database connection (clas/database.php script) :
<?php
$config = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'sampledatabase'
);
$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
推荐答案
这是我尝试了很多方法后的答案.解决我之前的问题.仅使用上面的单个成员表.
This is the answer after I try many ways. Solving my previous problem. Just using a single member table above.
显示左下线和右下线的数量.我将此脚本插入到树 A 中每个用户的 HTML 成员树页面中,下至 B/C,下至 D/E/F/G):
To show number of downlines, left and right. I insert this script in the HTML member tree page for every user in the tree A, down to B/C, down to D/E/F/G ):
<?php echo $users->downline_number($member,'_left'); ?>
<?php echo $users->downline_number($member,'_right'); ?>
在用户类中添加这个函数;
Add this function in User Class ;
function downline_number($member,$position) {
$query = $this->db->prepare("SELECT * FROM `member` WHERE `upline`='$member' AND `position`='$position'");
$query->bindValue(1, $member);
$query->bindValue(2, $position);
try{
$query->execute();
$rows = $query->fetch();
if($this->count_downline($member,$position) >0 ){
$total=$this->total_members_down($rows['username']);
}else{
$total=0;
}
return $total;
}catch(PDOException $e){
die($e->getMessage());
}
}
function count_downline($member,$position) {
$query = $this->db->prepare("SELECT * FROM `member` WHERE `upline`=? AND `position`=? ");
$query->bindValue(1, $member);
$query->bindValue(2, $position);
try{
$query->execute();
return $rows = $query->rowCount();
}catch(PDOException $e){
die($e->getMessage());
}
}
function total_members_down($upline,$reset=0) {
global $num;
if ($reset==0) { $num=1; }
$query = $this->db->prepare("SELECT * FROM `member` where `upline`='$upline' order by id asc");
$query->bindValue(1, $upline);
try{
$query->execute();
if ($upline !='') {
if ($this->total_down($upline) > 0 ) {
while ($rows = $query->fetch() ) {
$num++;
$this->total_members_down($rows['username'],$num);
}
return $num;
} else {
return $num;
}
} else { $num=0; return $num; }
}catch(PDOException $e){
die($e->getMessage());
}
}
function total_down($upline) {
$query = $this->db->prepare("SELECT * FROM `member` where `upline`='$upline' order by id asc ");
$query->bindValue(1, $upline);
try{
$query->execute();
return $rows = $query->rowCount();
}catch(PDOException $e){
die($e->getMessage());
}
}
它可以显示二元成员树结构.此处不附加显示 memberID,方法简单.刚离开 &正确的下线号码.
and it works showing binary member tree structure. Showing the memberID is not attached here, caused it simple way. Just left & right downlines number.
希望这篇文章能帮助到需要它的人.对更好的方法有什么建议吗?
Hope this post will help others who need it. Any suggestion for better ways?
这篇关于PHP计算二叉树下线数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!