Roybot06 Posted August 15, 2007 Share Posted August 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/ Share on other sites More sharing options...
willpower Posted August 16, 2007 Share Posted August 16, 2007 you need to use multidimensional arrays this is where each array key has another set of keys and values attached. see http://www.desilva.biz/arrays/multidimen.html Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-325263 Share on other sites More sharing options...
Roybot06 Posted August 16, 2007 Author Share Posted August 16, 2007 I tried that, but I need them to randomize in sync. And I can't figure a way to tell the array to place a specific method or tag somewhere...do arrays only take a character string or can I tell it to take the "<a href =\"#"> and move it with the pictures? Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-325272 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-325405 Share on other sites More sharing options...
keeB Posted August 16, 2007 Share Posted August 16, 2007 <?php $total = count($array_with_ads); $randomAd = rand(0, $total) print $array_with_ad[$randomAd]; ?> This what you want? Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-325438 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 I don't think it is. Sounded like he needed an ad (image) and a url. Your code will just give one. I'm pretty sure that's what he/she had to begin with. Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-325714 Share on other sites More sharing options...
LiamProductions Posted August 16, 2007 Share Posted August 16, 2007 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-325728 Share on other sites More sharing options...
Roybot06 Posted August 22, 2007 Author Share Posted August 22, 2007 Thank you guys, I have come up with a solution for this. It took a few days, but I couldn't thank you all enough. Now to my next problem! Quote Link to comment https://forums.phpfreaks.com/topic/65149-solved-dilemna-with-arrays/#findComment-331259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.