PHP array

xooromat

Junior Member
Joined
Feb 2, 2011
Messages
145
Reaction score
65
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!
 
PHP:
<?php
$file = "list.txt";
$random = file($file);

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

echo $var;
echo $var;  

?>

I think it should works ...
 
PHP:
<?php
$file = "list.txt";
$random = file($file);

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

echo $var;
echo $var;  

?>

I think it should works ...

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!
 
hm...

look here is the my code:

PHP:
  <p 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:
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!
 
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!

working! Thanks!! ;)
 
Back
Top