Forum Moderators: coopster
<?php
$myrandnum = array();
for ($i=0;$i<7;$i++) {
$mynum = rand(1,7);
if (!in_array($mynum,$myrandnum)) $myrandnum[] = $mynum;
else $i--;
echo ($mynum . ', ');
} ?>
What am I doing wrong?
Also, I'm trying to use the above to include the random numbers to call a PHP function--seven copies of the function, each using a different number. It almost works.
This code...
<?php
$myrandnum = array();
for ($i=0;$i<5;$i++) {
$mynum = 'pl_postlist("PostsLatestHOME_' . rand(1,7) .'")';
if (!in_array($mynum,$myrandnum)) $myrandnum[] = $mynum;
else $i--;
echo ($mynum . '/n');
} ?>
...returns this the following as rendered HTML instead of executing the PHP function...
("PostsLatestHOME_3")/npl_postlist("PostsLatestHOME_1")/npl_postlist("PostsLatestHOME_1")/npl_postlist("PostsLatestHOME_5")/npl_postlist("PostsLatestHOME_5")/npl_postlist("PostsLatestHOME_5")/npl_postlist("PostsLatestHOME_3")/npl_postlist("PostsLatestHOME_1")/npl_postlist("PostsLatestHOME_4")/npl_postlist("PostsLatestHOME_2")/n
And, again, it returns more than the desired 7 results.
Help, please?
[edited by: iamPariah at 11:44 pm (utc) on Aug. 19, 2008]
<?php
$myrandnum = array();
for ($i=0;$i<5;$i++) {
$mynum = 'pl_postlist("PostsLatestHOME_' . rand(1,7) .'")';
if (!in_array($mynum,$myrandnum)) $myrandnum[] = $mynum;
else $i--;
} ?><?php
print_r($myrandnum);
?>
...returns this in the rendered php/html....
Array ( [0] => pl_postlist("PostsLatestHOME_2") [1] => pl_postlist("PostsLatestHOME_3") [2] => pl_postlist("PostsLatestHOME_4") [3] => pl_postlist("PostsLatestHOME_1") [4] => pl_postlist("PostsLatestHOME_7") )
Did I do something wrong?
No, that's just the contents of the array. As you can see it's filled with 7 functions in a random order.
**when I try your code (below), nothing happens--no output in the page source
**echo pl_postlist('PostsLatestHOME_'.$mynum)
Thats because you are outputting the results of the function pl_postlist();
If you want to see the actual name of the function being called change the echo to:
<?php
$myrandnum = range(1,7);
shuffle($myrandnum);
foreach($myrandnum as $mynum){
echo "pl_postlist('PostsLatestHOME_$mynum')";
}
?>
It realty looks like yo should read up on your php/coding
Heh. I'm trying.
Maybe I'm going about asking for help the wrong way. I've tried to make it work myself a few ways, and I couldn't do it. Maybe if I'm a little more explicit in my intended end result it would be easier to help me get there.
I have to execute the following function 7 unique times, replacing "N" with a numeral 1-7, so as to load the function with a differently numbered variable each time. And, I need the results to be randomized with each execution--thus, it might load 7,5,3,2,1,4,6 or it might load 1,2,3,4,5,6,7. (I really hope I'm making sense here.)
<?php
if( function_exists("pl_postlist") )
pl_postlist("PostsAuthorHOME_N");
?>
Any advice?
$myrandnum = range(1,7); //fill array with 1 .. 7shuffle($myrandnum); //randomize the order
foreach($myrandnum as $N){
//loop through the array and call the function 7 times in the random order
pl_postlist('PostsLatestHOME_'.$N);}
Still, your code helped me with an alternative method for accomplishing the task. Thank you very much!