按状态计数ID与日期范围

发布于 2025-02-01 12:38:35 字数 2682 浏览 3 评论 0原文

因此,我想通过创建,分配,交付&取消。因此,我开始通过按日期范围过滤从SQL获取数据。但是,当我试图通过区分状态进行计数时,结果仅计算全部不考虑状态。我的错误在哪里?我找不到。帮助我,所以下面是我的代码,结果表明这样。

创建的总任务=警告:c:\ xampp \ htdocs \ tms \ tms \ des_rpt.php in Line 98 (应为0)

总待定任务=警告:c:\ xampp \ htdocs \ tms \ tms \ des_rpt.php in Line 99 (应为1)

总任务= 4(应为2)

总数取消=警告:c:\ xampp \ htdocs \ tms \ tms \ tms \ des_rpt.php在第101行上 (应该是1)

'''

<?php
//filter by date range
print "<form name=history>";
print "<table><tr>";
print "<td><b>From :</b></td>";
print "<td><b>To :</b></td></tr>";
print "<tr><td><input type=date name=tarikh1 value=\"$tarikh1\"></td>";
print "<td><input type=date name=tarikh2 value=\"$tarikh2\"><input type=hidden name=action value=cari1></td>";
print "<td><input type=submit value=SUBMIT></td>";
print "</tr></table></form>";
print " <br/>\n";

$sql1 = "SELECT * FROM task WHERE dateupdate >= '$tarikh1' AND dateupdate <= '$tarikh2'";
$a = 0;
$result1 = mysqli_query($conn, $sql1);
if(mysqli_num_rows($result1) > 0)
{
while($row1 = mysqli_fetch_array($result1))
{
    $id[] = $row1[1];
    $from[] = $row1[5];
    $to[] = $row1[6];
    $cdate[] = $row1[9];
    $ajen[] = $row1[11];
    $stat[] = $row1[13];
    $udate[] = $row1[14];
    $utime[] = $row1[15];
    $a++;
}
mysqli_free_result($result1);
} 

if (!empty($id))
{
 $tid=$id;
    foreach (array_unique($ajen) as $agent){
    $b = 0;

       for ($idk = 0; $idk < $a; $idk++)
       {
        if ($agent == $ajen[$b])
        {
            if ($stat[$b] == 'ASSIGNED'){
                $cntasg = count($tid);
            }

            if ($stat[$b] == 'CREATED'){
                $cntcreate = count($tid);
            }

            if ($stat[$b] == 'DELIVERED'){
                $cntdel = count($tid);
            }

            if ($stat[$b] == 'CANCEL'){
                $cntccl = count($tid);
            }
        }
       }


    } ?>

<p><b><font face=verdana>Total Task Created = <?php echo $cntcreate;  ?></font></b></p>
<p><b><font face=verdana>Total Pending Task = <?php echo $cntasg;  ?></font></b></p>
<p><b><font face=verdana>Total Task Delivered = <?php echo $cntdel;  ?></font></b></p>
<p><b><font face=verdana>Total Task Cancelled = <?php echo $cntccl;  ?></font></b></p>

<?php
}else{
    echo "<h1>-NO RELATED TASK ON THIS DATE!-</h1>";
}?>

So I wanna know the total of each task by it's status which is CREATED, ASSIGNED, DELIVERED & CANCEL. SO I started to get the data from SQL by filtering by date range. however when I tried to count by differentiate it's status, the result only count all without considering the status. Where is my mistake? I could't find it. Help me, so below is my codes and the result show like this.

Total Task Created = Warning: Undefined variable $cntcreate in C:\xampp\htdocs\tms\des_rpt.php on line 98
(should be 0)

Total Pending Task = Warning: Undefined variable $cntcreate in C:\xampp\htdocs\tms\des_rpt.php on line 99
(should be 1)

Total Task Delivered = 4 (should be 2)

Total Task Cancelled = Warning: Undefined variable $cntcreate in C:\xampp\htdocs\tms\des_rpt.php on line 101
(should be 1)

'''

<?php
//filter by date range
print "<form name=history>";
print "<table><tr>";
print "<td><b>From :</b></td>";
print "<td><b>To :</b></td></tr>";
print "<tr><td><input type=date name=tarikh1 value=\"$tarikh1\"></td>";
print "<td><input type=date name=tarikh2 value=\"$tarikh2\"><input type=hidden name=action value=cari1></td>";
print "<td><input type=submit value=SUBMIT></td>";
print "</tr></table></form>";
print " <br/>\n";

$sql1 = "SELECT * FROM task WHERE dateupdate >= '$tarikh1' AND dateupdate <= '$tarikh2'";
$a = 0;
$result1 = mysqli_query($conn, $sql1);
if(mysqli_num_rows($result1) > 0)
{
while($row1 = mysqli_fetch_array($result1))
{
    $id[] = $row1[1];
    $from[] = $row1[5];
    $to[] = $row1[6];
    $cdate[] = $row1[9];
    $ajen[] = $row1[11];
    $stat[] = $row1[13];
    $udate[] = $row1[14];
    $utime[] = $row1[15];
    $a++;
}
mysqli_free_result($result1);
} 

if (!empty($id))
{
 $tid=$id;
    foreach (array_unique($ajen) as $agent){
    $b = 0;

       for ($idk = 0; $idk < $a; $idk++)
       {
        if ($agent == $ajen[$b])
        {
            if ($stat[$b] == 'ASSIGNED'){
                $cntasg = count($tid);
            }

            if ($stat[$b] == 'CREATED'){
                $cntcreate = count($tid);
            }

            if ($stat[$b] == 'DELIVERED'){
                $cntdel = count($tid);
            }

            if ($stat[$b] == 'CANCEL'){
                $cntccl = count($tid);
            }
        }
       }


    } ?>

<p><b><font face=verdana>Total Task Created = <?php echo $cntcreate;  ?></font></b></p>
<p><b><font face=verdana>Total Pending Task = <?php echo $cntasg;  ?></font></b></p>
<p><b><font face=verdana>Total Task Delivered = <?php echo $cntdel;  ?></font></b></p>
<p><b><font face=verdana>Total Task Cancelled = <?php echo $cntccl;  ?></font></b></p>

<?php
}else{
    echo "<h1>-NO RELATED TASK ON THIS DATE!-</h1>";
}?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生死何惧 2025-02-08 12:38:35

如果 elseif ,则需要替换所有

if ($stat[$b] == 'ASSIGNED'){
  $cntasg = count($tid);
}elseif ($stat[$b] == 'CREATED'){
  $cntcreate = count($tid);
}elseif ($stat[$b] == 'DELIVERED'){
  $cntdel = count($tid);
}elseif ($stat[$b] == 'CANCEL'){
  $cntccl = count($tid);
}

You need to replace all your if with elseif.

if ($stat[$b] == 'ASSIGNED'){
  $cntasg = count($tid);
}elseif ($stat[$b] == 'CREATED'){
  $cntcreate = count($tid);
}elseif ($stat[$b] == 'DELIVERED'){
  $cntdel = count($tid);
}elseif ($stat[$b] == 'CANCEL'){
  $cntccl = count($tid);
}
梦太阳 2025-02-08 12:38:35

我对您的查询进行了一些更改,请检查列名&amp;如果需要进行更改。让我知道您是否在查询中遇到任何问题。

<?php
//filter by date range
print "<form name=history>";
print "<table><tr>";
print "<td><b>From :</b></td>";
print "<td><b>To :</b></td></tr>";
print "<tr><td><input type=date name=tarikh1 value=\"$tarikh1\"></td>";
print "<td><input type=date name=tarikh2 value=\"$tarikh2\"><input type=hidden name=action value=cari1></td>";
print "<td><input type=submit value=SUBMIT></td>";
print "</tr></table></form>";
print " <br/>\n";

$sql1 = "SELECT SUM(CASE WHEN stat = 'ASSIGNED' THEN 1 ELSE 0 END) AS ASSIGNED, SUM(CASE WHEN stat = 'CREATED' THEN 1 ELSE 0 END) AS CREATED, SUM(CASE WHEN stat = 'DELIVERED' THEN 1 ELSE 0 END) AS DELIVERED, SUM(CASE WHEN stat = 'CANCEL' THEN 1 ELSE 0 END) AS CANCEL FROM task WHERE dateupdate >= '$tarikh1'  AND created < dateupdate <= '$tarikh2'";

$result1 = mysqli_query($conn, $sql1);

if(mysqli_num_rows($result1) > 0) {

    $row1 = mysqli_fetch_array($result1)
    $cntcreate = $row1[1];
    $cntasg = $row1[0];
    $cntdel = $row1[2];
    $cntccl = $row1[3];

    echo "<p><b><font face=verdana>Total Task Created = $cntcreate </font></b></p>";
    echo "<p><b><font face=verdana>Total Pending Task = $cntasg </font></b></p>";
    echo "<p><b><font face=verdana>Total Task Delivered = $cntdel </font></b></p>";
    echo "<p><b><font face=verdana>Total Task Cancelled = $cntccl </font></b></p>";
}
else {
    echo "<h1>-NO RELATED TASK ON THIS DATE!-</h1>";
}
?>

I did some changes in your query, check the column name& make change if required. Let me know if you getting any issue with the query.

<?php
//filter by date range
print "<form name=history>";
print "<table><tr>";
print "<td><b>From :</b></td>";
print "<td><b>To :</b></td></tr>";
print "<tr><td><input type=date name=tarikh1 value=\"$tarikh1\"></td>";
print "<td><input type=date name=tarikh2 value=\"$tarikh2\"><input type=hidden name=action value=cari1></td>";
print "<td><input type=submit value=SUBMIT></td>";
print "</tr></table></form>";
print " <br/>\n";

$sql1 = "SELECT SUM(CASE WHEN stat = 'ASSIGNED' THEN 1 ELSE 0 END) AS ASSIGNED, SUM(CASE WHEN stat = 'CREATED' THEN 1 ELSE 0 END) AS CREATED, SUM(CASE WHEN stat = 'DELIVERED' THEN 1 ELSE 0 END) AS DELIVERED, SUM(CASE WHEN stat = 'CANCEL' THEN 1 ELSE 0 END) AS CANCEL FROM task WHERE dateupdate >= '$tarikh1'  AND created < dateupdate <= '$tarikh2'";

$result1 = mysqli_query($conn, $sql1);

if(mysqli_num_rows($result1) > 0) {

    $row1 = mysqli_fetch_array($result1)
    $cntcreate = $row1[1];
    $cntasg = $row1[0];
    $cntdel = $row1[2];
    $cntccl = $row1[3];

    echo "<p><b><font face=verdana>Total Task Created = $cntcreate </font></b></p>";
    echo "<p><b><font face=verdana>Total Pending Task = $cntasg </font></b></p>";
    echo "<p><b><font face=verdana>Total Task Delivered = $cntdel </font></b></p>";
    echo "<p><b><font face=verdana>Total Task Cancelled = $cntccl </font></b></p>";
}
else {
    echo "<h1>-NO RELATED TASK ON THIS DATE!-</h1>";
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文