1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP array

Discussion in 'PHP & Perl' started by xooromat, Jul 15, 2012.

  1. xooromat

    xooromat Junior Member

    Joined:
    Feb 2, 2011
    Messages:
    141
    Likes Received:
    65
    Location:
    ger
    Hi
    i've got a problem with php... i'm totally new to php ;)

    I've got that php:
    PHP:
    <?php$file "list.txt";$random file($file);echo $random[array_rand($random)]; ?>
    and it works fine! It outputs a random number from my list.txt!
    But I need that random number 6 times in my php file. but when I use it 6 times I get 6 random numbers....

    I guess it's pretty easy to solve that problem... well i hope so!

    Thanks!
     
  2. neomaeva

    neomaeva Newbie

    Joined:
    Sep 13, 2011
    Messages:
    25
    Likes Received:
    5
    PHP:
    <?php
    $file 
    "list.txt";
    $random file($file);

    $var=$random[array_rand($random)];

    echo 
    $var;
    echo 
    $var;  

    ?>
    I think it should works ...
     
    • Thanks Thanks x 1
  3. Lyscer

    Lyscer Junior Member

    Joined:
    Jun 29, 2012
    Messages:
    109
    Likes Received:
    46
    Occupation:
    Software Engineer
    xooromat, like neomaeva pointed out, just use

    echo $var;

    in your script where ever you need the random number, as opposed to generating it over again with the $random[rand_array($random)] call. Good luck!
     
    • Thanks Thanks x 1
  4. xooromat

    xooromat Junior Member

    Joined:
    Feb 2, 2011
    Messages:
    141
    Likes Received:
    65
    Location:
    ger
    hm...

    look here is the my code:

    PHP:
      <class="right follow-button" id="follow-1991753">          <a id="follow-button-1991753" href="#" class="button primary false" onclick="return false;">        Follow      </a>      <script type="text/javascript">        $('#follow-button-1991753').click(function() {          $('#follow-1991753').addClass('ajax');          $.post("http://weheartit.com/user/1991753/follow", { _method'post' }, undefined"html")          .success(function(data) {            $('#follow-1991753').html(data);            _gaq.push(['_trackEvent''Follow''Follow''1991753'])          })          .error(function(data) {            $('#notice').html(data.responseText).show();          })          .complete(function(data) {            $('#follow-1991753').removeClass('ajax');          });        });      </script>
    The number "1991753" is where the output should go in, a number from my list.txt

    Frist I tried something like that:

    PHP:
     <p class="right follow-button" id="follow-<?php$file = "list.txt";$random = file($file);
    $var=$random[array_rand($random)];
    echo $var;   ?>">          <a id="follow-button-<?php$file = "list.txt";$random = file($file);
    $var=$random[array_rand($random)];
    echo $var;   ?>" href="#" class="button primary false" onclick="return false;">        Follow      </a> 
    But I get different numbers... I hope you know what I mean lol!

    Thanks!
     
    Last edited: Jul 15, 2012
  5. neomaeva

    neomaeva Newbie

    Joined:
    Sep 13, 2011
    Messages:
    25
    Likes Received:
    5
    PHP:
    <?php
    $file 
    "list.txt";
    $random file($file);
    $var=$random[array_rand($random)];

    ?>

    <p class="right follow-button" id="follow-<?php echo $var?>">          
    <a id="follow-button-<?php echo $var?>" href="#" class="button primary false" onclick="return false;">        Follow      </a>
    </p>
    every time you use "$random[array_rand($random)];" it gives you a random number, in your code you use it twice, that's why you got different number.
    You need to call "$random[array_rand($random)];" just one time

    good luck!
     
    • Thanks Thanks x 1
  6. xooromat

    xooromat Junior Member

    Joined:
    Feb 2, 2011
    Messages:
    141
    Likes Received:
    65
    Location:
    ger
    working! Thanks!! ;)