php help

Soldato
Joined
12 May 2007
Posts
3,896
Location
Bristol
I desperately need some help. If some kind soul is bored and wouldn't mind, I'd really appreciate it.

I have the following php to drop random items in a list.

Code:
<?php

$aHTML[0] = 'random4';
$aHTML[1] = 'random3';
$aHTML[2] = 'random2';
$aHTML[3] = 'random1';	

$iUpper = sizeof($aHTML) - 1;
$iLower = 0;
$iIndex = rand($iLower, $iUpper);

echo "<li><img src='media/images/dotArrow.jpg' alt='' />$aHTML[$iIndex]</li>"
?>

And the following html to go with it


Code:
<div id="factoidsContent">
    <ul>
      <?php include('randomFactoids.php'); ?>
      <?php include('randomFactoids.php'); ?>
      <?php include('randomFactoids.php'); ?>
    </ul>
</div>

What I want to do is have a variable in the script to determine how many list items to drop in (each one random and not a duplicate). So if I want 5 list items, I can just just change a variable in the script instead of having 5 includes in the html.

They each still have to be random and it would be nice if whichever random ones were displayed were not duplicated but that's not too important. I'll have enough variables that it shouldn't come up too often.
 
Thanks for the reply. I have almost zero experience with php which is why you might find it 'quirky'.

I've changed it to the following which is now looping it three times which is what I wanted. I'm not quite sure what you meant about unset. I'm assuming this is how I keep it from repeating the same random variable? Where would I put unset($aHTML[$iIndex]) in?


Code:
<?php

for ($i=1; $i<=3; $i++) { 

$aHTML[0] = 'random4'; 
$aHTML[1] = 'random3'; 
$aHTML[2] = 'random2'; 
$aHTML[3] = 'random1';

$iUpper = sizeof($aHTML) - 1;
$iLower = 0;
$iIndex = rand($iLower, $iUpper);

echo "<li><img src='media/images/dotArrow.jpg' alt='' />$aHTML[$iIndex]</li>"
; }

?>
 
Back
Top Bottom