Jump to content

Recommended Posts

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.

}

Link to comment
https://forums.phpfreaks.com/topic/191496-array/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/191496-array/#findComment-1009458
Share on other sites

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>');

    }

  }

 

 

Link to comment
https://forums.phpfreaks.com/topic/191496-array/#findComment-1009471
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/191496-array/#findComment-1009474
Share on other sites

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);

}

 

Link to comment
https://forums.phpfreaks.com/topic/191496-array/#findComment-1010820
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.