Jump to content

Please help with image rotator


lingo5

Recommended Posts

Hi,

I have this PHP script tha displays all images in a given directory:

 

<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "banners/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 20);

//display images
foreach ($imgs as $img) {
    echo "<img src='$img' /> ";
}
?>

 

This works fine. My problem is when I try to use the $img var in the following Jquery banner rotator:

<body>
    <div id="wrapper">
    
        <a href="http://dev7studios.com" id="dev7link" title="Go to dev7studios">dev7studios</a>

        <div class="slider-wrapper theme-default">
        <div class="ribbon"></div>
            <div id="slider" class="nivoSlider">
                <img src="<?php echo $img />" alt="" /> // this is where my images should go

            </div>
            <div id="htmlcaption" class="nivo-html-caption">
                <strong>This</strong> is an example of a <em>HTML</em> caption with <a href="#">a link</a>.
            </div>
        </div>

    </div>
  <script type="text/javascript">
    $(window).load(function() {
        $('#slider').nivoSlider();
    });
    </script>
</body>

 

The Jquery script is premade but quite simple.

Please can anyone tell me how I could do this?

Thanks

Link to comment
Share on other sites

in the second example, where are you setting the $img variable? can we see that?

 

just noticed this...

 

<img src="<?php echo $img />" alt="" /> // this is where my images should go

 

should probably be

 

<img src="<?php echo $img; ?>" alt="" /> // this is where my images should go

Link to comment
Share on other sites

ooops ...thanks smerny, that fixed part of the problem.

The thing is that my banner rotator is now rotating just the first image.....I want it to rotate all images in the "banners" dir.

:(

Link to comment
Share on other sites

Thanks a lot for your help smerny...but I was just being blind.

I forgot to loop through the images in the banners dir....duh

This is how I got it to work:

<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "banners/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 20);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.nivo.slider.pack.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
    <link rel="stylesheet" href="themes/default/default.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="themes/pascal/pascal.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="themes/orman/orman.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="rotator_css/nivo-slider.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="rotator_css/style.css" type="text/css" media="screen" />

</head>

<body>
    <div id="wrapper">
    
        <a href="http://dev7studios.com" id="dev7link" title="Go to dev7studios">dev7studios</a>

        <div class="slider-wrapper theme-default">
        <div class="ribbon"></div>
            <div id="slider" class="nivoSlider">
            <?php 
                foreach ($imgs as $img) {
    			echo "<img src='$img' /> ";
			}
		?>

            </div>
            <div id="htmlcaption" class="nivo-html-caption">
                <strong>This</strong> is an example of a <em>HTML</em> caption with <a href="#">a link</a>.
            </div>
        </div>

    </div>
  <script type="text/javascript">
    $(window).load(function() {
        $('#slider').nivoSlider();
    });
    </script>
</body>
</html>

Link to comment
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.