Jump to content

Random select image - with a difference!


mattyvx

Recommended Posts

Hi!

 

I have a small problem in that I have various "how to guides" on my sites.  Each guide has a picture linking to it.

 

Currently I have a file called guides.html which will be included on all my pages;

 

<a href="/search.php">
<img src="/images/sqsearch.gif" /></a>

<a href="/guides/a.php" >
<img src="/images/a.gif"  /></a>

<a href="/guides/b.php" >
<img src="/images/b.gif" /></a>

<a href="/guides/c.php" >
<img src="/images/c.gif" /></a>

 

Example the above script shows 4 images and links to four pages.

 

I want only three images to be shown with the following criteria:

 

The first image/link must always be search.php.

 

The second two images can be any of the remaing links.

 

Im guessing I need to put the above script into an array? Please assist, I dont think the solution is too complicated.

 

Thanks

 

Link to comment
Share on other sites

this should help you get started

 

<?php
$str = '
<a href="/search.php">
<img src="/images/sqsearch.gif" /></a>';

$links = array('a', 'b', 'c', 'd', 'e', 'f', 'h');
$stable = 'a'; // link you want stable 

$str .= "<a href=\"/guides/{$stable}.php\" ><img src=\"/images/{$stable}.gif\"  /></a>";

$index = array_search($stable, $links);
if ($index !== false) {
    unset($links[$index]);
}
shuffle($links);

for ($i=0; $i<2;$i++) {
    $str .= "<a href=\"/guides/{$links[$i]}.php\" ><img src=\"/images/{$links[$i]}.gif\"  /></a>";
}

echo $str;
?>

Link to comment
Share on other sites

thanks! Abit of tweaking and I have this as my final solution;

 

<?php

echo "<a href=\"/guides/search.php\" title=\"Search\">
      <img src=\"/images/search.gif\" alt=\"Search\" /></a>";

$links = array( 
'horray for the array', 
'how to do something pointless', 
'a guide on something else', 
);

if(!isset($no)){$no = "2";}else{$no = count($links);}

shuffle($links);

for ($i=0; $i<$no;$i++) {
    $str .= "<a href=\"/guides/{$links[$i]}.php\" title=\"{$links[$i]}\" >
             <img src=\"/images/{$links[$i]}.gif\" alt=\"{$links[$i]}\" /></a>";
}

echo $str;
?>

 

There is one page which i would like to display all the links, on this one page just before the include"" I can set the $no variable to anything and it will show all the guides!

 

Working great thanks alot!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.