我是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 或其赞助商/推荐人,以及 位置的位置是lft(左)和rgt(右)。
我打算做的是,我想向用户(或childID)展示他们的用户 总左(左)和右(rgt)下线。
例如,我想从左和右获取childID(First)的所有下划线的总和 基于我的表结构并将其显示给用户(第一个)。
希望有人可以帮我解决这个问题。提前谢谢。
答案 0 :(得分:1)
我认为这可以通过修改脚本来解决问题。这将计算成员树二叉树中左右下线的数量。
制作一张注册会员表:
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!='');
class" user"对于那个剧本:
<?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并置于你的regitration 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条记录。
愿这里有人拥有最好的系统。请指教。