2017-08-01 65 views
-1

我非常需要計算每個級別的每個成員我有15個級別的深度佣金模型,因此我能夠計算第一級但不能夠統計整個團隊都有一個人在他/她的下線。我想統計每個級別的成員數量。 喜歡1級10位成員,第2級55位成員,第3級35位成員等。 我需要計算一級,二級...到15級的家長ID的全部成員數。 我可以,如果你需要任何澄清,請評論,我將解釋在第一級來算像如何計算每個級別的15級深度php

$result = mysqli_query($con, "SELECT count(*) FROM affiliateuser where referedby = '" . $_SESSION['username'] . "' AND active = 1"); 

。 我在這裏分享我的第一個2級代碼。請檢查下面的代碼。

開始訪下線用戶的第一級

$totalref = 0; 
$totalrefear = 0; 
$query = "SELECT fname,email,doj,active,username,level,pcktaken FROM affiliateuser where referedby = '" . $_SESSION['username'] . "'"; 
$result = mysqli_query($con, $query); 
while($row = mysqli_fetch_array($result)) { 
    $ac = "$row[active]"; 
    $countusername = "$row[username]"; 
    $countuserlevel = "$row[level]"; 
    $pcktook = "$row[pcktaken]"; 
} 

開始訪下線用戶的第二級

$totalrefear = 0; 
$query = "SELECT fname,email,doj,active,level,username FROM affiliateuser where referedby = '" . $_SESSION['username'] . "'"; 
$result = mysqli_query($con, $query); 
while($row = mysqli_fetch_array($result)) { 
    $ac = "$row[active]"; 
    $countusername = "$row[username]"; 
    $countuserlevel = "$row[level]"; 
    $query2level = "SELECT fname,email,doj,active,level,username,pcktaken FROM affiliateuser where referedby = '$countusername'"; 
    $result2level = mysqli_query($con, $query2level); 
    while($row2 = mysqli_fetch_array($result2level)) { 
     $ac2 = "$row2[active]"; 
     $countusername2 = "$row2[username]"; 
     $countuserlevel2 = "$row2[level]"; 
     $pcktook = "$row2[pcktaken]"; 
    } 
} 

我與這個查詢試圖在計算用戶父母的第二級別。但它讓我看到了整個數據庫用戶。我只想計算二級用戶。請問你的身體能幫我解決嗎?

$queryridd =mysqli_query($con, "SELECT COUNT(Id) AS countt, level AS Lebel from affiliateuser WHERE level = '$countuserlevel' GROUP BY level"); 
+2

一些明智的代碼縮進將是一個好主意。 它可以幫助我們閱讀代碼,更重要的是它可以幫助您調試代碼**。 [快速瀏覽編碼標準](http://www.php-fig.org/psr/psr-2/)爲您帶來的好處。 您可能會被要求在幾周/幾個月內修改此代碼,最後您會感謝我。 – GrumpyCrouton

+4

[Little Bobby](http://bobby-tables.com/)說** [您的腳本存在SQL注入攻擊風險](http://stackoverflow.com/questions/60174/how-cani-i-防止-SQL注入功能於PHP)**。瞭解[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)的[Prepared Statements](準備語句)(http://en.wikipedia.org/wiki/Prepared_statement)。即使** [轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)**是不安全的! – GrumpyCrouton

+0

您的預期產出與您的實際產出有什麼關係? – GrumpyCrouton

回答

0

請使用if出發到另一個層次

<?php 

$query = "SELECT fname,email,doj,active,level,username FROM affiliateuser where referedby = '" . $_SESSION['username'] . "'"; 
$result = mysqli_query($con, $query); 
while($row = mysqli_fetch_array($result)) { 
    $ac = "$row[active]"; 
    $countusername = "$row[username]"; 
    $countuserlevel = "$row[level]"; 
    // use if to check the level 
    if($countuserlevel == 2) { // use if to check the level 

     $query2level = "SELECT fname,email,doj,active,level,username,pcktaken FROM affiliateuser where referedby = '$countusername'"; 
     $result2level = mysqli_query($con, $query2level); 
     while($row2 = mysqli_fetch_array($result2level)) { 
      $ac2 = "$row2[active]"; 
      $countusername2 = "$row2[username]"; 
      $countuserlevel2 = "$row2[level]"; 
      $pcktook = "$row2[pcktaken]"; 
     } 

     $queryridd = mysqli_query($con, "SELECT COUNT(Id) AS countt, level AS Lebel from affiliateuser WHERE level = '$countuserlevel' GROUP BY level"); 
     while($rowbb = mysqli_fetch_array($queryridd)) { 
      $countingUsers2 = "$rowbb[0]"; 
     } 

    } 
} // use if to check the level 
?> 
<td class='text-left'><?php echo $countingUsers2; ?></td> 
+1

改變教學/宣傳草率和危險的編碼習慣。 如果您發佈的答案沒有準備好的陳述[您可能想在發佈之前考慮這一點](http://meta.stackoverflow.com/q/344703/)。 此外[更有價值的答案來自顯示OP的正確方法](https://meta.stackoverflow.com/a/290789/1011527)。 – GrumpyCrouton

+1

[Little Bobby](http://bobby-tables.com/)說** [您的腳本存在SQL注入攻擊風險](http://stackoverflow.com/questions/60174/how-cani-i-防止-SQL注入功能於PHP)**。瞭解[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)的[Prepared Statements](準備語句)(http://en.wikipedia.org/wiki/Prepared_statement)。即使** [轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)**是不安全的! – GrumpyCrouton

+0

好的,但問題沒有解決,我想統計每個級別的成員。例如,您登錄後,儀表板中有15個級別。所以我想要統計15個級別的每個級別的成員。基本上,我正在創建傳銷軟件。我已經分享了前兩個級別的代碼。請檢查 –

0

假設你有一個PARENT_ID場之前檢查水平,你算多少記錄已PARENT_ID等於你看一。爲了獲得下一個級別,您只需重複剛剛獲得的每條記錄。

現在...你怎麼做到這一點?

首先,您的數據庫需要正確計劃。從simple parent - child using recursion, to materialized path, to nested sets有很多方法可以製作樹結構。各有優點和缺點。我個人喜歡物化路徑;但是對於你的應用程序來說樹的深度會使父母孩子的變化更可能。

那麼數據庫結構是什麼樣的呢?

Superheroes 
---------- 
id   <--\ 
parent_id ---/ 
name 

所以,如果你有一個層次,像這樣:

      1 Steven Rogers 
         /   \ 
        2 Bruce Banner  3 Wally West 
       /  \ 
     4 Peter Parker  5 Jay Garrick 
     /  \ 
6 Barry Allen 7 Reed Richards 

您的表看起來像這樣:

 superheroes 
     ----------- 
id parent_id name 
1 0   Steven Rogers 
2 1   Bruce Banner 
3 1   Wally West 
4 2   Peter Parker 
5 2   Jay Garrick 
6 4   Barry Allen 
7 4   Reed Richards 

找到誰是正下方史蒂夫·羅傑斯,

$sth = $dbh->prepare("select id,name from superheroes where parent_id=?"); 
$sth->execute(array(1)); 
$result = $sth->fetchAll(); 

要找到直接在P之下的人eter Parker,

$sth = $dbh->prepare("select id,name from superheroes where parent_id=?"); 
$sth->execute(array(4)); 
$result = $sth->fetchAll(); 

太棒了,就是一級。我如何獲得下一個級別?這是一個功能的工作。(實際上,一類會更好,因爲範圍是這麼容易對付。)

class CountDownLine { 

    public function __construct($database) { 

     $this->db = $database; 
    } 

    public function getDownline($id, $depth=2) { 

     $stack = array($id); 
     for($i=1; $i<=$depth; $i++) { 

      // create an array of levels, each holding an array of child ids for that level 
      $stack[$i] = $this->getChildren($stack[$i-1]); 
     } 

     return $stack; 

    } 

    public function countLevel($level) { 

     // expects an array of child ids 
     settype($level, 'array'); 

     return sizeof($level); 
    } 

    private function getChildren($parent_ids = array()) { 

     $result = array(); 
     $placeholders = str_repeat('?,', count($parent_ids) - 1). '?'; 

     $this->$db->prepare("select id from superheroes where parent_id in ($placeholders)"); 
     $this->$db->->execute(array($parent_ids)); 

     while($row=$this->$db->fetch()) { 

      $results[] = $row->id; 
     } 

     return $results; 
    } 

} 

下面是它如何工作的:

$DL = new CountDownLine(new Database); // inject your favorite database abstraction 

$id = 1; // ie, id of Steve Rogers 
$depth = 2; // get the counts of his downline, only 2 deep. 

$downline_array = $DL->getDownline($id, $depth=2); 

// => $downline_array[1] = [2,3] 
// => $downline-array[2] = [4,5] 

$count_of_first_level = $DL->countLevel($downline_array[1]); 
$count_of_second_level = $DL->countLevel($downline_array[2]); 

注:這是展示概念。我沒有在數據庫上測試它。你必須使它適應你的數據庫訪問方法。