flemingmike Posted September 23, 2011 Share Posted September 23, 2011 hi, is there a way to tell php to do a process again? example: $draft_number=rand(1, 15); if($draft_number == 5){ START OVER AT TOP } else { $sql1 = "UPDATE names SET draft = '$draft_number' WHERE id = '$uid'"; $result1 = mysql_query($sql1) or die('Error, Check you fields and try again.'); } Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/ Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 Ive never used this before but it may suit your needs. http://php.net/manual/en/control-structures.goto.php Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272017 Share on other sites More sharing options...
flemingmike Posted September 23, 2011 Author Share Posted September 23, 2011 thats what i had in mind, but it doesnt work to go back up. Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272021 Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2011 Share Posted September 23, 2011 If you are looping over some data, you would use a loop construct of some kind to iterate over that specific data. What's your overall code? Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272022 Share on other sites More sharing options...
flemingmike Posted September 23, 2011 Author Share Posted September 23, 2011 how about a loop, would that work? Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272024 Share on other sites More sharing options...
flemingmike Posted September 23, 2011 Author Share Posted September 23, 2011 If you are looping over some data, you would use a loop construct of some kind to iterate over that specific data. What's your overall code? your quicker than me! here is my code if(isset($_POST)) { $uid=$_POST['name']; $totalplayers=mysql_query("SELECT COUNT(*) FROM names"); $totalplayers=mysql_fetch_array($totalplayers); $totalplayers="$totalplayers[0]"; $draft_number=rand(1, $totalplayers); $totalla=mysql_query("SELECT COUNT(*) FROM names WHERE draft = $draft_number"); $totalla=mysql_fetch_array($totalla); $totalla="$totalla[0]"; if($totalla == 0){ $sql1 = "UPDATE names SET draft = '$draft_number' WHERE id = '$uid'"; mysql_query($sql1) or die('Error, try again.'); } else { } } Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272025 Share on other sites More sharing options...
flemingmike Posted September 23, 2011 Author Share Posted September 23, 2011 its the else statement that i would like to make it loop. Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272029 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2011 Share Posted September 24, 2011 So, are you trying to assign a unique and random draft number to each player, all at once? Or are you trying to assign a (one) unique/unused random draft number to a player who's id is in $uid? Also, how many player names are there, because it might be more prudent to get a list of the used draft numbers up front, rather than execute a query inside of a loop trying to find an unused draft number. Edit: Unless you have thousands of players, regardless of assigning draft numbers all at once or to only one player with a specific id, it will be more efficient (because you are trying to use all the choices when you get closes and closer to the end, you will need to execute more and more select queries inside the loop to find an unused number) to get a list of the available draft numbers, randomize it, then pick a number(s). Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272226 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2011 Share Posted September 24, 2011 Assign random draft numbers all at once - <?php if(isset($_POST)) { $result=mysql_query("SELECT id FROM names"); // get all rows/all id's $totalplayers=mysql_num_rows($result); $available_numbers = range(1,$totalplayers); // array of all draft numbers shuffle($available_numbers); // randomize the available numbers while($row = mysql_fetch_assoc($result)){ $draft_number = array_pop($available_numbers); // pop a number from the end of the array $query = "UPDATE names SET draft = '$draft_number' WHERE id = '{$row['id']}'"; mysql_query($query) or die('Update query failed!'); } } Assign one unused random draft number to a specific id - <?php if(isset($_POST)) { $uid=$_POST['name']; $result=mysql_query("SELECT GROUP_CONCAT(draft), COUNT(*) FROM names"); // get existing draft numbers and count of all rows list($draft,$totalplayers)=mysql_fetch_row($result); $draft_numbers = range(1,$totalplayers); // array of all draft numbers $used_numbers = explode(',',$draft); // array of used draft numbers $available_numbers = array_diff($draft_numbers, $used_numbers); // determine unused draft numbers shuffle($available_numbers); // randomize the available numbers $draft_number = array_pop($available_numbers); // pop a number from the end of the array $query = "UPDATE names SET draft = '$draft_number' WHERE id = '$uid'"; mysql_query($query) or die('Update query failed!'); } Quote Link to comment https://forums.phpfreaks.com/topic/247711-is-there-a-way-to-repeat-process/#findComment-1272407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.