PHP: How do i work with loops

twister

Howdy
Hey can anyone help me with this? I'm pulling a bunch of info out of a database. Each value of row[1] will be one of six choices. Now i need to count up those choices. So i wrote this.

if ( $row[1] == "Strongly Disagree") {
$Q1SD++;
} elseif ( $row[1] == "Sorta Disagree") {
$Q1KD++;
} elseif ( $row[1] == "Disagree") {
$Q1D++;
} elseif ( $row[1] == "Agree") {
$Q1A++;
} elseif ( $row[1] == "Sorta Agree") {
$Q1KA++;
} elseif ( $row[1] == "Strongly Agree") {
$Q1SA++;
} else {
}

It will work fine for Strongly Disagree but nothing more. If i echo the $row[1] I will get Sorta Disagree for some of the answers but it will not count them. It will only count how may Disagree's because that's what it came across first.

Data:
$row = Disagree,Sorta Agree,Agree,Disagree,Agree

What's up?

Thanks
 
Try this way:
if ( $row[1] == "Strongly Disagree") {
$Q1SD++;
};
if ( $row[1] == "Sorta Disagree") {
$Q1KD++;
};
if ( $row[1] == "Disagree") {
$Q1D++;
};
if ( $row[1] == "Agree") {
$Q1A++;
};
if ( $row[1] == "Sorta Agree") {
$Q1KA++;
};
if ( $row[1] == "Strongly Agree") {
$Q1SA++;
};
 
Ok i will. I Think i tried though. Don't rememer. Alls i know is elseif didn't work and some switch comand didn't work. Shouldn't be that hard should it?

Thanks
 
I think in this case, since it appears that $row[1] will only have one of those listed values, that an else statement wont be needed.

There is a switching shortcut way to do this kind of thing, but I think it only applies to assigning a new value to one of the arguements being evaluated, not a third party, but I dont know the proper syntax off the top of my head.

They way I put it is the most straightforward approach, and hopefully the right syntax.
 
My code was right. I spend hours chasing a bug that didn't exist. The problem was that i was outputting echo ("Q1 = ".$row[1]); to see what the answers where and this is what i saw 'Q1 = Sorta Disagree' Now when i asked a friend for help they did this echo ("Q1 = |".$row[1]."|"); and this is what they got 'Q1 = | Sorta Disagree|'

So the whole time i had an extra space that i could't see.

Gees. But it's fixed now. Thanks for all the help.
 
Back
Top