Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    The First State
    Posts
    233
    Thanks
    9
    Thanked 0 Times in 0 Posts

    images not displaying with rotator script

    I know it's something stupid but right now it's eluding me and driving me crazy.

    I'm using a rotating ads script with php and jQuery. It basically looks for all files in a directory and rotates them every 5 seconds.

    Works great on the home page but as soon as I hit a page that is in a subdirectory the ad images do not show.

    This code is in an include file in the includes directory which calls the rotator:

    Code:
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    function theRotator() {
    	//Set the opacity of all images to 0
    	$('#rotator  li').css({opacity: 0.0});
    	
    	//Get the first image and display it (gets set to full opacity)
    	$('#rotator  li:first').css({opacity: 1.0});
    		
    	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    	setInterval('rotate()',5000);
    	
    }
    
    function rotate() {	
    	//Get the first image
    	var current = ($('#rotator  li.show') ?  $('#rotator  li.show') : $('#rotator  li:first'));
    
    	//Get next image, when it reaches the end, rotate it back to the first image
    	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator  li:first') :current.next()) : $('#rotator  li:first'));	
    	
    	//Set the fade in effect for the next image, the show class has higher z-index
    	next.css({opacity: 0.0})
    	.addClass('show')
    	.animate({opacity: 1.0}, 1000);
    
    	//Hide the current image
    	current.animate({opacity: 0.0}, 1000)
    	.removeClass('show');
    	
    };
    
    $(document).ready(function() {		
    	//Load the slideshow
    	theRotator();
    });
    </script>

    This code is my leftnav.php also in the includes directory:

    Code:
    <div id="adframe">
    <?php
    $files=glob("images/ads/*.jpg*", GLOB_NOSORT);
    
    $firstfile=reset($files);
    
    echo "<ul id=\"rotator\">\n";
    	foreach($files as $file){
    			if($firstfile==$file){
    				echo "\t<li class=\"show\"><a href=\"#\"><img src=\"".$file."\" width=\"\" height=\"\" alt=\"\" /></a></li>\n";
    			}else{
    				echo "\t<li><a href=\"#\"><img src=\"".$file."\" width=\"\" height=\"\" alt=\"\" /></a></li>\n";
    			}
    	}
    echo "</ul>\n";
    ?>
    </div>
    this is the line that sets the img path:

    Code:
    $files=glob("images/ads/*.jpg*", GLOB_NOSORT);
    The above works on the home page, not the sub directory pages (ex: test/index.php)

    If I do /images/ads, nothing works
    If I do absolute path http:.../images/ads nothing works


    I know it is something completely stupid but does anyone know what I'm doing wrong.

    thanks for the help

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    It seems to be that it's a path issue to your ads. I know why your relative path doesn't work. When you go to a subdirectory, the images directory does not exist in there. You need to use absolute paths, although you're saying that's not working either. That's what confuses me a bit.

    You should have:
    PHP Code:
    $files=glob("http://sitename.com/images/ads/*.jpg*"GLOB_NOSORT); 
    Does that not work for your home page also? Or only subdirectories?

    -Shane

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Try
    PHP Code:
    $files=glob($_SERVER['DOCUMENT_ROOT']."/images/ads/*.jpg*"GLOB_NOSORT); 
    (Assuming the images folder is located under the document root)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder
    Join Date
    May 2004
    Location
    The First State
    Posts
    233
    Thanks
    9
    Thanked 0 Times in 0 Posts
    neither options work. I tried the absolute path and my list never gets built:

    Code:
    <div id="adframe">
    <ul id="rotator">
    </ul>
    </div>
    I thought about the document root option before just couldn't remember the syntax. But it gives me the path to the drive? the images folder is in the root i.e. /images/ads/

    Code:
    <div id="adframe">
    <ul id="rotator">
    	<li class="show"><a href="#"><img src="D:\Hosting\4601878\html/images/ads/banner1.jpg" width="" height="" alt="" /></a></li>
    
    	<li><a href="#"><img src="D:\Hosting\4601878\html/images/ads/banner2.jpg" width="" height="" alt="" /></a></li>
    </ul>
    </div>

  • #5
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by finstah1 View Post
    I tried the absolute path and my list never gets built:
    Sorry, my fault. You cannot use absolute paths with glob.

    Try both of these. I've done this before when having to avoid absolute paths, but I can't remember what I did.
    PHP Code:
    $files=glob("/html/images/ads/*.jpg*"GLOB_NOSORT);
    $files=glob("./images/ads/*.jpg*"GLOB_NOSORT); 
    I think I've used the second one and it worked.

    -Shane

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    The First State
    Posts
    233
    Thanks
    9
    Thanked 0 Times in 0 Posts
    same issue although the second option works on the home page but not the sub pages.

    I'm not strong enough in php Is there another method I can use besides the glob?

    The one other thing I have is a config.php in the head that contains this:

    Code:
    define('SITE_ROOT', realpath(substr(realpath(__FILE__), 0, strrpos(realpath(__FILE__), DIRECTORY_SEPARATOR)+1) . '../') . '/');
    define('SITE_PATH', str_replace('\\', '/', substr(SITE_ROOT, strlen($_SERVER['DOCUMENT_ROOT']))));
    define('SITE_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/' . str_replace('\\', '/', substr(SITE_ROOT, strlen($_SERVER['DOCUMENT_ROOT'])+1)));
    define('SITE_INC', SITE_ROOT . 'includes/'); // Includes directory
    can I define the images directory here?

    This stuff definitely took me off my pedestal and knocked me down a few notches. I'm comfortable with JS but PHP is an entirely new ballgame to me. I want to learn but I'm so damn frustrated.
    Last edited by finstah1; 07-31-2009 at 03:42 PM.

  • #7
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by finstah1 View Post
    Is there another method I can use besides the glob?
    See readdir. The examples are helpful. Modify to feed image files into an array. Check the comments for various tweaks.
    Are you a Help Vampire?

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    This function I posted the other day will return an array of files in a directory with a couple of minor alterations.

    Dynamic files open in php

  • #9
    Regular Coder
    Join Date
    May 2004
    Location
    The First State
    Posts
    233
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I appreciate that.

    However I've got the code there.

    I just can't get the %@#$&%$*& path right for the sub pages.

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    The First State
    Posts
    233
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    This function I posted the other day will return an array of files in a directory with a couple of minor alterations.

    Dynamic files open in php
    do I set the directory path via $dir?

    could there be other issues with the config code I posted?

    Any other suggestions?

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    I've updated the function. $dir is the directory in which the function should scan for files, and $uri is the URI which will prefix the returned filenames. Do something along the lines of the following to generate the array containing the files:

    Code:
    $images = readfiles('images/ads/', 'http://example.org/');

    $images will then be an array containing the full URI for each image.

    Code:
    <?php
    
    // Create a file called list_function.php and place all of this code in that file.
    
    function readfiles($dir, $uri = false)
    {
            $rdata = array();
            $exfiles = array('.', '..', '.htaccess');
    
            if (is_dir($dir))
            {
                    $listdir = opendir($dir);
    
                    while (false !== ($file = readdir($listdir)))
                    {
                            if (is_file($dir.$file) && !in_array($file, $exfiles))
                            {
                                    if (!$uri)
                                    {
                                        $rdata[] = $file;
                                    }
                                    else
                                    {
                                        $rdata[] = $uri.$file;
                                    }
                            }
                    }
                    closedir($listdir);
    
                    return $rdata;
            }
            return false;
    }
    
    ?>
    Last edited by MattF; 07-31-2009 at 09:30 PM.

  • Users who have thanked MattF for this post:

    finstah1 (08-03-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •