random selection of CSV's

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • monomaniac21

    random selection of CSV's

    Hi all!

    Is there a function in PHP-MySQL to randomly select comma-seperated
    variables in a text field?

    For example if you have a text field containing the values
    "1,1.2,4,5. 6," is there a way of randomly selecting one of the numbers?

    Any ideas would be greatly appreciated!

  • monomaniac21

    #2
    Re: random selection of CSV's

    is array_rand the best bet?

    Comment

    • noone

      #3
      Re: random selection of CSV's

      monomaniac21 wrote:[color=blue]
      > Hi all!
      >
      > Is there a function in PHP-MySQL to randomly select comma-seperated
      > variables in a text field?
      >
      > For example if you have a text field containing the values
      > "1,1.2,4,5. 6," is there a way of randomly selecting one of the numbers?
      >
      > Any ideas would be greatly appreciated!
      >[/color]

      $selected = $arrayvar[ rand(0,array_co unt_values($rec ord)-1) ]
      get a random element within the array

      not tested - YMMV.

      Comment

      • noone

        #4
        Re: random selection of CSV's

        monomaniac21 wrote:[color=blue]
        > is array_rand the best bet?
        >[/color]

        yes, that too... :)

        Comment

        • monomaniac21

          #5
          Re: random selection of CSV's

          Thanks noone, how can you convert a text field into an array? or how
          can you create an array field in mysql?

          Comment

          • Al Binns

            #6
            Re: random selection of CSV's

            monomaniac21 wrote:[color=blue]
            > Thanks noone, how can you convert a text field into an array? or how
            > can you create an array field in mysql?
            >[/color]

            I'd use the explode() function

            <?php

            $str = "1,2,3,4";
            $arr = explode(",",$st r);
            print_r($arr);

            ?>

            Comment

            • noone

              #7
              Re: random selection of CSV's

              monomaniac21 wrote:[color=blue]
              > Thanks noone, how can you convert a text field into an array? or how
              > can you create an array field in mysql?
              >[/color]


              go to the docs and lookup arrays, explode

              Two sites that will help you immeasurably!!

              and http://us3.php.net/manual/en/funcref.php
              then use the search feature


              From the docs: http://us3.php.net/manual/en/function.explode.php
              // Example 1
              $pizza = "piece1,piece2, piece3,piece4,p iece5,piece6";

              // or you can get the field from a database - coding left to
              // the OP - you learn better that way...

              $pieces = explode(",", $pizza);
              echo $pieces[0]; // piece1
              echo $pieces[1]; // piece2

              Comment

              • Cmac

                #8
                Re: random selection of CSV's

                $string = $_POST['something'];

                $chars = preg_split('//', $string, -1, PREG_SPLIT_NO_E MPTY);

                list($pone,$ptw o,$pthree,$pfou r,$pfive,$psix, $pseven) = $chars;

                you can split a string into an array this way too...

                Comment

                • Jerry Stuckle

                  #9
                  Re: random selection of CSV's

                  monomaniac21 wrote:[color=blue]
                  > Thanks noone, how can you convert a text field into an array? or how
                  > can you create an array field in mysql?
                  >[/color]

                  You don't create an array field in MySQL. Rather, you create a second
                  table with a one-to-multiple linkage.

                  In this case you would have two columns - id and numval (or a more
                  appropriate name for whatever you're using it for). id would match the
                  id in the first table. numval would hold one value in each row. So,
                  you might end up with something like:

                  id numval
                  4 1
                  4 1.2
                  4 4
                  4 5.6


                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  Working...