2016-02-19 66 views
0

所以我試圖做到這一點..rowCount時基於行值

$userID = $_SESSION['user_session']; 
    $stmt = $this->db->prepare("SELECT * FROM orrs"); 
    $stmt->bindparam(":id", $userID); 
    $stmt->execute(); 
    $count = $stmt->rowCount(); 


echo 
"<div class='table-responsive'> 
<table class='table' border='1'> 
    <tr class='head'> 
    <h3>Snapshot</h3> 
    <th>Se</th> 
    <th>#s</th> 
    <th>Ae</th> 
    <th>Prt</th> 
    <th>Pin</th> 


    </tr>"; 


     while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) { 


     if($userRows['stage'] == '1') 
     { 
     echo "<tr>"; 
     echo "<td>" . "Newn" . "</td>"; 
     echo "<td>" . $count . "</td>"; 
     echo "<td>" . $userRows['aow'] . "</td>"; 
     echo "<td>" . $userRows['pit'] . "</td>"; 
     echo "<td>" . $userRows['pgin'] . "</td>"; 


     } 
     else if($userRows['stage'] == '2') 
     { 

     echo "<tr>"; 
     echo "<td>" . "Pendinn" . "</td>"; 
     echo "<td>" . $count . "</td>"; 
     echo "<td>" . $userRows['gfh'] . "</td>"; 
     echo "<td>" . $userRows['pt'] . "</td>"; 
     echo "<td>" . $userRows[trin'] . "</td>"; 
     } 



    } 

基本上,如果在該行STAGE = 1我想它來計算這些行,給我多少價值。如果STAGE = 2的值我希望它計算這些行並給我數字。

現在,它只是計算的所有行。所以兩者的IF的statment其計數是說2,當僅存在在每節1 ..

回答

1

我認爲你需要執行三個不同的報表,一個(在你循環並創建輸出)獲得所有的行和一個讓每個計數

//The current one 
$stmt = $this->db->prepare("SELECT * FROM orrs"); 

//The get the count for stage '1' 
$stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1"); 
$stage_1_count = $stage_1_stmt->rowCount(); 

//The get the count for stage '2' 
$stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2"); 
$stage_2_count = $stage_2_stmt->rowCount(); 

,你可以執行這些別人得到你應該使用的計數代替循環中的$ count。

while循環就變成

while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) { 


    if($userRows['stage'] == '1') 
    { 
    echo "<tr>"; 
    echo "<td>" . "Newn" . "</td>"; 
    echo "<td>" . $stage_1_count . "</td>"; 
    echo "<td>" . $userRows['aow'] . "</td>"; 
    echo "<td>" . $userRows['pit'] . "</td>"; 
    echo "<td>" . $userRows['pgin'] . "</td>"; 


    } 
    else if($userRows['stage'] == '2') 
    { 

    echo "<tr>"; 
    echo "<td>" . "Pendinn" . "</td>"; 
    echo "<td>" . $stage_2_count . "</td>"; 
    echo "<td>" . $userRows['gfh'] . "</td>"; 
    echo "<td>" . $userRows['pt'] . "</td>"; 
    echo "<td>" . $userRows[trin'] . "</td>"; 
    } 
} 
+0

正如你看到的,雖然,Im做while循環。我不能做3個不同的while循環或它不會正確工作。 – Kmiles1990123

+0

您可以使用相同的循環,我會更新我的答案 – Robin

+0

完美工作,感謝您的幫助! – Kmiles1990123