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

huangapple go评论81阅读模式
英文:

Counting table results: A PHP switch case that uses a radio button value

问题

我编辑了帖子以便更好地理解。这是我作为学生实习生的第一个项目。这是一个设备监控系统,用于记录计算机设备的情况。这是一个包含过滤选项和显示计算机设备列表的表格的页面的代码部分。过滤选项包括属于两个类别的多个单选按钮,这两个类别是“状态”和“条件”。请在帖子末尾查看这两个类别的组件。这些组件是单选按钮的值。

当单击另一个单选按钮时,页面会重新加载,类似于Facebook搜索。我想要计算每个状态和条件下设备的数量,并显示在单选按钮标签旁边。但是,状态单选按钮会影响设备的计数。例如,New EQ是刚刚输入库存的设备类型。当选中New EQ按钮并选中Available/Unassigned按钮时,表格结果将被筛选,显示包含状态为New且条件为Available/Unassigned的设备的结果。表格结果将被计数,并在单选按钮标签旁边输出,告诉用户有多少设备是**“New”“Available/Unassigned”。当单击另一个状态单选按钮,例如Old EQ按钮时,标签旁边显示的Available/Unassigned设备数量将根据返回的行数而变化,因为用户更改了输出设备的请求,要输出状态为“Old”“Available/Unassigned”的设备。顺便说一下,状态和条件是同一表格中的列。这将适用于所有单选按钮。如果用户在条件类别下单击另一个单选按钮,例如Assigned EQ按钮,则标签旁边显示的Available/Unassigned数量将更改为0,因为用户请求表格结果应显示新和已分配的设备,未请求Available/Unassigned设备。如果用户点击“所有条件”按钮,它将计算包含所有条件的表格行,该行还具有新**状态的数据。显示表格结果的筛选过程已经正常运行。我现在正在努力在单选按钮标签旁边输出数量。

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

<?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;
}
         //上面的代码是不完整的
$result3=mysqli_query($conn,$sql3);
$count=mysqli_num_rows($result3);
echo "<label style='color:red;'><strong>".$count."</strong></label>";

?>

类别:

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

条件: 所有条件、可用/未分配、维修、缺少零件、缺少设备、有缺陷、翻新、未知条件

英文:

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.

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开关案例
计数表结果:一个使用单选按钮值的PHP开关案例

<?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>";

?>

Categories:

State: All state,new,old,unknown state

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

答案1

得分: 0

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

因此,唯一有用的情况是:

  • case true
  • case false
  • case default

在您的情况下,您应该使用 if / else 结构。

更新

因为在评论中要求如何在 switch 结构中编写条件,见下面的嵌套代码:

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;

        }
}
英文:

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

So the only useful cases then are

  • case true
  • case false
  • case default

In your case you should use if / else construct.

Update

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;

        }
}

答案2

得分: 0

你可以尝试这样做:

<?php
$sql3 = "SELECT * FROM eq_inv WHERE eq_condition = 'Available/Unassigned'";

if (in_array($state, ['new', 'old', 'Unknown state']) && in_array($condition, ['allcondition', 'Available/Unassigned'])) {
  $sql3 .= " AND eq_state = '" . $state . "'";
}

$result3 = mysqli_query($conn, $sql3);
$count = mysqli_num_rows($result3);
echo "<label style='color:red;'><strong>" . $count . "</strong></label>";
?>
英文:

You can try this:

&lt;?php
$sql3=&quot;SELECT *FROM eq_inv WHERE eq_condition=&#39;Available/Unassigned&#39;&quot;;
if(in_array($state, [&#39;new&#39;, &#39;old&#39;, &#39;Unknown state&#39;]) &amp;&amp; in_array($condition, [&#39;allcondition&#39;, &#39;Available/Unassigned&#39;])) {
$sql3 .=&quot; AND eq_state=&#39;&quot;.$state.&quot;&#39;&quot;;
}
$result3=mysqli_query($conn,$sql3);
$count=mysqli_num_rows($result3);
echo &quot;&lt;label style=&#39;color:red;&#39;&gt;&lt;strong&gt;&quot;.$count.&quot;&lt;/strong&gt;&lt;/label&gt;&quot;;
?&gt;

答案3

得分: 0

我与大家分享可运行的代码:

<label style="font-size:20px;"><strong>筛选结果:</strong></label>
<?php
$state=$_SESSION['state'];
$condition=$_SESSION['condition'];
echo "<label style='color:red;'>" . " " . "State:" . " " . $state . " " . "-" . " " . "Condition:" . " " . $condition . "</label>";
?>
<br>
<div class="filters">
<?php
}
?>
<script>
  function handleRadio(data){
    window.location="sessionstate.php?state="+data.value;
  }
  function handleRadiocond(data2){
    window.location="sessioncondition.php?condition="+data2.value;
  }
</script>

其余代码在这里

英文:

I am sharing with you guys the working code:

&lt;label style=&quot;font-size:20px;&quot;&gt;&lt;strong&gt;Filter Result:&lt;/strong&gt;&lt;/label&gt;
&lt;?php
$state=$_SESSION[&#39;state&#39;];
$condition=$_SESSION[&#39;condition&#39;];
echo &quot;&lt;label style=&#39;color:red;&#39;&gt;&quot;.&quot; &quot;.&quot;State:&quot;.&quot; &quot;.$state.&quot; &quot;.&quot;-&quot;.&quot; 
&quot;.&quot;Condition:&quot;.&quot; &quot;.$condition.&quot;&lt;/label&gt;&quot;;
?&gt;
&lt;br&gt;
&lt;div class=&quot;filters&quot;&gt;
&lt;?php
}
?&gt;
&lt;script&gt;
function handleRadio(data){
window.location=&quot;sessionstate.php?state=&quot;+data.value;
}
function handleRadiocond(data2){
window.location=&quot;sessioncondition.php?condition=&quot;+data2.value;
}
&lt;/script&gt;

The rest of the code is here.
1: https://pastebin.com/3BrjaSSk

huangapple
  • 本文由 发表于 2020年1月3日 15:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定