array from query then random gen. of array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cherryst152
    New Member
    • Nov 2007
    • 16

    array from query then random gen. of array

    I'm trying to make an array from a query and then randomly select 10 values from the array, but the error i keep getting is that it's not reading as an array. Thanks in advance for anyone who can help.

    $query = "SELECT Id FROM January WHERE Username='0' AND Day='1'";
    $result = mysql_query($qu ery);

    while($row=mysq l_fetch_assoc($ result))
    {
    echo $row['Id'];
    echo "<br>";
    }

    for($i=1; $i<=10; $i++){
    $random = array_rand($row );
    echo $random;
    }
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by cherryst152
    I'm trying to make an array from a query and then randomly select 10 values from the array, but the error i keep getting is that it's not reading as an array. Thanks in advance for anyone who can help.

    $query = "SELECT Id FROM January WHERE Username='0' AND Day='1'";
    $result = mysql_query($qu ery);

    while($row=mysq l_fetch_assoc($ result))
    {
    echo $row['Id'];
    echo "<br>";
    }

    for($i=1; $i<=10; $i++){
    $random = array_rand($row );
    echo $random;
    }
    ?>
    Try:
    [php]
    <?php
    $_query = mysql_query("SE LECT * FROM `Members_Info`" );

    function rand_from_array ($_data)
    {
    for($_i = 0; $_i < 10; ++$_i)
    {
    echo array_rand($_da ta);
    }
    }

    while($_row=mys ql_fetch_assoc( $_query))
    {
    echo rand_from_array ($_row) . "<Br />";
    }
    ?>
    [/php]
    You'll have to adapt it (slight modifications)
    Also untested!
    Let me know if there's a problem!

    Comment

    • cherryst152
      New Member
      • Nov 2007
      • 16

      #3
      When i tried that, all i get was 'Id' listed way too many times.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by cherryst152
        When i tried that, all i get was 'Id' listed way too many times.
        Show me what you got.





        ...
        :|

        Comment

        • cherryst152
          New Member
          • Nov 2007
          • 16

          #5
          $_query = mysql_query("SE LECT Ad_Id FROM January_Ad1 WHERE Username='0' AND Day='1'");

          function rand_from_array ($_data)
          {
          for($_i = 0; $_i < 10; ++$_i)
          {
          echo array_rand($_da ta);
          }
          }

          while($_row=mys ql_fetch_assoc( $_query))
          {
          echo rand_from_array ($_row) . "<Br />";
          }

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            Look at shuffle() and array_rand() functions here.

            Sorry this didn't post, so only posted it a few replies later so it's out of date!
            Last edited by TheServant; Mar 17 '08, 11:08 PM. Reason: Delayed response

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Actually, look at that link. array_rand() takes another input which lets you choose how many elements you want to return, so that might be what you're after!

              Comment

              • cherryst152
                New Member
                • Nov 2007
                • 16

                #8
                that definitely helped and put things in a more logical perspective. thanks. however, i remain with the same original problem. i feel as though this problem is because the array which i am trying to carry over from the while loop is not reading in the for loop. any further suggestions?

                Comment

                • cherryst152
                  New Member
                  • Nov 2007
                  • 16

                  #9
                  works properly now. finished code below.

                  $query = "SELECT Id FROM January WHERE Username='0' AND Day='1'";
                  $result = mysql_query($qu ery);

                  $data = array();
                  while($row=mysq l_fetch_assoc($ result))
                  {
                  $data[] = $row['Id']; // store in array
                  }

                  $random = array_rand($dat a, 10); // get 10 random keys

                  foreach ($random as $key)
                  {
                  echo $data[$key];
                  echo "<br>";
                  }

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Originally posted by cherryst152
                    that definitely helped and put things in a more logical perspective. thanks. however, i remain with the same original problem. i feel as though this problem is because the array which i am trying to carry over from the while loop is not reading in the for loop. any further suggestions?
                    Why don't you use [PHP]array_rand($dat a, 10)[/PHP] instead of your for loop? Don't they do the same thing?


                    ** ALSO: Start using appropriate code tags around any posted code!

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      lol, I'm a bit slow today. Glad we could help ;)

                      Comment

                      Working...