gpt4 book ai didi

php - 基于行值的 rowCount

转载 作者:行者123 更新时间:2023-11-29 11:42:41 25 4
gpt4 key购买 nike

所以我正在尝试这样做..

    $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 语句,它的计数数字都是 2,而每个部分中只有 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>";
}
}

关于php - 基于行值的 rowCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com