By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

random pic - does not ALWAYS show

P: n/a
i just want to show up a random pic.

<?
$files = glob("./gfx/zufall/*");
shuffle ($files);
if (count($files) < 1)
$count = count($files);
else
$count = 1;
for ($i = 0; $i < $count; $i ++)
{
$file = $files[$i];
echo "<a href='index.php'><img src='$file' height='341'
width='634' border='0'></a>";
}
?>

it works fine, just ocasionally, it shows NO pic.
whats wrong? that is, how can i solve it?

Jan 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Tobi Hammert" <sp*******@gmail.comwrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
>i just want to show up a random pic.

<?
$files = glob("./gfx/zufall/*");
shuffle ($files);
if (count($files) < 1)
$count = count($files);
else
$count = 1;
for ($i = 0; $i < $count; $i ++)
{
$file = $files[$i];
echo "<a href='index.php'><img src='$file' height='341'
width='634' border='0'></a>";
}
?>

it works fine, just ocasionally, it shows NO pic.
whats wrong? that is, how can i solve it?
On those rare occasions when it doesn't work, have you looked at the source
code to see what the output was? print_r($files); to see if . and .. are
included in the file list. If they are, those might be casusing your
problem. If not, see the output when it doesn't work and try to figure what
went wrong. Another guess would be non-ascii characters in filenames don't
translate to url, like characters with umlauts or stuff like that.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 25 '07 #2

P: n/a
Message-ID: <11*********************@a75g2000cwd.googlegroups. comfrom
Tobi Hammert contained the following:
><?
$files = glob("./gfx/zufall/*");
shuffle ($files);
if (count($files) < 1)
$count = count($files);
else
$count = 1;
for ($i = 0; $i < $count; $i ++)
{
$file = $files[$i];
echo "<a href='index.php'><img src='$file' height='341'
width='634' border='0'></a>";
}
?>

it works fine, just ocasionally, it shows NO pic.
whats wrong? that is, how can i solve it?
Occasionally you get . or .. in which case you won't see a picture. If
your files are all .jpgs, try $files = glob("./gfx/zufall/*.jpg");
Alternatively try (untested):

$files = glob("./gfx/zufall/*");
shuffle ($files);
foreach( $files as $key=>$value){
if ($value=="." || $value==".."){
unset($files[$key]);
}
}

echo "<a href='index.php'><img src='".$file[0]."' height='341'
width='634' border='0'></a>";
}
?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jan 25 '07 #3

P: n/a
Message-ID: <dj********************************@4ax.comfrom Geoff
Berrow contained the following:
>
$files = glob("./gfx/zufall/*");
shuffle ($files);
foreach( $files as $key=>$value){
if ($value=="." || $value==".."){
unset($files[$key]);
}
}

echo "<a href='index.php'><img src='".$file[0]."' height='341'
width='634' border='0'></a>";
}
?>
Ummm, that should be:-

$files = glob("./gfx/zufall/*");
shuffle ($files);
foreach( $files as $key=>$value){
if ($value=="." || $value==".."){
unset($files[$key]);
}
}
echo "<a href='index.php'><img src='".$files[0]."' border='0'></a>";

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jan 25 '07 #4

P: n/a
Geoff Berrow wrote:
Message-ID: <dj********************************@4ax.comfrom Geoff
Berrow contained the following:
> $files = glob("./gfx/zufall/*");
shuffle ($files);
foreach( $files as $key=>$value){
if ($value=="." || $value==".."){
unset($files[$key]);
}
}

echo "<a href='index.php'><img src='".$file[0]."' height='341'
width='634' border='0'></a>";
}
?>

Ummm, that should be:-

$files = glob("./gfx/zufall/*");
shuffle ($files);
foreach( $files as $key=>$value){
if ($value=="." || $value==".."){
unset($files[$key]);
}
}
echo "<a href='index.php'><img src='".$files[0]."' border='0'></a>";
Close, Goeff. But I think you want to remove the pseudo-files before
you shuffle. What happens if after the shuffle one of them comes up as
element 0?

Just putting the shuffle() after the foreach loop should work better.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 25 '07 #5

P: n/a
found the bug - its almost embarassing ;)
was a hidden file, created prob. by dreamweaver. after i removed it,
everything worked smoothly.

thanks anyway!

Jan 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.