Jump to content

Recommended Posts

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

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.

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.