Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP Random Images Function

Guest
Oct 27, 2006 Oct 27, 2006
I had made a php random image function and the problem I have at this moment is I don't want to change the "4" number to a "5" because I included another image. Is there a value where the "4" is some php function so I do not need to always update?

<?php
$roll = rand(1,4);
print "<img src = randImages/Bn$roll.jpg>";
?>

Thank you,
AdonaiEchad
TOPICS
Server side applications
300
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 27, 2006 Oct 27, 2006
.oO(AdonaiEchad)

>I had made a php random image function and the problem I have at this moment is
>I don't want to change the "4" number to a "5" because I included another
>image. Is there a value where the "4" is some php function so I do not need to
>always update?
>
> <?php
> $roll = rand(1,4);
> print "<img src = randImages/Bn$roll.jpg>";
> ?>

You could try something like this:

<?php
$path = $_SERVER['DOCUMENT_ROOT'].'/randImages';
$roll = rand(1, count(glob("$path/*.jpg"));
print "<img src='/randImages/Bn$roll.jpg' alt='...'>\n";
?>

glob() searches a given directory for filenames matching the wildcard
pattern (in this case *.jpg). It returns an array with all found
filenames. count() then simply returns the array's size and passes it to
rand().

HTH
Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 31, 2006 Oct 31, 2006
Thank you for all of your help.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 31, 2006 Oct 31, 2006
LATEST
Thank you for all of your help it worked.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines