douhe6255 2014-04-27 13:15
浏览 54
已采纳

使用带fetch_array的switch语句

Ok, I've created a simple page that allows me to calculate customer satisfaction for the leaders who work in my store. Basically, I have a mysql database that contains the names of all the leaders who work there, along with the dates and time that they work. I also have a database that contains the dates, and times of a customers visit and the rating that they gave their visit.

I've created a query that behaves in such that when I query a leader's name, it returns all of the ratings/scores that were submitted by customers on the dates and times that specific leader was working.

Customers usually rate their experiences from 1 - 10 with 10 being the best. I wanted to transform those scored responses to a simple lettered system where a score of 9-10 would equal "G" for Good, 7-8 would equal "O" for ok and anything below 6 would equal "B" for bad.

Using mysqli_fetch_array and a switch statement, I tried to convert the numbers to the letters. However, I don't seem to be getting any results from this. I've tested the query and the fetch_array and if I use them by themselves (without the switch statement), they produce the correct scored responses (ie. Paul's score 10, 9, 9). But when I insert the switch statement, they do not convert to letters and nothing appears on the screen. Question: Is there something wrong with the way i'm using this switch statement or mysqli_fetch_array. I'm very new to coding so I may have a misunderstanding of the way these are used.

here's the php

<html>
<body>


<?php 

include("db.php");

echo $_POST['searched']; // temp. check to see if post came through
echo '<br>';

$searched = $_POST["searched"]; // create variable to put searched name in query.
$good = array(); //create array to store good scores
$ok = array(); //create array to store ok scores
$bad = array(); //create array to store bad scores


// Search the database and retrieve all ratings That matches a managers name

$query = "SELECT leaders.name, responses.score
     FROM leaders
     INNER JOIN responses
     ON leaders.shift_date = responses.visit_date
     AND leaders.shift_time = responses.visit_time
     AND leaders.name = '$searched' ORDER BY leaders.id;";    


$result = $db->query($query); //store that query

//iterate through result and grab each score
while ($row = $result->fetch_array()){ // place scores into an array
// use a switch statement to change numbered system to lettered
    switch($row[1]) {
        case 10:
        case 9:
            array_push($good, "G");
        break;

//echo $row[1] . ' '; temp check to ensure array call was successful
echo $good[0] . ' ';
    }
}
//echo "<script>window.location = 'http://localhost/~baronjon/ilotf/main.php'</script>";

 ?>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dswqz24846 2014-04-27 13:37
    关注

    You have to switch on a specific field of the array and not the whole array.

    Try this

    //iterate through result and grab each score
    while ($row = $result->fetch_array()){ // place scores into an array
    // use a switch statement to change numbered system to lettered
        switch($row['score']) {
            case 10:
            case 9:
                array_push($good, $row);
                break;
            case 8:
            case 7:
                array_push($ok, $row);
                break;
            default:
                array_push($bad, $row);
    
        } // endswitch
    }
    
    print_r( $good );
    print_r( $ok );
    print_r( $bad );
    

    Now you have the 3 new arrays each containing the result rows that fall into the 3 categories.

    PS Dont use row[0] syntax, because as soon as you change your select statement and add another field to the front of the field list you will be testing the wrong field in your switch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据