mattyvx Posted December 16, 2009 Share Posted December 16, 2009 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 https://forums.phpfreaks.com/topic/185375-random-select-image-with-a-difference/ Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 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 https://forums.phpfreaks.com/topic/185375-random-select-image-with-a-difference/#findComment-978625 Share on other sites More sharing options...
steveboj Posted December 16, 2009 Share Posted December 16, 2009 I did something similar to this once with a simple array and the rand() function. Alternatively you could put the your last 3 lines in an array, then use the shuffle() function and display just the first 2 lines. Link to comment https://forums.phpfreaks.com/topic/185375-random-select-image-with-a-difference/#findComment-978632 Share on other sites More sharing options...
mattyvx Posted December 17, 2009 Author Share Posted December 17, 2009 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 https://forums.phpfreaks.com/topic/185375-random-select-image-with-a-difference/#findComment-979017 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.