Random number, then remove from a list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sikelsh
Forum Newbie
Posts: 8
Joined: Thu Mar 23, 2006 4:20 am

Random number, then remove from a list

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

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

Code: Select all

WHERE Name IS NULL
Post Reply