Jump to content

Randomise content without a database


Ricky55

Recommended Posts

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

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

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

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.