Jump to content

Why is this loop not working? (mysql, php)


lilman

Recommended Posts

The objective with this do while loop is to pull all the possible candidates to vote on, but than test to make sure you haven't already voted on that person, if you haven't, it should display the picture.

 

do
{
                // Pull all candidates from DB
	$sql = "SELECT * FROM pandoris_hotornot_options WHERE gender = '$gender'";
	$run = mysql_query($sql);
	$numrow = mysql_num_rows($run);

                // Put all the results in an array
	for ($i=0; $i <$numrow; $i++)
	{
		$row=mysql_fetch_array($run);
		$ids[$i]=$row['user_id'];
		$names[$i]=$row['user_name'];
		$path[$i]=$row['path'];
		$comments[$i]=$row['comments'];

	}

                // Randomly pick a number
	$number=rand (0, ($numrow-1));

                // Check another DB which stores candidates id's and the rater id's.  
	$checksql = "SELECT * FROM pandoris_hotornot_ratings WHERE canidate_id = '$number' AND rater_id = '$user_id'";
	$checkrun = mysql_query($checksql);
	$check_num_rows = mysql_num_rows($checkrun);

                // If it comes back 0 that means they haven't voted on that person yet.
	if($check_num_rows == "0")
	{
		$go_on = "no";
	}
	else
	{
		$go_on = "yes";
	}
}

        // If $check_num_rows equals 0 then it should $go_on should say no, thus, stopping the loop
while($go_on == "yes");

 

I cannot seem to understand why it isn't working right.  Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/50140-why-is-this-loop-not-working-mysql-php/
Share on other sites

try

 

<?php
$go_on = "yes";
        // If $check_num_rows equals 0 then it should $go_on should say no, thus, stopping the loop
while($go_on == "yes")
{
                // Pull all candidates from DB
	$sql = "SELECT * FROM pandoris_hotornot_options WHERE gender = '$gender'";
	$run = mysql_query($sql);
	$numrow = mysql_num_rows($run);

                // Put all the results in an array
	for ($i=0; $i <$numrow; $i++)
	{
		$row=mysql_fetch_array($run);
		$ids[$i]=$row['user_id'];
		$names[$i]=$row['user_name'];
		$path[$i]=$row['path'];
		$comments[$i]=$row['comments'];

	}

                // Randomly pick a number
	$number=rand (0, ($numrow-1));

                // Check another DB which stores candidates id's and the rater id's.  
	$checksql = "SELECT * FROM pandoris_hotornot_ratings WHERE canidate_id = '$number' AND rater_id = '$user_id'";
	$checkrun = mysql_query($checksql);
	$check_num_rows = mysql_num_rows($checkrun);

                // If it comes back 0 that means they haven't voted on that person yet.
	if($check_num_rows == "0")
	{
		$go_on = "no";
	}
	else
	{
		$go_on = "yes";
	}
}


?>

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.