Results 1 to 7 of 7

Thread: Possible to use array to set a random value?

  1. #1
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default Possible to use array to set a random value?

    Okay so that title might not be the best inthe world. What I have is a script where I am using math over a period of time.

    I just started writing the script as an idea I had and wanted to try some php with it.

    I have made an array of numbers for the value:

    PHP Code:
    $value=array(124816);
    $valtot'31'
    So then I made some check boxes:

    Code:
    <table>
    <tr>
    <td>
    <input type="checkbox" name="choice1" value=""> Choice 1
    </td>
    <td>
    <input type="checkbox" name="choice2" value=""> Choice 2
    </td>
    <td>
    <input type="checkbox" name="choice3" value=""> Choice 3
    </td>
    <td>
    <input type="checkbox" name="choice4" value=""> Choice 4
    </td>
    </tr>
    </table>
    What I am attempting to accomplish next is adding something to the value="" line in the check boxes that I can use for the math part of the script.

    I also want the values to be random each time I load the scritp, BUT ONLY the FIRST TIME.

    As you can see there is one more value than there are check box options. SO I want the value of the check box to be random, thus leaving one out. Then I will select one or 2 checkboxes, click a button, and it will do the math. then when the math is complete I will select another box (as the others will remain checked) then do another math check.

    Then if I refresh the page, NEW values are given to the check boxes, so I could invariably choose the same options each time and get different results.

    Does that make sense?

    Okay, good. So what I guess I am after then is a way to pull a number randomly from the array and attatch it to a checkbox.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Okay so after some trial and error I have found the use of the
    PHP Code:
    $rand_keys=array_rand($value,5); 
    tag. I have placed this inside the tabe, but echoe'd after the Choice 1 text, so basically it is there, but not "assigned" to the choice checkbox it is with.

    Is there a way to make choice 1 BE the random number generated by the $rand_keys thing?
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    for($i = 1; $i <= 4; ++$i) {
      $rnd = rand(0, count($value));
      echo('<label><input type="checkbox" name="choice' . $i . '" value="' . $value[$rnd] . '"> Choice ' . $i . '</label>');
      unset($value[$rnd]);  // Prevent the same value appearing twice
    }
    Last edited by Twey; 07-09-2006 at 11:38 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Very nice! Now I can move onto the Math parts! Thanks Twey!
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Also, just to clarify that you were in FACT just testing me..

    PHP Code:
    $rnd rand(0cout($value)); 
    needs to be couNt not cout
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Gah, sorry. Stuck halfway between languages again.

    That code will make mincemeat of your array, by the way, so you might want to keep a copy somewhere.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    LOL just made a back up before I did it. Always do
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

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