I have a business directory website that users can create listings in. I already have the listing functionality mostly working. An example of the code i use is this:

<div id="companyName"> <?php echo $row_getListing['company_name']; ?></div>

What I want is a code like this to get an image from the logo row in my table unless there is no image in that row then it instead gets a placeholder image from my images folder. Can anyone help?

Member Avatar for LastMitch

echo $row logo image or placeholder image

Maybe something like this:

<div id="companyName"><img src="<?php $row_getListing['company_name']; ?>" alt="Image from DB" /></div>

Explain a little more, if that's not what you are looking for you only provided a line of code then provide a little more code to see what you are doing?

What I want is the listing to show the companies logo if they have one and a placeholder image if they dont. The code i got for the image is the following:

<div id="logo"><img src="images/<?php echo $row_getListing['image']; ?>" width="114" height="57" /></div>

So if they have a company logo and upload it then it will display in their listing. If they don't upload an image instead a placeholder image will display.

The code i use for the listings in as follows:

<?php do { ?>
    <div id="listing">
      <div id="pageTitle"> <a href="<?php echo $row_getListing['city']; ?>/<?php echo $row_getListing['catagory']; ?>/<?php echo $row_getListing['page_name']; ?>"><?php echo $row_getListing['page_title']; ?></a></div>
      <div id="logo"><img src="images/<?php echo $row_getListing['image']; ?>" width="114" height="57" /></div>
      <div id="phoneNumber"> <?php echo $row_getListing['phone_number']; ?></div>
      <div id="companyName"> <?php echo $row_getListing['company_name']; ?></div>
      <div id="address"> <?php echo $row_getListing['address']; ?>, <?php echo $row_getListing['city']; ?>, <?php echo $row_getListing['province_state']; ?></div>
      <div id="pageDescription"> <?php echo $row_getListing['page_description']; ?></div>
      <div id="website"> <a href="http://<?php echo $row_getListing['websiteurl']; ?>"><?php echo $row_getListing['websiteurl']; ?></a></div>
    </div>
    <?php } while ($row_getListing = mysql_fetch_assoc($getListing)); ?>

My way would be to put the placeholder as default, and change it only by uploading a
logo, instead of the other way round, which is what you seem to be doing.

Check for existence of image in the DB table and if exists asign it to $logo variable otherwise assign a placeholder to $logo variable. Then display whatever is in the $logo.

<?php 
if(isset($row_getListing['image'])) {
    $logo = $row_getListing['image']; 
} else {
    $logo = 'placeholder.jpg'; 
}
?>

<div id="logo"><img src="images/<?php echo $logo; ?>" width="114" height="57" /></div>

All you have to do is;

<?php
    // Fetch the image from the database
    $query = "SELECT logo FROM logo_imageso WHERE id='$image_id'";

    //Execute the mysql_query, assuming you are using mysql
    $result=mysql_query($query) or die(mysql_error());

    //Check to see if an image has been returned
    if(mysql_num_rows($result) > 0 )
    {
    //If it is returned, then display the logo
    $row=mysql_fetch_array($result);
    $badge = $row['logo'];
    echo"<img src=$row[logo] width=100 height=80>";
      }
      else //If no image has been returned
      {
      //Display the place holder
    echo "<img src='image/place_holder.jpg'width=90 height=60>";
      }
  ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.