icantcode Posted October 21, 2009 Share Posted October 21, 2009 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> Quote Link to comment Share on other sites More sharing options...
cactuscake Posted October 21, 2009 Share Posted October 21, 2009 ?><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. Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 21, 2009 Author Share Posted October 21, 2009 alt tag didn't work. I can't get the syntax right on this: any help? echo "<img src='directory/thumbs/nophoto.jpg' /> "; Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Share Posted October 21, 2009 echo '<img src="thumbs/nophoto.jpg" alt="'.$field1_name.'" />'; Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks, but it still doesn't work. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 21, 2009 Share Posted October 21, 2009 Thanks, but it still doesn't work. what doesn't work? are you sure your paths to the images are correct? are you getting a syntax error? Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 21, 2009 Author Share Posted October 21, 2009 Yes, the source for my images are correct. For every instance that I don't have an image associated with a name, I get a ? in place of the image. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 21, 2009 Share Posted October 21, 2009 it appears you are appending a variable ($field1_name) to the end of the 'nophoto.jpg' ... echo "<img src='thumbs/nophoto.jpg".$field1_name."' />"; try: echo '<img src="thumbs/nophoto.jpg" />'; Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 22, 2009 Author Share Posted October 22, 2009 Still doesn't work. is there another way code it, if the file doesn't exist? no matter what I do, I can't get the image to show up. I know my path is correct because the rows that have images show up. Quote Link to comment Share on other sites More sharing options...
Stephen Posted October 22, 2009 Share Posted October 22, 2009 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). Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 22, 2009 Author Share Posted October 22, 2009 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 ########## Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 22, 2009 Author Share Posted October 22, 2009 yes, I added /directory to the path. I switched the location of the file. The path is still correct. The code still won't pull the image, though. I have replaced the path with other images to see if there is something funky with the image and no dice. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 22, 2009 Author Share Posted October 22, 2009 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> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
icantcode Posted October 22, 2009 Author Share Posted October 22, 2009 IT WORKS!!!!!! Thanks a million! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.