Reputation: 121
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>
Upvotes: 0
Views: 243
Reputation: 94672
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.
Upvotes: 1