Connecting Tech Pros Worldwide Forums | Help | Site Map

random pic - does not ALWAYS show

Tobi Hammert
Guest
 
Posts: n/a
#1: Jan 25 '07
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?


Kimmo Laine
Guest
 
Posts: n/a
#2: Jan 25 '07

re: random pic - does not ALWAYS show



"Tobi Hammert" <speichern@gmail.comwrote in message
news:1169688138.621201.85300@a75g2000cwd.googlegro ups.com...
Quote:
>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
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)


Geoff Berrow
Guest
 
Posts: n/a
#3: Jan 25 '07

re: random pic - does not ALWAYS show


Message-ID: <1169688138.621201.85300@a75g2000cwd.googlegroups. comfrom
Tobi Hammert contained the following:
Quote:
><?
$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/
Geoff Berrow
Guest
 
Posts: n/a
#4: Jan 25 '07

re: random pic - does not ALWAYS show


Message-ID: <djpgr21fg61gl5sl97c8ag385sibd7gfge@4ax.comfrom Geoff
Berrow contained the following:
Quote:
>
$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/
Jerry Stuckle
Guest
 
Posts: n/a
#5: Jan 25 '07

re: random pic - does not ALWAYS show


Geoff Berrow wrote:
Quote:
Message-ID: <djpgr21fg61gl5sl97c8ag385sibd7gfge@4ax.comfrom Geoff
Berrow contained the following:
>
Quote:
> $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.
jstucklex@attglobal.net
==================
Tobi Hammert
Guest
 
Posts: n/a
#6: Jan 25 '07

re: random pic - does not ALWAYS show


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

thanks anyway!

Closed Thread