Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random image link 2

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
I'm trying to create a scrolling marquee that scrolls random images including links. Unfortunaltey I keep running into problems with each of my loops!

There are several arrays (1 per image) that contains height, width, title, image-url & link-url. I'm trying to get these images to all appear on the page, but in a random order each time the page is loaded. For simplicities sake I have only included 3 arrays, but the idea is that there could be any number.

I was kind of hoping someone could take a look at the following & let me know how to fix it. (please note there are several loops that have been commented out where I have been trying to fix this myself).

Code:
<?php

$startnum = 1;
$endnum = 3;

$items = array(	
1 => array(		
'href' => 'dragon.php',		
'title' => 'Dragon',		
'src' => 'includes/images/misc/alfa-146-jtd.jpg',
'alt' => 'Dragon',
'width' => '100',
'height' => '100'),	

2 => array(		
'href' => 'forte.php',		
'title' => 'forte',	
'src' => 'includes/images/misc/forte.jpg',		
'alt' => 'Forte',
'width' => '110',
'height' => '80'),	

3 => array(		
'href' => 'castrol.php',		
'title' => 'castrol',		
'src' => 'includes/images/misc/castrol.jpg',		
'alt' => 'Castrol',
'width' => '115',
'height' => '50'),);


$item = $items[rand($startnum,$endnum)];

//foreach ($items as $value) {
//echo '<tr><td class="infoBoxContents" valign="top"></td><td class="infoBoxContents"><center>';
//echo '<a href="' . $item['href'] . '" title="' . $item['title'] . '"><img src="' . $item['src'] . '" alt="' . $item['alt'] . '" width="' . $item['width'] . '" height="' . $item['height'] . '"></a>';
//echo '<br><br><br><br></center></td></tr>';
//};

//$ourrandnum = $item;
//
//
//for ($item; $item<=$endnum; $item++)
//{
//echo '<a href="' . $item['href'] . '" title="' . $item['title'] . '"><img src="' . $item['src'] . '" alt="' . $item['alt'] . '"></a>';
//}
//;
//
//do {
//echo '<a href="' . $item['href'] . '" title="' . $item['title'] . '"><img src="' . $item['src'] . '" alt="' . $item['alt'] . '"></a>';
//$item = $item++;
//}
//while ($item<$endnum);


?>
 
I think there is a function to randomise arrays already included in PHP...

Yep, the function that will make your life easy is called shuffle:


-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Code:
//shuffle the master array randomly
shuffle($items);

//now output them
echo "<table>\r\n";
foreach ($items as $item){
	$output .= <<<HTML
	<tr>
		<td class="infoBoxContents" valign="top"></td>
		<td class="infoBoxContents" style="text-align:center; padding-bottom:30px;">
				<a 	href="{$item['href']}" 
					title="{$item['title']}">
					<img 	src="{$item['src']}" 
							alt="{$item['alt']}" 
							width="{$item['width']}" 
							height="{$item['height']}">
				</a>
		</td>
	</tr>

HTML;
} //end the foreach loop
echo "</table>\r\n";
 
sorry - should have checked for interim posts.
 
Thanks, that's great. I'm getting closer:

Code:
$keys = array_keys($items);
shuffle($keys);

foreach ($keys as $key) {

  $arr_elem = $arr[$key];
echo '<tr><td class="infoBoxContents" valign="top"></td><td class="infoBoxContents"><center>';
echo '<a href="' . $item['href'] . '" title="' . $item['title'] . '"><img src="' . $item['src'] . '" alt="' . $item['alt'] . '" width="' . $item['width'] . '" height="' . $item['height'] . '"></a>';
echo '<br><br><br><br></center></td></tr>';

}

produces:

Code:
<tr><td class="infoBoxContents" valign="top"></td><td class="infoBoxContents"><center><a href="" title=""><img src="" alt="" width="" height=""></a><br><br><br><br></center></td></tr><tr><td class="infoBoxContents" valign="top"></td><td class="infoBoxContents"><center><a href="" title=""><img src="" alt="" width="" height=""></a><br><br><br><br></center></td></tr><tr><td class="infoBoxContents" valign="top"></td><td class="infoBoxContents"><center><a href="" title=""><img src="" alt="" width="" height=""></a><br><br><br><br></center></td></tr></table>
But where are my array elements???
 
you don't want to shuffle just the keys - shuffle the whole array. take a look at the code I posted.
 
Also used
foreach ($keys as $item) {
but getting the same result.

Jpadie. I tried using your code, but it doesn't appear to be working. nothing is being produced / outputted.

I do prefer your code. It looks much neater, but I'm still a bit of a newb at this PHP stuff, which is why I was shuffling the wrong bits. I'll have another go.
 
Thankyou both. Fixed it!!
Code:
shuffle($items);

foreach ($items as $item) {
echo 'my html & array stuff here

Hooray!
 
my fault. change the $output.= to an echo

Code:
//shuffle the master array randomly
shuffle($items);

//now output them
echo "<table>\r\n";
foreach ($items as $item){
    [red]echo[/red] <<<HTML
    <tr>
        <td class="infoBoxContents" valign="top"></td>
        <td class="infoBoxContents" style="text-align:center; padding-bottom:30px;">
                <a     href="{$item['href']}"
                    title="{$item['title']}">
                    <img     src="{$item['src']}"
                            alt="{$item['alt']}"
                            width="{$item['width']}"
                            height="{$item['height']}">
                </a>
        </td>
    </tr>

HTML;
} //end the foreach loop
echo "</table>\r\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top