Ricky55 Posted May 30, 2017 Share Posted May 30, 2017 Hi guys I have a series of images that are wrapped with links. I need these to display in a random order with each page refresh. I've had a quick look for some code that I can customise but I'm drawing a blank. I just have <a href="http://www.diy.com"><img src="/_/images/where-to-buy/b-and-q.png" alt="B&Q"></a> Obviously I need to keep the href image source and alt tag together. Is there a way to create an array or switch statement and then output it as needed? Thanks guys Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2017 Share Posted May 30, 2017 Put everything into an array and pick a random one to show. Link to comment Share on other sites More sharing options...
Ricky55 Posted May 30, 2017 Author Share Posted May 30, 2017 I've just found this code. How would I echo this out into my html? function retailerGet($name=null){ $retailers = array('amazon'=>array('href'=>'http://www.amazon.co.uk/', 'src'=>'amazon.png', 'alt'=>'Amazon'), 'argos'=>array('href'=>'http://www.argos.co.uk/', 'src'=>'argos.png', 'alt'=>'Argos'), 'asda'=>array('href'=>'http://www.asda.co.uk/', 'src'=>'asda.png', 'alt'=>'ASDA')); if($name==null){ $keys = array_keys($retailers); shuffle($keys); return $retailers[$keys[mt_rand(0,count($retailers)-1)]]; }else{ return $names[$name]; } } print_r(retailerGet()); Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2017 Share Posted May 30, 2017 Are you looking for something that does 99% or more of the work for you? Or are you looking to solve the problem? Because there's not actually that much work involved in this. A couple minutes. 1. Make an array that has all the data for each link that you need. The multidimensional approach that sample code uses is good. Like array( array("link" => "http://www.diy.com/", "image" => "b-and-q.png", "name" => "B&Q"),2. Use array_rand() to get a random key, then get the data using that key. shuffle()ing the entire array is a waste and the person who wrote retailerGet didn't understand that.3. ??? 4. Profit. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.