Need help modifying PHP script

They have: 68 posts

Joined: Jun 2006

I'm trying to combine a part of one script into another PHP script I'm trying to use. The script functions as displaying all images in a directory along with their file names beneath and what I'd like to do is have the script take off the extension.

Here's the script that creates the gallery:

<?php
Header
(\"content-type: application/x-javascript\");

function returnimages(
$dirname=\".\") {
  
$pattern=\"\.(jpg|jpeg|png|gif|bmp)$\";
  
$files = array();
  
$curimage=0;
   if(
$handle = opendir($dirname)) {
       while(false !== (
$file = readdir($handle))){
               if(eregi(
$pattern, $file)){
        
$filedate=date (\"M d, Y H:i:s\", filemtime($file));
                 echo 'galleryarray[' .
$curimage .']=[\"' . $file . '\", \"'.$filedate.'\"];' . \"\n\";
                
$curimage++;
               }
       }

       closedir(
$handle);
   }
   return(
$files);
}

echo \"var galleryarray=new Array();\" . \"\n\";
returnimages();

?>

And I have another script working a different function that already does what I'm looking for, it's just not in the above one.. and my attempts at adding it haven't worked out.

Here's the other script:

<?php
ini_set
('display_errors', 1);
error_reporting(E_ERROR | E_WARNING | E_PARSE);


   
define('SERVER_ROOT','C:/Program Files/Apache Software Foundation/Apache2.2/htdocs'); // Server path to domainname

   
define('WEB_PATH','/rotate/birthdays'); // Path to images under domainname

   

    // In the above example, <a href="http://www.domain.com/" title="http://www.domain.com/">http://www.domain.com/</a> would use files

    // located on the server in /home/httpd/html and the images to

    // be called by this script are at <a href="http://www.domain.com/pics/staff
" title="http://www.domain.com/pics/staff
">http://www.domain.com/pics/staff
</a>   

   
$files = array();

   

$d = dir(SERVER_ROOT . WEB_PATH);
    while ((
$filename = $d->read()) !== false)
    {
       
$ext = strtolower(substr($filename,-4));
        if (
$ext=='.jpg' || $ext=='.gif' || $ext=='.png' || $ext=='.pict')
           
$files[] = $filename;
    }
   
$d->close();


   

    if (count(
$files)<1) die ('No Images Found');

   

    // Seed Random Number

        list(
$usec,$sec) = explode(' ', microtime());

        srand((float)
$sec + ((float) $usec * 100000));

   

   
$imgnum = rand(1,count($files));

   

    if (isset(
$files[0]))

       
$file2use = $files[$imgnum-1];

    else

       
$file2use = $files[$imgnum];

       

    // START GET DISPLAY NAME

    // filename (no path) is in
$file2use

       

   
$dispname = substr($file2use,0,-4); // Gets rid of the .jpg extension

   
$dispname = str_replace('_',' ',$dispname); // Converts underscores to spaces


   

    // END GET DISPLAY NAME   

    // Displayed name is in
$dispname

   

   
$imgpath = WEB_PATH . \"/\" . $file2use;

   
?>

The finished product is going to look something like this

And as a side note... if you click different pages of pictures... the styling goes away and I'm not sure why.. but the aforementioned problem is the one I really am needing help with.

Thanks all.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

For what you are wanting, you need to not only modify the PHP file you have here, but also the javascript that uses it.

In the PHP code you gave (1st one), where you have the following line:

[="Courier New"]echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";[/]

add these two lines right before it

[="Courier New"]$dispname = substr($file,0,-4); // Gets rid of the .jpg extension
$dispname = str_replace('_',' ',$dispname); // Converts underscores to spaces
[/]

and then modify that line to be

[="Courier New"]echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'", "'.$dispname .'"];' . "\n";[/]

Then on the webpage, in the javascript, change this line:
[="Courier New"]tempcontainer+=''+galleryarray[i][0]+''[/]
to be
[="Courier New"]tempcontainer+=''+galleryarray[i][2]+''[/]

-Greg

They have: 68 posts

Joined: Jun 2006

Works great, many thanks Greg.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.