Jump to content

[SOLVED] Dilemna with Arrays


Roybot06

Recommended Posts

Okay I am creating an page using a .php array to randomize a few ads on the page. Every time it is refreshed it will either mix the three ads on the page or throw in another one. I had the idea of linking these pictures to their respective sites, but if I used an anchor tag out side the :

print "<a href=\"#"><img src=\"images/".$ad_array[0]."_120x60.jpg\"></a><br><br>\n";

 

It will give me a fixed link, but I won't have a way of having the link move with the randomizing of the pictures. I was thinking on the lines of creating another array to move with the random pictures.

 

I have the array set to present a word in front of the picture extension (i.e., "red" in the array will make the picture red_120x60.jpg, etc.) , and the array's code:

 

$ad_array = array("red", "green", "blue", "magenta");

 

Does anyone have any suggestions, or can they tell me if what I'm thinking of is possible?

 

Either one will be greatly valued.

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/
Share on other sites

I don't understand your response but a multidimensional array will work.

 

$arr = Array();

$arr[] = Array("www.thisisaurl.com", "thisisanimage.jpg");

 

So then you randomly select an index between 0 and the maximum number of array elements - 1. At this index you do this:

 

$url = $arr[$randomlyselectedindexhere][0];

$img = $arr[$randomlyselectedindexhere][1];

 

You've now got your add and it's associated link.

<?php

$rand = rand(1,4);

case $rand:
     case 1:
     echo "Imageurl.gif";
     break;
     case 2:
     echo "imageurl2.gif2;
     break;
    default:
    echo "No image was produced";

 

not sure, if that case works but i'm sure the rand() function is giving you idea how you could do it

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.