random hier möglich?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • random hier möglich?

    Hi ihr,

    mein Problem:

    Ich holle mir jeweils die ersten Teile aus so aufgebauten Dateien:
    Part1 | a
    Part2 | b
    Part3 | c
    Part4 | d


    Ich brauche zB. nur Part1, Part2, Part3 oder Part4. abc und d brauche ich hier nicht, kann ich aber auch nicht weglassen, da es für was anderes benötigt wird.

    Ich hole die ersten Teile damit:
    PHP Code:
    <?php
    $fcontents 
    file("books/$e.html"); 
    while (list(
    $line_num,$line) = each ($fcontents)) { 
       
    $bookrand explode("|",$line); 
       echo 
    "
    $bookrand[0]
    "
    ;} ?>
    Jetzzt muss ich aber statt alle Parts aufzureihen, nur einen zufällt aus dieser Reihe.
    Wie kann ich in meinen Code eine Random einbauen, dass je nur ein Part zufällig ausgewählt wird???

    Ich habe noch diesen Code, kann aber hier nix damit anfangen, oder könnt ihrs?:
    PHP Code:
    $datei "books/$e.html";
    $file trim(implode('',file($datei))); 
    $file explode("|"$file);
    srand((double)microtime()*100000);
    $rand rand(0sizeof($file)-1);
    echo 
    $file[$rand]; 
    Ciao
    arrays sind klasse

  • #2
    wenn Du das hier machst
    PHP Code:
    <?php
    $fcontents 
    file("books/$e.html"); 
    while (list(
    $line_num,$line) = each ($fcontents)) { 
       
    $bookrand explode("|",$line); 
       
    // dann mach mal:
       
    $array[] = $bookrand[0];

    /** jetzt hast Du alle Part1 in $array
     * und jetzt nimmst Du Deine  PHP Bibel und schaust Dir die Funktion array_rand() an
    */
    ?>
    Beantworte nie Threads mit mehr als 15 followups...
    Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25

    Comment


    • #3
      danke dir für den halben code ;9

      ich hab den rest versucht, bekomm es aber nur hin, dass alle Part1's aufgezählt werden.

      arrays sind klasse

      Comment


      • #4


        könnt ihr mir bitte dabei noch helfen?

        arrays sind klasse

        Comment


        • #5
          ich hab da jetzt was hinbekommen, was zwar zufällig ausgegeben wird, mit dem code:

          PHP Code:
          <?php
          $fcontents 
          file("$e.html"); 
          while (list(
          $line_num,$line) = each ($fcontents)) { 
             
          $bookrand explode("|",$line); 
             
          $input[] = $bookrand[0]; }
          srand((double)microtime()*100000);
          $rand_keys array_rand($inputcount($input)-1);
          for(
          $i=0;count($rand_keys)>$i;$i++) {
          $endarray.=$input[$rand_keys[$i]]."<br>";

          echo 
          $endarray;


          ?>
          jetzt werden aber alle Part1, die es in der datei gibt, in zufälliger reihenfolge ausgegeben.
          Ich brauche aber nur eines dieser Parts, dass zufällig aus allen ausgewählt wird.
          Wie bekomme ich das hin?

          arrays sind klasse

          Comment


          • #6
            ick hab dat jet hier jemacht:

            PHP Code:
            <?php
            $fcontents 
            file("$e.html"); 
            while (list(
            $line_num,$line) = each ($fcontents)) { 
               
            $bookrand explode("|",$line); 
               
            $var[] = $bookrand[0]; }

            srand((double)microtime() * 10000000);
            $rand_var array_rand($var1);
            for(
            $x=0;$x<count($rand_var);$x++){
              echo 
            $var[$rand_var[$x]] . "<br>";
            }

            ?>
            nur leider funktioniert es nicht, wenn ich 1 eingebe.
            >>> $rand_var = array_rand($var, 1); <<<
            nur bei 2 oder mehr klappt es herrlich ( leider nur dann).
            also so klappt es:

            <?php
            $fcontents = file("$e.html");
            while (list($line_num,$line) = each ($fcontents)) {
            $bookrand = explode("|",$line);
            $var[] = $bookrand[0]; }

            srand((double)microtime() * 10000000);
            $rand_var = array_rand($var, 2);
            for($x=0;$x<count($rand_var);$x++){
            echo $var[$rand_var[$x]] . "<br>";
            }

            ?>

            was hab ich falsch gemacht?
            arrays sind klasse

            Comment


            • #7
              Danke, habs schon:

              PHP Code:
              <?php
              $fcontents 
              file("$e.html"); 
              while (list(
              $line_num,$line) = each ($fcontents)) { 
                 
              $bookrand explode("|",$line); 
                 
              $array[] = $bookrand[0]; }
              $anz=count($array)-1
              mt_srand((double)microtime()*1000000); 
              $rnd=mt_rand(0,$anz); 
              echo 
              "$array[$rnd]"
              ?>
              arrays sind klasse

              Comment

              Working...
              X