计数表结果:使用单选按钮值的 PHP 开关案例

2022-01-19 00:00:00 switch-statement php

我编辑了帖子以便更好地理解.这是我作为学生实习生的第一个项目.它是一种设备监控系统,用于保存计算机设备的记录.这是包含过滤选项和显示计算机设备列表的表格的页面中的代码的一部分.过滤选项包含几个单选按钮,它们属于两个类别,即状态和条件.请参阅帖子末尾的两个类别的组成部分.组件是单选按钮的值.

I edited the post for a better understanding. This is my first project as a student-trainee. It is an equipment monitoring system that keeps a record of computer equipment. This is part of code in a page that contains a filtering option and a table that displays a list of computer equipment. The filtering option contains several radio buttons that belongs to two categories which are State and Condition. Please see the components of the two categories at the end of the post. The components are the radio button values.

当单击另一个单选按钮时页面会重新加载,类似于 Facebook 搜索.我希望计算每个条件和状态的设备数量,以显示在单选按钮标签旁边.但是,状态单选按钮会影响设备的计数.例如,New EQ 是刚刚进入库存的设备类型.选中新的EQ按钮并检查可用/未分配的按钮时,将过滤表格结果,将显示包含具有新的状态的结果以及可用/未分配的条件.表格结果将被计算并输出在单选按钮标签旁边,该标签告诉用户有多少设备是新"和可用/未分配".当点击其他状态单选按钮时,例如Old EQ按钮,标签旁边显示的可用/未分配设备的数量将根据返回的行数而变化,因为用户将请求更改为输出设备"旧"和可用/未分配".顺便说一句,状态和条件是同一个表中的列.这将发生在所有单选按钮上.如果用户单击 Condition 类别下的另一个单选按钮,例如 Assigned EQ 按钮,则标签旁边显示的 Available/Unassigned 的数量将变为 0 或 0,因为用户请求表结果应显示新且已分配的设备,未请求可用/未分配的设备.如果用户点击所有条件"按钮,它将计算包含ALL条件的表格行,其行上也有数据a New状态.显示表格结果的过滤过程已经在运行.我现在正在输出单选按钮标签旁边的数量.

The page reloads when another radio button is clicked similar to Facebook search. I wanted to count the number of equipment per Condition and State to be displayed beside the radio button label. BUT, the state radio buttons affect the counting of equipment. For example, New EQ is the type of equipment that was just entered into the inventory. When the New EQ button is checked and Available/Unassigned button is also checked, the table result will be filtered that will display results that contain equipment with a state that is New and a condition that is Available/Unassigned. The table result will be counted and will be outputted beside the radio button label which tells the user how many equipment are "New" and "Available/Unassigned". When another state radio button is clicked, such as Old EQ button, The displayed quantity of the Available/Unassigned equipment beside the label will change depending on the number of rows returned because the user changed the request to output equipment that is "Old" and "Available/Unassigned". BTW the state and condition are columns in the same table. This will happen to all the radio buttons. If the user clicks another radio button under the Condition category, such as the Assigned EQ button, the quantity of the Available/Unassigned displayed beside the label will change to 0 or zero because the user requested that the table result should display equipment that are New and Assigned, the equipment that is Available/Unassigned was not requested. If the user clicks "All condition" button, it will count the table rows that contain ALL the conditions which also has a data on its row a New state. The filtering process to display table results is already functioning. I am now working on outputting the quantities beside the radio button labels.

<?php

switch($state AND $condition){
    case $state=="allstate" AND $condition=="allcondition":
        $sql3="SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned'";
        break;

    case $state=="new" AND $condition=="allcondition":
        $sql3="SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='new'";
        break;

    case $state=="old" AND $condition=="allcondition":
        $sql3="SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='old'";
        break;

    case $state=="Unknown state" AND $condition=="allcondition":
        $sql3="SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='Unknown state'";
        break;

    case $state=="Unknown state" AND $condition=="Available/Unassigned":
        $sql3="SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='Unknown state'";
        break;
}
         //Code above is incomplete
$result3=mysqli_query($conn,$sql3);
$count=mysqli_num_rows($result3);
echo "<label style='color:red;'><strong>".$count."</strong></label>";

?>

分类:

状态:所有状态,新,旧,未知状态

State: All state,new,old,unknown state

状况:所有状况、可用/未分配、维修、缺少零件、缺少 eq、有缺陷、翻新、未知状况

Condition: All condition,available/unassigned,repair,missing parts, missing eq, defective, refurbished,unknown condition

推荐答案

switch($state AND $condition) 将根据值评估为真或假 $state$condition.

switch($state AND $condition) will evaluate to true or false depending of the values $state and $condition.

所以唯一有用的情况是

  • 情况为真
  • 案例错误
  • 默认情况

在您的情况下,您应该使用 if/else 构造.

In your case you should use if / else construct.

因为在评论中询问如何在 switch 构造中编写条件,请在此处查看嵌套代码:

Because asked in the comments how to write the conditions anyway in a switch construct see the nested code here:

switch ($state)
{
    case "allstate":
        if ($condition == "allcondition")
            $sql3 = "SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned'";
        break;

    case "new":
        if ($condition == "allcondition")
            $sql3 = "SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='new'";
        break;

    case "old":
        if ($condition == "allcondition")
            $sql3 = "SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='old'";
        break;

    case "Unknown state":
        switch ($condition)
        {
            case "Available/Unassigned":
            case "allcondition":
                $sql3 = "SELECT *FROM eq_inv WHERE eq_condition='Available/Unassigned' AND eq_state='Unknown state'";
                break;

        }
}

相关文章