sorting random values from a string

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

    sorting random values from a string

    Hi
    I have a php string like this:
    $string = "one two three four five"
    I have to sorting the values randomly every time the page are loaded.
    So, for example:
    first time
    "one two three four five"
    second time
    "three five one four two"
    third time
    "five four two one three"
    ....
    The number are a images in a folder and I have to sort these images in
    a random sequence.
    Thanks for suggestions.

  • Rik Wasmus

    #2
    Re: sorting random values from a string

    On Thu, 06 Sep 2007 15:39:06 +0200, pt36 <key.alberto@gm ail.comwrote:
    Hi
    I have a php string like this:
    $string = "one two three four five"
    I have to sorting the values randomly every time the page are loaded.
    So, for example:
    first time
    "one two three four five"
    second time
    "three five one four two"
    third time
    "five four two one three"
    ...
    The number are a images in a folder and I have to sort these images in
    a random sequence.
    Thanks for suggestions.

    $string = "one two three four five";
    $array = explode(' ', $string);
    shuffle($array) ;
    $string = implode(' ',$string);
    --
    Rik Wasmus

    Comment

    • Rik Wasmus

      #3
      Re: sorting random values from a string

      On Thu, 06 Sep 2007 15:47:51 +0200, Rik Wasmus
      <luiheidsgoeroe @hotmail.comwro te:
      On Thu, 06 Sep 2007 15:39:06 +0200, pt36 <key.alberto@gm ail.comwrote:
      >
      >Hi
      >I have a php string like this:
      >$string = "one two three four five"
      >I have to sorting the values randomly every time the page are loaded.
      >So, for example:
      >first time
      >"one two three four five"
      >second time
      >"three five one four two"
      >third time
      >"five four two one three"
      >...
      >The number are a images in a folder and I have to sort these images in
      >a random sequence.
      >Thanks for suggestions.
      >
      >
      $string = "one two three four five";
      $array = explode(' ', $string);
      shuffle($array) ;
      $string = implode(' ',$string);
      ....ofcourse:
      $string = implode(' ',$array);




      --
      Rik Wasmus

      Comment

      • pt36

        #4
        Re: sorting random values from a string

        Ok Rik
        thanks for your help, but please tell me why this work
        <?php
        $string = "uno.jpg due.jpg tre.gif quattro.gif cinque.jpg";
        $array = explode(' ', $string);
        shuffle($array) ;
        $string = implode(' ', $array);
        echo $string ;
        ?>

        and this not work
        <?php
        if ($handle = opendir('banner/')) {
        while (false !== ($file = readdir($handle ))) {
        if ($file != "." && $file != "..") {
        $file = $file . " ";
        $array = explode(' ', $file);
        shuffle($array) ;
        $file = implode(' ', $array);
        echo $file;
        } }
        closedir($handl e);
        }
        ?>

        Comment

        • Rik Wasmus

          #5
          Re: sorting random values from a string

          On Thu, 06 Sep 2007 17:26:25 +0200, pt36 <key.alberto@gm ail.comwrote:
          Ok Rik
          thanks for your help, but please tell me why this work
          <?php
          $string = "uno.jpg due.jpg tre.gif quattro.gif cinque.jpg";
          $array = explode(' ', $string);
          shuffle($array) ;
          $string = implode(' ', $array);
          echo $string ;
          ?>
          >
          and this not work
          <?php
          if ($handle = opendir('banner/')) {
          while (false !== ($file = readdir($handle ))) {
          if ($file != "." && $file != "..") {
          $file = $file . " ";
          Because a file might have a space in it, and it might be a directory? It's
          quite nonsense to keep juggling between an array & a string.

          <?php
          if ($handle = opendir('banner/')) {
          $files = array();
          while (false !== ($file = readdir($handle ))) {
          if (is_file($file) ) $files[] = $file;
          }
          shuffle($files) ;
          echo implode(' ',$files);
          closedir($handl e);
          }
          ?>
          --
          Rik Wasmus

          Comment

          • pt36

            #6
            Re: sorting random values from a string

            Sorry Rik but this code not display nothing in my page
            (in the directory banner I have some file)

            <?php
            if ($handle = opendir('banner/')) {
            $files = array();
            while (false !== ($file = readdir($handle ))) {
            if (is_file($file) ) $files[] = $file;
            }
            shuffle($files) ;
            echo implode(' ',$files);
            closedir($handl e);}

            ?>
            I made mistake ?
            Thanks


            Comment

            • Rik Wasmus

              #7
              Re: sorting random values from a string

              On Thu, 06 Sep 2007 18:12:40 +0200, pt36 <key.alberto@gm ail.comwrote:
              Sorry Rik but this code not display nothing in my page
              (in the directory banner I have some file)
              >
              <?php
              if ($handle = opendir('banner/')) {
              $files = array();
              while (false !== ($file = readdir($handle ))) {
              if (is_file($file) ) $files[] = $file;
              }
              shuffle($files) ;
              echo implode(' ',$files);
              closedir($handl e);}
              >
              ?>
              I made mistake ?

              Nope, I did :P

              <?php
              $dir = './banner';
              if ($handle = opendir($dir)) {
              $files = array();
              while (false !== ($file = readdir($handle ))) {
              if (is_file($dir.'/'.$file)) $files[] = $file;
              }
              shuffle($files) ;
              echo implode(' ',$files);
              closedir($handl e);}

              ?>


              --
              Rik Wasmus

              Comment

              • pt36

                #8
                Re: sorting random values from a string

                Thanks Rik now work well
                Have good day

                Comment

                Working...