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

佐手、 提交于 2020-01-11 13:13:10

问题


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

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:


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:


You can try this:

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



回答3:


I am sharing with you guys the working code:

<label style="font-size:20px;"><strong>Filter Result:</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>

The rest of the code is here.



来源:https://stackoverflow.com/questions/59574648/counting-table-results-a-php-switch-case-that-uses-a-radio-button-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!