我是 PHP 的新学习者,我正在修改一个简单的二进制站点。 我来这里寻求帮助,我如何计算我的下线对或总的左侧和右侧 一些用户。
这是我在数据库中的表的示例:
+++++++++++++++++++++++++++++++++
| childID | parentID | position +
+++++++++++++++++++++++++++++++++
| Admin | NULL | NULL +
| First | Admin | lft +
| Second | Admin | rgt +
| Third | First | lft +
| Fourth | First | rgt +
| Fift | Second | lft +
| Six | Second | rgt +
| Sevent | Third | lft +
| Eight | Third | rgt +
| Ninth | Fourth | lft +
| Tenth | Fifth | rgt +
我有三个表,其中包含 childID 或其用户名、parentID 或其赞助商/推荐人,以及 position 表示他们的位置,即 lft(左)和 rgt(右)。
我打算做的是,我想向用户(或 childID)展示他们的 左(lft) 和右(rgt) 下线总数。
比如我想获取childID(First)左右所有下线的总和 基于我的表结构并将其显示给用户(第一)。
希望有人能帮助我解决这个问题。提前致谢。
最佳答案
我认为这可以通过修改你的脚本来解决。这将计算成员树二叉树中左右下线的数量。
制作成员(member)注册表:
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 ;
从简单或基本注册表中获取:
$newuser = htmlentities(trim($_POST['user']));
$sponsor = htmlentities(trim($_POST['sponsor']));
$upline = htmlentities(trim($_POST['upline']));
$position = htmlentities(trim($_POST['position']));
在此过程中,应完成此树步骤:
// 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 // statr 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 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 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脚本的顶部:
<?php
if(!isset($_SESSION)) { session_start(); }
require 'conn/database.php'; // in folder conn
require 'clas/users.php'; // in folder class
$users = new Users($db);
}
?>
这用于处理数据库连接(clas/database.php 脚本):
<?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);
从这里您可以看到基于二叉树的上线的成员资格结构。通过统计与用户匹配的查询即可获得下线数量。
缺点是net_downline数据库会快速增长,导致系统将为任何新注册评估并重新安排结构。对于 level(n) 下线级别,它会更新并为单个新用户创建 n 条记录。
愿这里有人拥有最好的系统。请指教。
关于php - 如何计算并显示二进制上的所有左子和右子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22927780/