Results 1 to 7 of 7

Thread: pick a number

  1. #1
    Join Date
    Apr 2005
    Posts
    694

    pick a number

    Hi guys,

    I have a php file, which have inside:

    PHP Code:
    <? id 1,2,3,4,5,6,7,8?>
    I also have another php file, where I would like it to randomly pick an id number from the previous file, can somebody please help me with this?

    Thanks in advance!

  2. #2
    Join Date
    Nov 2004
    Location
    HK
    Posts
    309
    Code:
    <? id = 1,2,3,4,5,6,7,8; ?>
    I don't think this is a valid php code...

    BTW you can try this:

    $id=array(1, 2, 3, 4, 5);
    echo array_rand($id);


    reference:
    http://hk.php.net/manual/en/function.array-rand.php
    Rails in DA - Ruby on Rails plugin for Directdmin | DA-Tomcat - Tomcat Manager plugin for Directdmin
    DA-PgSQL - PostgreSQL plugin for Directdmin | IP Deny Manager - IP Deny Manager plugin for Directdmin
    DeeperAdmin - Manager your DirectAdmin server "deeper"
    Order now at http://www.daplugin.com

  3. #3
    Join Date
    Mar 2006
    Posts
    965
    If you intend to use mySQL with PHP, you can simply call your IDs by distinction:

    PHP Code:
    SELECT DISTINCT field_id
    FROM your_table
    ORDER BY RAND
    () 

  4. #4
    Join Date
    Apr 2005
    Posts
    694
    Quote Originally Posted by hehachris
    Code:
    <? id = 1,2,3,4,5,6,7,8; ?>
    I don't think this is a valid php code...

    BTW you can try this:

    $id=array(1, 2, 3, 4, 5);
    echo array_rand($id);


    reference:
    http://hk.php.net/manual/en/function.array-rand.php
    Hi Horizon and Chris,

    I am not planning to use mysql with us, i just want to store the array in a seperate file, is that possible? or do I need to put the: $id=array(1, 2, 3, 4, 5); in the same file I'm executing it from?

    Thanks for your kind replies...

  5. #5
    Join Date
    Apr 2005
    Posts
    694
    PHP Code:
     $sql="insert into userid set
                            UID= 'array_rand(
    $id)', 
    That doesn't seem correct....

  6. #6
    Join Date
    Dec 2005
    Posts
    395
    save your array to a php file. Lets call it id.php. then do

    PHP Code:
    include 'id.php';

    $arraySize sizeof($id);
    $element rand(0,$arraySize);
    $randomNumber $id[$element]; 
    $randomNumber is a random number from your array.

  7. #7
    Join Date
    Apr 2005
    Posts
    694
    works great! Thanks for all the help!

Posting Permissions

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