Jump to content

problems managing data from a single field


attaboy

Recommended Posts

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

post-98811-13482403317851_thumb.png

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.