Simple PHP If Statements - Help

Soldato
Joined
31 Oct 2005
Posts
8,845
Location
Leeds
Hello there again, I've left you alone for a while and only have a quick question this time

I currently have a piece of code that finds the number of a user, and kicks out their picture from file, only problem is, if there isn't a picture, it displays an error message.

Put simply, I'm after something like, "if $file <0 then display somefile.jpg"

Should be a 5 second job for some on you, would be much apperciated

Here's the PHP

PHP:
<?php  
foreach (glob("$d_no.*") as $file) {
echo '<td>' . "<a href='$file' target=_blank><img  src='$file'  alt='$file'  width='180' height='300'></a>" . '</td>'; 
}   
echo '</table>'; 

?>
 
But if no files match the pattern then the for loop wont even execute once will it? You might need to store it in an array first, something like this:

PHP:
<?php  
$files = glob("$d_no.*");
if(sizeof($files) > 0) {
 // There are some files
 foreach ($files as $file) {
echo '<td>' . "<a href='$file' target=_blank><img  src='$file'  alt='$file'  width='180' height='300'></a>" . '</td>'; 
}   
} else {
// Do something else
}

echo '</table>'; 

?>

I haven't used glob before so don't know if this will work!
 
But if no files match the pattern then the for loop wont even execute once will it? You might need to store it in an array first, something like this:


I haven't used glob before so don't know if this will work!

Cheers for the PHP attempt, I tried
PHP:
<?php  
$files = glob("$d_no.*");
if(sizeof($files) > 0) {
 // There are some files
 foreach ($files as $file) {
echo '<td>' . "<a href='$file' target=_blank><img  src='$file'  alt='$file'  width='180' height='300'></a>" . '</td>'; 
}   
} else {
echo '<td>' . "<a href='/test.jpg' target=_blank><img  src='$file'  alt='$file'  width='180' height='300'></a>" . '</td>'; 
echo'</table>';
// Do something else
}

echo '</table>'; 

?>

Which works fine on users with pics, but alas the same error without

Warning: Invalid argument supplied for foreach()
 
Back
Top Bottom