Jump to content

is there a way to repeat process


flemingmike

Recommended Posts

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 {

}


}

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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!');
}

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.