Jump to content

[SOLVED] scrolling images on webpage.


Looktrne

Recommended Posts

Ok I have a webpage and I have a folder full of thumnail images

 

so how could I get the images scrolled in order or randomly to make the home page constantly scroll the images from that image folder?

 

what is the best way to do this?

 

hope you can understand my question

 

:)

 

Paul

Link to comment
Share on other sites

Sorry,

 

<?php

function getrandom($amount, $min, $max, $arr=array()) {
  $rand = rand($min, $max);
  while (count($arr) < $amount-1) {
    if (!in_array($rand, $arr) {
      $arr[] = $rand;
    } else {
      getrandom($amount, $min, $max, $arr);
    }
  }
  return $arr;
}

echo '<marquee>';
$files = glob('images/thumbs/*.jpg');
$random = getrandom(200, 0, count($files));
foreach ($random as $rand) {
  echo '<img src="' . $files[$rand] . '">';
}
echo '</marquee>';

?>

 

Carefull, its not tested.

Link to comment
Share on other sites

Carefull, its not tested.

 

Now it is.

 

<?php

function getrandom($amount, $min, $max, &$arr=array()) {
  $rand = rand($min, $max);
  while (count($arr) < $amount-1) {
    if (!in_array($rand, $arr)) {
      $arr[] = $rand;
    } else {
      getrandom($amount, $min, $max, $arr);
    }
  }
  return $arr;
}

echo '</marquee>';
$files = glob('images/thumbs/*.jpg');
$random = getrandom(200, 0, count($files)-1);
foreach ($random as $rand) {
  echo '<img src="' . $files[$rand] . '">';
}
echo '</marquee>';

?>

Link to comment
Share on other sites

This is less code:

 

$img_array = glob("images/thumbs/*.jpg");
//Pick a random image from the array
for($i=0;$i<200;i++){
     $img = array_rand($img_array);
     //Display the image on the page
     $display .='<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />'; 
}
echo '<marquee>'.$display.'</marquee>';

 

Your code could easily repeat an image, mine won't.

Link to comment
Share on other sites

This is less code:

 

$img_array = glob("images/thumbs/*.jpg");
//Pick a random image from the array
for($i=0;$i<200;i++){
     $img = array_rand($img_array);
     //Display the image on the page
     $display .='<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />'; 
}
echo '<marquee>'.$display.'</marquee>';

 

Your code could easily repeat an image, mine won't.

 

 

Good point!

 

I could make it so it won't, but this topic is solved!

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.