2

This is the first time I have ever posted a question. I am a newbie, and I hope I get all the formatting correct in this message. I love stack overflow. It has helped me get through many challenging situations. But this one has me stymied.

I would like to replace a MySQL database result with an image. I have a plant database that I'm querying to show lists of plants. My results table consists of the plant type, the botanical name, the common name, and whether it is a native plant.

The result for the native plant comes in from the database as the letter 'y' (for yes). I would like to replace that 'y' with an image of an oak leaf. The code I'm currently using displays the oak leaf on all plants, not just the ones that are native plants.

What to do? I feel like I need to insert an if statement in the while loop, but it never works when I try it. I've tried tons of different solutions for a few days now, and would like to ask for help with this.

Here's the latest code I've got that displays the image for every plant, not just the natives:

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

    $native_image = $row['native'];
    $image = $native_image;
    $image = "images/oak_icon.png";


   echo '<tr><td>' . 
        $row['plant_type'] .  
    '</td><td>' . 
        $row['botanical_name'] .   
            $row['common_name'] .           
    '</td><td>' . 
        $image .  
    '</td></table>'; 

}
1
  • 2
    You don't actually check whether it's y May 18, 2013 at 23:57

2 Answers 2

1

I hope I understood your question.

Some of rows in MySQL have $row['native'], that are equals 'y'. If it is so, then you are on the right way - you have to put an if statement in your loop. Try it

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    if ($row['native'] == "y") {
        //we show our image
        $image = "images/oak_icon.png";
    } else {
        //we don't have to show our image
        $image = "";
    } 

    echo '<tr><td>' . 
        $row['plant_type'] .  
    '</td><td>' . 
        $row['botanical_name'] .   
        $row['common_name'] .           
    '</td><td>' . 
        $image .  
    '</td></table>'; 

 }

And where is your IMG tag? =) For the future: In my Projects, if I want to save the information about image, that belongs to the row in MySQL, I just save the name of the picture, where it has to be, and if not, i save the name of empty 1x1 PNG image (usual "blank.png"). If you put blank.png in an IMG tag, that has fixed height and width, so IMG takes just the place on your page.

I hope I helped you, and this is my first Answer! =)

P.s. sorry for my english...

1
  • Yes! Success! Both of these solutions worked beautifully. It turns out I had two flaws: 1. I kept using one "=" sign instead of two when I checked for "y" 2. I didn't provide for the "else" of no "y" present. Thank you, guys. I really appreciate the swift response. Yay for first questions and first answers! Lori
    – Lori
    May 19, 2013 at 0:37
1

Explosion Pills is correct. You will need to check if the $row['native'] is 'y' then set the image html to whatever is required. Hope this helps.

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

    $image='';

    if($row['native']=='y'){
        $image = '<img src="images/oak_icon.png" />';
    }

   echo '<tr><td>' . 
        $row['plant_type'] .  
    '</td><td>' . 
        $row['botanical_name'] .   
            $row['common_name'] .           
    '</td><td>' . 
        $image .  
    '</td></table>'; 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.