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
https://forums.phpfreaks.com/topic/134300-solved-scrolling-images-on-webpage/
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.

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

?>

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

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.