0

How would I join these two strings and create random link based on the entry list.

// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";

// SHOW ONE AD LINK

srand ((double) microtime() * 1000000);
$adlink[] + $adlinkpic[] = rand(0,count($quotes)-1);
echo "<a href='$adlink'><img src='$adlinkpic' />";

// SHOW TWO AD LINKS /cannot be same

// code here
2
  • Can you explain a bit clearer what you are trying to do? Commented Feb 1, 2012 at 11:04
  • I thing, He is trying to get random image & link from listed links. is it correct ? Commented Feb 1, 2012 at 11:06

2 Answers 2

3

It is easy to put all of your links in an (associative) array then use array functions to manipulate them:

<?php
$ad = array(
    array(
        "url" => "http://www.****.com/sale.",
        "img" => "http://www.***-cdn.com/blogAssets/ad/1.jpg"
    ),
    array(
        "url" => "http://www.*****.com/sale.",
        "img" => "http://www.**-cdn.com/blogAssets/ad/2.jpg"
    ),
    array(
        "url" => "http://www.**.com/product.php",
        "img" => "http://www.**-cdn.com/blogAssets/ad/3.jpg"
    ),
    array(
        "url" => "http://www.**.com/wedding-boutique.",
        "img" => "http://www.**-cdn.com/blogAssets/ad/4.jpg"
    ),
    array(
        "url" => "http://www.**.com/made-to-measure-service.",
        "img" => "http://www.**-cdn.com/blogAssets/ad/5.jpg"
    )
    // more ads
);

$id = array_rand($ad); // choose a random index from the array
echo "<a href=\"{$ad[$id]['url']}\"><img src=\"{$ad[$id]['img']}\" /></a>\n";
unset($ad[$id]);       // remove the chosen one so that it is not displayed on next pass

$id = array_rand($ad);
echo "<a href=\"{$ad[$id]['url']}\"><img src=\"{$ad[$id]['img']}\" /></a>\n";
unset($ad[$id]);
Sign up to request clarification or add additional context in comments.

4 Comments

Nice, but you should make a loop around the echo block instead of copy paste.
thanks really nice. although loop would have been better but I like it. Repped both
Sure; just wrap the three lines inside a loop that runs twice. I separated them assuming that the ads might need to be displayed on two separate locations on the page.
I am not using a loop but future people I recommend the code below (just loop the loop part) this is some nice code actually and I thank you very much!
1

I'd be tempted to do it something like this:

<?php
  // Add a link and the associated image //
  $adlink1="http://www.****.com/sale.php";
  $adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
  $adlink2="http://www.*****.com/sale.php";
  $adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
  $adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
  $adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
  $adlink4="http://www.**.com/wedding-boutique.php";
  $adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
  $adlink5="http://www.**.com/made-to-measure-service.php";
  $adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";

  $links = array();
  $links[0]=array('link'=>$adlink1,'pic'=>$adlinkpic1);
  $links[1]=array('link'=>$adlink2,'pic'=>$adlinkpic2);
  $links[2]=array('link'=>$adlink3,'pic'=>$adlinkpic3);
  $links[3]=array('link'=>$adlink4,'pic'=>$adlinkpic4);
  $links[4]=array('link'=>$adlink5,'pic'=>$adlinkpic5);

  $alreadyAdded=array();
  for ($i=0;$i<2;$i++) {
    $added = false;
    while (!$added) {
      // generate random number
      $rand = mt_rand(0,4);
      if (!in_array($rand,$alreadyAdded)) {
        echo "<a href='".$links[$rand]['link']."'><img src='".$links[$rand]['pic']."' />";
        $added = true;
        $alreadyAdded[]=$rand;
      }
    }
  }

Edit: noticed you wanted more than 1 outputted, updated code to reflect.

2 Comments

Don't forget to unset that variable from the array of items so that it doesn't get re-used :)
thanks really nice. although I went for the double array above - repped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.