Jump to content

Problem with random thing


gli

Recommended Posts

Hi!

As you see i have list with variables,

$player1

$player2

$player3 etc.

I want to insert these variables randomly in the database, to make unique list like

$player3 vs $player1 etc.

 

How to make this variable list to insert randomly in database, in more than one rows (in each row to player variables) and varibales which have randomly insterted dont repeat in other mysql queries when inserting thow player variables from that variable list.

 

Maybe you can give me the way how it can be done, and i'll make, because i cant find sollution which commands from php or mysql can do it.

 

Here's my code for better understanding:

// database results associtated with variables
$player1 = $row1['playername'];
$player2 = $row2['playername'];
$player3 = $row3['playername'];
$player4 = $row4['playername'];


// insert players in database randomly, in each row two variables, and dont repeat same variables in all rows at all.
$cupname = $row['cup'];
mysql_query("INSERT INTO gamessch 
(cup,player1,player2,played,date)
VALUES('$cupname','$playerX','$playerX','no',CURDATE()) ") 
or die(mysql_error());

mysql_query("INSERT INTO gamessch 
(cup,player1,player2,played,date)
VALUES('$cupname','$playerX','$playerX','no',CURDATE()) ") 
or die(mysql_error());

 

Thanks, and sorry for my bad English.

Link to comment
Share on other sites

i have never done what you are asking but as a start i know you can randomly select rows out of a mysql table.  so you could store the players in a table which has i table id.  you could then select the rows at random using the mysql_rand.  using the row id number, enter this on to a blacklist of numbers that should be avoided when selecting the random row.

 

you would need to look into this a bitmore but it could be a start here is a link which can explain it a bit better than me.  just scroll down for the musql_rand section it is on the comments part at the top.

 

http://uk2.php.net/rand

Link to comment
Share on other sites

Hi thanks for answer.  I studied this for an hour. I realised that in my case i cant use mysql random.

Because in that table there are much id's, but i need only id's where 'name' = etc., and i cant and in code exactly row id's. Im begginer on php and mysql so probbably i think not right.

But i found one way but i have problem, i make random number with php, but how turn this number in 'blacklist' , that the next 'random' php command will avoid this number.

 

 

$number1 = rand(1, 4);

 

// this avoids from $number1

$number2 = rand(1, 4); 

 

// this avoids from $number1 and $number2

$number3 = rand(1, 4);

 

// this avoids from $number1 , $number2 and $number3

$number4 = rand(1, 4);

 

Link to comment
Share on other sites

This should give you an idea:

 

<?php

// Untested...

// Array of players
$players = array( 'Steve', 'John', 'Al', 'Mike', 'Jill' );

// We can use a single INSERT statement to insert multiple rows, with the
// format:
// INSERT INTO `table_name` ( `col1`, `col2`, `col3` ) VALUES
//   ( row1_val1, row1_val2, row1_val3 ),
//   ( row2_val1, row2_val2, row2_val3 ),
//   ...

// The next code segment builds every combination of the VALUES-part for our
// array of players
$value_parts = array();
$num_players = count( $players );
for( $i = 0; $i < $num_players - 1; $i++ ) {
  for( $j = $i + 1; $j < $num_players; $j++ ) {
    // $i: player1
    // $j: player2
    $value_parts[] = sprintf( "( '%s', '%s', '%s', 'no', NOW() )",
                     $cupname ,
                     $players[$i] ,
                      $players[$j] 
                   );
  }
}

// Now we create our sql statement
$sql = sprintf( "
  INSERT INTO `gamessch` ( `cup`, `player1`, `player2`, `played`, `date` )
  VALUES %s", implode( ', ', $value_parts ) );

echo $sql;
?>

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.