attaboy Posted March 19, 2012 Share Posted March 19, 2012 I have a table for collecting airline satisfaction survey results(see attached image). I run a query to select the staff field there are 25 rows in this column I run a switch case block to filter out a result set that includes the responses 'poor', 'fair', 'good', and 'excellent' the result set should contain 20 elements function score_staff() { $staff_count = 0; $result_set_cnt = 0; $query = "SELECT staff FROM flight_survey"; $result = mysql_query($query); while ($get_info = mysql_fetch_row($result)){ foreach ($get_info as $field){ switch($field) { case "poor": $staff_count++; $result_set_cnt++; $exit; case "fair": $staff_count+=2; $result_set_cnt++; $exit; case "good": $staff_count+=3; $result_set_cnt++; $exit; case "excellent": $staff_count+=4; $result_set_cnt++; $exit; drfault: // do nothing } } } echo $staff_count."<br>"; echo $result_set_cnt."<br>"; } For each match I add 1 to the result set so with 20 matches $result_set_cnt should be 20 and not 53 as shown at the bottom of the attached image. The array $get_info bewilders me and I can't seem to access it's elements without putting it in a while loop. Anyway I'm pretty new to php and have a lot to learn. If anyone can explain how I could change the code so that the result set would contain 20 I think I'll be alright. thanks Link to comment https://forums.phpfreaks.com/topic/259231-problems-managing-data-from-a-single-field/ Share on other sites More sharing options...
AyKay47 Posted March 19, 2012 Share Posted March 19, 2012 SELECT staff FROM flight_survey WHERE staff IN ('good', 'fair', 'poor'); EDIT: There is no need to use a foreach inside of the while loop, $get_info[0] will hold the results set value. Link to comment https://forums.phpfreaks.com/topic/259231-problems-managing-data-from-a-single-field/#findComment-1328971 Share on other sites More sharing options...
attaboy Posted March 19, 2012 Author Share Posted March 19, 2012 but how do get the info out of $get_info if I do this: echo "{$get_info[0][0]}<br>"; echo "{$get_info[0][1]}<br>"; echo "{$get_info[0][2]}<br>"; echo "{$get_info[0][3]}<br>"; I get n o o f a i r f a i r g o o d f a i r g o o d n o o n o o n o o n o o e x c e e x c e g o o d g o o d p o o r f a i r f a i r g o o d f a i r g o o d g o o d f a i r p o o r p o o r p o o r Link to comment https://forums.phpfreaks.com/topic/259231-problems-managing-data-from-a-single-field/#findComment-1329173 Share on other sites More sharing options...
AyKay47 Posted March 19, 2012 Share Posted March 19, 2012 I have already given you the answer, post the updated code. Link to comment https://forums.phpfreaks.com/topic/259231-problems-managing-data-from-a-single-field/#findComment-1329193 Share on other sites More sharing options...
attaboy Posted March 19, 2012 Author Share Posted March 19, 2012 I found something that works, it includes the SELECT statement you suggested, the foreach statement you thought I didn't need and I had to replace the switch block with some if else if's for some reason. Thanks for the help. $staff_count = 0; $result_set_cnt = 0; $query = "SELECT staff FROM flight_survey WHERE staff IN ('poor', 'fair', 'good', 'excellent')"; $result = mysql_query($query); while ($get_info = mysql_fetch_row($result)){ foreach ($get_info as $field){ echo "{$field}<br>"; if ($field == "poor") { $staff_count++; $result_set_cnt++; } else if ($field == "fair") { $staff_count += 2; $result_set_cnt++; } else if ($field == "good") { $staff_count += 3; $result_set_cnt++; } else if ($field == "excellent") { $staff_count += 4; $result_set_cnt++; } else { //nothing } } } echo $staff_count."<br>"; echo $result_set_cnt."<br>"; } Link to comment https://forums.phpfreaks.com/topic/259231-problems-managing-data-from-a-single-field/#findComment-1329234 Share on other sites More sharing options...
AyKay47 Posted March 20, 2012 Share Posted March 20, 2012 As I have stated before, you don;t need the foreach loop, and it shouldn't be there as it is a waste of resources. $get_info[0] will contain the value of the column `staff`. Link to comment https://forums.phpfreaks.com/topic/259231-problems-managing-data-from-a-single-field/#findComment-1329260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.