简体   繁体   中英

Img won't display using PHP

The gist of this code is to display a picture of an animal n number of times using post variables.

The problem is that only the alt is being displayed for the images.

Here is the entire code: Given the URL: /index.php?animal=boxer&count=3

<?php
    $animal = $_GET['animal'];
    $count = $_GET['count'];

    $valid_animals = array("crow", "boxer", "rat");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Animoz</title>
</head>
<body>
<p>
    <?php if (in_array($animal, $valid_animals)): ?>
        <?php for ($j = 0; $j < $count; $j++): ?>
            <img src="images/<?=$animal?>.jpg" alt="<?=$animal?>">
        <?php endfor; ?>
    <?php else: ?>
        <p> I can't show you any <?=$animal?>s!!!!</p>
    <?php endif; ?>
</p>
</body>
</html>

I've also tried:

<?php
    if (in_array($animal, $valid_animals))
    {
        for ($j = 0; $j < $count; $j++)
        {
            echo '<img src="'. $animal . '.jpg" alt="' . $animal . '">';
        }
    }
?>

The HTML code that comes of it looks A-OK:

<body>
     <p>
        <img src="images/boxer.jpg" alt="boxer">
        <img src="images/boxer.jpg" alt="boxer">
        <img src="images/boxer.jpg" alt="boxer">    
     </p>
</body>

The file path being used is correct.

Any idea what the heck I'm doing wrong here??

the images were being pulled from /Part2.php/images instead of /Part2/images (Part2 also contained Part2.php). Apparently it wasn't enough to use "src=/images"-- I ended up having to use "src=../images".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM