php replace the result of the database with an image

advertisements

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>'; 

}


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...