Jump to content

Display a default image


icantcode

Recommended Posts

I am trying to display generic image for items that don't have one, but I can't get it to display..

 

 

?><center><? if(file_exists("thumbs/".$myrow[$field1_name]."")){

echo "<img src='thumbs/".$myrow[$field1_name]."' /> ";

} else {

echo "<img src='thumbs/nophoto.jpg".$field1_name."' />";

} ?></center><br /><?php

?><center><b><?php echo nl2br(stripslashes($myrow[$field2_name])); ?></b></center>

Link to comment
Share on other sites

?><center><? if(file_exists("thumbs/".$myrow[$field1_name]."")){

echo "<img src='thumbs/".$myrow[$field1_name]."' /> ";

} else {

echo "<img src='thumbs/nophoto.jpg' alt='".$field1_name."' />";

} ?></center><br /><?php

?><center><b><?php echo nl2br(stripslashes($myrow[$field2_name])); ?></b></center>

 

Try adding the red part above.

 

Or just remove the second instance of $field1_name altogether, at the moment your script is producing an img src of thumbs/nophoto.jpgblahblahblah (where blahblahblah is whatever $field1_name's value is) it's not a valid file extension.

Link to comment
Share on other sites

What do variables $myrow and $field1_name contain?

 

<?php
    if( file_exists( "thumbs/" . $myrow[$field1_name] ) )
        {
                echo( "<img src=\"./thumbs/" . $myrow[$field1_name] . "\" />" );
        }
    else
        {
                echo( "<img src=\"./thumbs/nophoto.jpg\" />" );
        }
?>

 

This should work if the file doesn't exist and the "./thumbs/nophoto.jpg" exists, although this code is basically the same as what mrMarcus suggested (except I use ./ before the location).

Link to comment
Share on other sites

Still doesn't work. Here is the code that references the fieldname. Could it be anything to do with this?

 

$field1_name = "filename"; // change to whatever field name from the $table_name

$field2_name = "author"; // change to whatever field name from the $table_name

$field3_name = "details"; // change to whatever field name from the $table_name

$field4_name = "location"; // change to whatever field name from the $table_name

$field5_name = "keywords"; // change to whatever field name from the $table_name

 

 

################### E N D C O N F I G U R A T I O N ######################

 

 

####################################################################################

#################### STOP HERE - NO NEED TO CHANGE FROM THIS POIN #################

####################################################################################

$sql = "SELECT * FROM $table_name";

$result = mysql_query($sql ,$db);

$total_records = mysql_num_rows($result);

$num_rows = ceil($total_records / $number_of_colums);

 

if ($result)

{

if ($myrow = mysql_fetch_array($result))

{

do

{

 

?><table width="100%" border="0" cellspacing="15" cellpadding="5">

<tr>

<?php

do

{

if ($newrowcount == $number_of_colums)

{

$newrowcount = 0;

?><tr>

 

<?php

}

?><td>

<?php

 

 

################### DISPLAY cell info ##########

?><center><?php

    if( file_exists( "directory/thumbs/" . $myrow[$field1_name] ) )

        {

                echo( "<img src=\"./directory/thumbs/" . $myrow[$field1_name] . "\" />" );

        }

    else

        {

                echo( "<img src=\"./directory/thumbs/nophoto.jpg\" />" );

        }

?></center><br /><?php

?><center><b><?php echo nl2br(stripslashes($myrow[$field2_name])); ?></b></center><?php

?><center><?php echo nl2br(stripslashes($myrow[$field3_name])); ?></center><?php

?><center><?php echo $myrow[$field4_name]; ?></center><?php

?><center><b>Email:</b> <?php echo $myrow[$field5_name]; ?></center><br /><?php 

################### DISPLAY cell info ##########

Link to comment
Share on other sites

you have nested do{} statements with no while() loop in sight.

 

if all you are doing is drawing records from the database, there are much simpler ways of doing so than what you have done here.

 

and, assuming the paths are correct and the nophoto.jpg exists, the corrected code should work .. i noticed you added 'directory/' to the file path, why?

 

do a view-source in your browser, scroll down to where the image set is and look at the '/path/to/file/nophoto.jpg' and match it exactly as seen in the screen source to that of your existing directory.

Link to comment
Share on other sites

when you viewed the source, what is the file path and location of the image(s) with the '?' in it?

 

edit: so your outputted code looks something like this:

 

hasphoto.jpg

hasphoto.jpg

hasphoto.jpg

nophoto.jpg

hasphoto.jpg

nophoto.jpg

 

correct?

 

or is the code stopping when it reaches a nophoto.jpg

Link to comment
Share on other sites

 

This is the source from the webpage that isn't displaying the nophoto.jpg. It doesn't even acknowledge it.

 

<center><img src="./directory/thumbs/" /></center>

 

 

This is the source code of a picture that is acknowledged, so I know the path is correct.

<center><img src="./directory/thumbs/bissette.jpg" /></center>

Link to comment
Share on other sites

here is what's happening...

 

$myrow[$field1_name]

 

is not always being populated (there will be instances when there is no image), but file_exists() continues on through, ie:

 

if( file_exists( "directory/thumbs/" . $myrow[$field1_name] ) ) //right here, $myrow[$field1_name] is empty, yet file_exists still runs true executing the next line with a blank filename;
{
echo( "<img src=\"./directory/thumbs/" . $myrow[$field1_name] . "\" />" );
}
else
{
echo( "<img src=\"./directory/thumbs/nophoto.jpg\" />" );
}

 

the fix?  add something like this:

 

if (!empty ($myrow[$field1_name]) && (file_exists ('directory/thumbs/' . $myrow[$field1_name])))
{ echo '<img src="./directory/thumbs/' . $myrow[$field1_name] . '" />'; }
else
{ echo '<img src="./directory/thumbs/nophoto.jpg" />'; }

 

try that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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