frank_solo Posted January 9, 2012 Share Posted January 9, 2012 How do I do I prevent a broken image icon if there is no image? Here is my code: <?php if ($_POST){ $county = $_POST['county']; } $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $imageLocation = $row['imageurl1']; $result = mysql_query("SELECT * FROM places WHERE `county` = '".mysql_real_escape_string($county)."' order by `date_created` DESC"); if ( mysql_num_rows($result) > 0 ) { echo "<strong>Click Headers to Sort</strong>"; echo "<table border='0' align='center' bgcolor='#999969' cellpadding='3' bordercolor='#000000' table class='sortable' table id='results'> <tr> <th> Title </th> <th> Borough </th> <th> Town </th> <th> Phone </th> <th> Rooms </th> <th> Bath </th> <th> Fees </th> <th> Rent </th> <th> Image </th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr> <td bgcolor='#FFFFFF' style='color: #000' align='center'> <a href='classified/places/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['county'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['town'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['feeornofee'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'><img src=user/". $row['imageurl1'] ." width='50'></td> </tr>"; } echo "</table>"; print_r($apts); } else { echo "<p> </p><p> </p> No Results <br /><p> </p><FORM><INPUT TYPE='button' VALUE='Go Back' onClick='history.go(-1);return true;'></FORM> and Refine Your Search <p> </p><p> </p>"; } ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/254630-how-to-not-display-picture-icon-if-no-picture-in-mysql-database/ Share on other sites More sharing options...
blacknight Posted January 9, 2012 Share Posted January 9, 2012 replace <img src=user/". $row['imageurl1'] ." width='50'> with ".(isset($row['imageurl1'] ) ? "<img src=user/". $row['imageurl1'] ." width='50'>" : '' )." Quote Link to comment https://forums.phpfreaks.com/topic/254630-how-to-not-display-picture-icon-if-no-picture-in-mysql-database/#findComment-1305674 Share on other sites More sharing options...
frank_solo Posted January 9, 2012 Author Share Posted January 9, 2012 Thanks for the quick reply but I still get that broken image. Quote Link to comment https://forums.phpfreaks.com/topic/254630-how-to-not-display-picture-icon-if-no-picture-in-mysql-database/#findComment-1305675 Share on other sites More sharing options...
Psycho Posted January 9, 2012 Share Posted January 9, 2012 isset() will not work, because the field will be set for every record returned from the database. The value may be null or an empty string - but it will be set! The question is, do you want to show NO image or do you want to show a default image? Either way, the logic will be the same, but the implementation slightly different. To display NO image do this while($row = mysql_fetch_array($result)) { $image = (!empty($row['imageurl1'])) ? "<img src=\"user/{$row['imageurl1']}\" width='50'>" : ''; echo "<tr> <td bgcolor='#FFFFFF' style='color: #000' align='center'> <a href='classified/places/index.php?id={$row['id']}'>{$row['title']}</a></td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>$row['county']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['town']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['phone']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['rooms']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['bath']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['feeornofee']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['rent']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$image}</td> </tr>"; } For a default image do this (replace with the appropriate value while($row = mysql_fetch_array($result)) { $imageSrc = (!empty($row['imageurl1'])) ? $row['imageurl1'] : 'default.jpg'; echo "<tr> <td bgcolor='#FFFFFF' style='color: #000' align='center'> <a href='classified/places/index.php?id={$row['id']}'>{$row['title']}</a></td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>$row['county']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['town']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['phone']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['rooms']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['bath']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['feeornofee']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>{$row['rent']}</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'><img src=\"user/{$imageSrc}\" width='50'></td> </tr>"; } Also, if you want a default image, you could also change your query to return the default image when the field is empty and not have to change the output code at all. But, since you are using '*' in your query I'm not going to bother going to the trouble of editing it like it should be. As a general rule you should not use '*' for your SELECT parameters. Instead indicate the fields that you want. SELECTing more fields than you need is a waste of resources. Quote Link to comment https://forums.phpfreaks.com/topic/254630-how-to-not-display-picture-icon-if-no-picture-in-mysql-database/#findComment-1305676 Share on other sites More sharing options...
frank_solo Posted January 9, 2012 Author Share Posted January 9, 2012 Amazing mjdamato! It works like a charm! Quote Link to comment https://forums.phpfreaks.com/topic/254630-how-to-not-display-picture-icon-if-no-picture-in-mysql-database/#findComment-1305678 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.