Page 1 of 1

Random number, then remove from a list

Posted: Wed Jul 05, 2006 7:41 am
by sikelsh
A bit of background.

I run a fishing website, normally when we have a Match at a lake, we pull our peg numbers from a bag and thats the peg you fish for the day.

However, people are arriving at seperate times, so we are trying to do this online.

So in english this is what im trying to achieve.

On a lake there are say 32 pegs, so my array is (1,2,3,4,5.etc to.32) I want the user to complete a form, and the script to randomly select one of the above numbers, data returned is say "David" "Peg 5".

I need David and his peg (5) to be logged into a MySQL database and the number 5 to disapear from my array.

Any help to kick me off would be most appriciated. Im not fantastic with PHP but I know a bit, I just cant seem to get started!

Thanks in advance
Si

Posted: Wed Jul 05, 2006 7:58 am
by Ollie Saunders
Attempt it then when you have a problem we'll be able to help you.

If you don't know where to start, find a tutorial, read a book on the subject or consult the MySQL and PHP manuals.

Posted: Wed Jul 05, 2006 10:14 am
by Jenk
the function array_rand, or shuffle will be of assistance here..

after a quick think, make a table for pegs with a second column for name of angler..

PegNum (Primary Key, Not Null), Name (Default=Null)

Then fill it with a row for each peg, and of course the peg number goes in PegNum, but leave Name blank.. as you don't know who has which peg yet.

Code: Select all

<?php

$pegs = array();
$query = "SELECT `PegNum` FROM `Pegs` WHERE `Name` = NULL;";
$result = mysql_query($query) or die('of death');

while ($row = mysql_fetch_assoc($result)) {
    $pegs[] = $row['PegNum'];
}

$randomPeg = $pegs[array_rand($pegs)]; //this is the random generated peg number..

?>
The rest I shall leave to you, good luck :)

Posted: Wed Jul 05, 2006 10:29 am
by Ollie Saunders

Code: Select all

WHERE `Name` = NULL;
will always test false. use:

Code: Select all

WHERE Name IS NULL