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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.