Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    select random number from a table

    hey guys,

    im going to start writing a script tomorrow that once every week will pick a number at random from a table of tickets, its a lot like a lottery game, however im asking now as i dont know how is the best way to work out how many tickets are in the table 'lottery_tickets' and then do a random number: mt_rand(1,x); to come up with the random number but within the ammount that is in the table.

    thanks guys

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Put the numbers into an array and shuffle the array.
    When you pick the first array item, it will be random (from the shuffled array).

    Have another column in your table that identified the ticket (id) and has a pick column.
    You can mark that one so it never gets picked again.

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Thanks for the reply, how would I go about shuffling the array? Could you provide a breif example please if its no trouble
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    You would be doing a MySQL query, but I don't have any availability to your database, tables, or columns ...

    Save this script as "test.php" and run it over and over again.
    PHP Code:
    <?php

    $tickets 
    = array("1234","6546","67657","3422","2346");

    echo 
    "The first ticket in the original array is: ".$tickets[0]."<br><br>";

    // now we shuffle the array.
    shuffle($tickets);

    echo 
    "The first random ticket is: ".$tickets[0]."<br><br>";

    // notice in both cases, we're looking at the first array item.

    ?>

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i have two tables, one for the tickets:

    Code:
    ticketid
    username
    ticketid being the auto inc

    and the actual lottery table which has the date it will be drawn, ect

    but i didnt want to use mysql number rows to figure out how many tickets there are because people say its slow, and there could be more than say 10,000 tickets, so would it be worth more to use COUNT(ticketid) also, after it is drawn it will be emptying the table, will this reset the auto inc to start at 1 again, or will i have to go in manually to change it back again?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    also, how would i go about triggering a script to run, without a cron job, every wednesday and saturday at 5 pm?

    cheers.

    Danny
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Dan ...

    What will happen if you don't do anything to the data until someone
    views the site after 5pm on Wed and Sat? If you don't trigger the script
    to run automatically, what consequences would there be?

    If it's after 5pm on Wed and a person views the site, or access the script,
    your script will execute. So you don't need to run it automatically?

    It's sort of like "If a tree falls in the forest and nobody is there... "


    .

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i get what you mean, i think i will use a cron job for it thanks alot

    Edit:
    also, if i do empty the table will it reset my auto inc back to 0/1 or will i have to do that myself each time?
    Last edited by Dan13071992; 04-14-2012 at 07:00 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    See this ... two methods:
    http://www.electrictoolbox.com/artic...ll-data-mysql/

    DELETE FROM retains the auto-increment seed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •