Phredster Posted February 9, 2010 Share Posted February 9, 2010 I am trying to modify another persons script, it randomly changes the header graphic. I would like the script to choose a header graphic based upon the Month Day if it exists, and if not then just choose one randomly. The script uses an ini configuration file to hold all the filenames, etc this is what I have. But I don't have enough understanding of arrays. Any insight or help would be appreciated. $day = date('Mj'); if (array($images, $day)) { echo "Got Image"; $img = array($images); } else { $img = array_rand($images); <---- this was the original code that randomly picked the graphic file from an array. } Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/ Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 im not quite sure you understand what the array() function does. it creates an array of the elements you pass into the function. perhaps you want to use isset(), something like. Which array holds all the filenames?. assuming $images is the array, and the keys are defined as the days (in the format of Mj, like you have in the date function) something like the following may be what your after. if (isset($images[$day]); if not then try doing echo "<pre>"; printt_r($images); echo "</pre>"; and tell me the structure of the array Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/#findComment-1009458 Share on other sites More sharing options...
Phredster Posted February 9, 2010 Author Share Posted February 9, 2010 Wow, you replied quick, I appreciate it. To help you better understand what I am working with, I will include the ini config file and the original function script that I started with. The original author no longer supports it. config ini -- [Feb10] src = /images/headers/front-01.png alt = Header Graphic url = http://www.stoll.us title = Graphic id = frontphoto [Front-02] src = /images/headers/front-02.png alt = Header Graphic url = http://www.stoll.us title = Graphic id = frontphoto and here is the original function --- function showImage( $ini=null ) { global $IMG_CONFIG_FILE; # if no custom ini file has been specified, use the default $ini_file = $ini ? $ini : $IMG_CONFIG_FILE; # read the config file into an array or die trying $images = @parse_ini_file($ini_file,true); if (! $images) { die('Unable to read ini file.'); } # pick a random image from the parsed config file $img = array_rand($images); # get the selected image's css id if one exists $id = $images[$img]['id'] ? sprintf( ' id="%s" ', $images[$img]['id'] ) : ''; # get the selected image's css class if one exists $class = $images[$img]['class'] ? sprintf( ' class="%s" ', $images[$img]['class'] ) : ''; # get selected image's dimensions $size = @getimagesize( $images[$img]['src'] ); # if an url was specified, output the opening A HREF tag if ( $images[$img]['url'] ) { printf( '<a href="%s" title="%s">', $images[$img]['url'], $images[$img]['title'] ); } # output the IMG tag printf( '<img src="%s" alt="%s" %s %s%s/>', $images[$img]['src'], $images[$img]['alt'], $size[3], $id, $class ); # if an url was specified, output the closing A HREF tag if ( $images[$img]['url'] ) { echo('</a>'); } } Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/#findComment-1009471 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 in that function, it looks like $img is the key. I'm assuming the keys are of the format date('Mj'), which, for example, today would be feb9. in that case, doing something like $img = date('Mj'); if (!isset($images[$img])){ $img = array_rand($images); } and replacing the line $img = array_rand($images); should make it work. Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/#findComment-1009474 Share on other sites More sharing options...
Phredster Posted February 9, 2010 Author Share Posted February 9, 2010 Thanks!! That seems to work great! I wasn't even close to coming up with that solution. I appreciate your help. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/#findComment-1009484 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 No problem, if your topic is solved, you can click the mark solved button at the bottom of the thread Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/#findComment-1009496 Share on other sites More sharing options...
Phredster Posted February 11, 2010 Author Share Posted February 11, 2010 It works as I have said earlier, but how would I make it not randomly select any header graphic for other days, like it will display a valentines banner on Feb14, but it could still show it on other days if it is selected by the array_rand($images). I was thinking in the config file, adding a random = no, and have it check to see if it could be part of the random choices, but then I don't know what changes I would have to make to the code below. $img = date('Mj'); if (!isset($images[$img])){ $img = array_rand($images); } Quote Link to comment https://forums.phpfreaks.com/topic/191496-array/#findComment-1010820 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.