Page 1 of 1
[SOLVED] Problem with while logic
Posted: Tue Apr 06, 2004 4:44 pm
by johnperkins21
I am trying to create a poll script that starts at the beginning of all the polls in the database and looks at a field that counts the total number of votes. After there have been 6 votes, I want to display the next poll. I am stuck on how to check for the number of votes and then change my variables accordingly. I know that my logic is just messed up on how to do this. Any suggestions would be appreciated. thanks.
Here's the while loop:
Code: Select all
<?php
$query = "SELECT * FROM Poll ORDER BY PollID";
$num_votes_query = mysql_query($query);
$six_votes = 'false';
do {
while ($num_votes = mysql_fetch_array($num_votes_query)) {
if ($num_votes[7] < '5') {
$six_votes == 'true';
$pollid = $num_votes[0];
$query = "SELECT * FROM Poll WHERE PollID = '$pollid'";
break;
}
}
if ($six_votes == 'true') {
break;
}
} while ($six_votes == 'false');
?>
It just creates an endless loop. I figured if I hit the first poll and there were only 1 vote, it would stop and change my query statement for me. Any ideas?
Posted: Tue Apr 06, 2004 4:49 pm
by Paddy
I am not exactly sure what you are trying to do, I kinda get it, but what I would ask you to do first is put in two echos so you can tell us which loop is looping eternally.
Posted: Tue Apr 06, 2004 4:58 pm
by johnperkins21
I tried adding echos as follows:
Code: Select all
<?php
$query = "SELECT * FROM Poll ORDER BY PollID";
$num_votes_query = mysql_query($query);
$six_votes = 'false';
do {
while ($num_votes = mysql_fetch_array($num_votes_query)) {
if ($num_votes[7] < '5') {
$six_votes == 'true';
$pollid = $num_votes[0];
$query = "SELECT * FROM Poll WHERE PollID = '$pollid'";
echo "if";
break;
}
echo "while";
}
if ($six_votes == 'true') {
break;
}
echo "dowhile";
} while ($six_votes == 'false');
?>
The output was:
ifdowhileifdowhileifdowhileifdowhile...
It breaks out of the interior while ok it seems since I don't get that echo statement.
I'll try to explain the situation better. I have a database table Poll with PollID, PollQ, PollA1, PollA2, PollA3, PollA4, NumQuestions, TotalVotes. Now instead of getting the most recent poll, I want the most recent poll with under 6 votes. Hence if PollID = 1 has TotalVotes = 6, I want to check to see how many votes are in PollID = 2. If there are only 3 votes in PollID = 2, then I want my query statement to have WHERE PollID = 2. Does that make sense at all, or am I completely nuts?
Posted: Tue Apr 06, 2004 5:01 pm
by markl999
Can't you just do,
$query = "SELECT * FROM Poll WHERE TotalVotes < 6 ORDER BY PollID DESC LIMIT 1";
?
That should get the most recent poll with less than 6 votes.
Posted: Tue Apr 06, 2004 5:04 pm
by johnperkins21
Holy Sh!t, I'm an idiot. I didn't even think of that. Thank you! Thank you sir.
I actually wanted the oldest poll with less than 6 votes, but removing DESC takes care of that. Thank you both for your help.
Posted: Tue Apr 06, 2004 6:53 pm
by Weirdan
Just in case you're still curious what was wrong with your code:
Code: Select all
// was
....
$six_votes == 'true';
....
// should be
....
$six_votes = 'true';
....
Posted: Wed Apr 07, 2004 2:01 pm
by johnperkins21
Weirdan,
I thought that the == was for string literals whereas = would be for numbers and assignments.
Since 'true' was quoted, wouldn't that make it a string, therefore requiring ==? I'm still kind of confused on the whole =, ==, === thingy. I don't know why === looks for exact matches and == only looks for matches. What's the difference? Thanks for your help.
Posted: Wed Apr 07, 2004 2:10 pm
by d3ad1ysp0rk
== is used for comparing
= sets a variable
Code: Select all
$x = 1; //set x to 1
$y = $_POST['y']; //set y to the post variable
if($x == $y){ //check if x equals y
//do stuff
}
Posted: Wed Apr 07, 2004 2:13 pm
by johnperkins21
Oh yeah, I caught it now. Just not paying attention to details. Get me in trouble every time.