Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中如何在foreach上放置不同的图像_Php_Html_Image_Foreach - Fatal编程技术网

在php中如何在foreach上放置不同的图像

在php中如何在foreach上放置不同的图像,php,html,image,foreach,Php,Html,Image,Foreach,我正在进行PHP培训,我还在学习,我正在尝试制作一个糟糕的视频游戏广告,我把我的PHP文件放在一个文件夹中,还有一个名为img的文件夹放在同一个文件夹中。嗯,我想知道的是,我该如何为不同的游戏展示不同的图像?我试着至少展示一下看门狗的img,但它没有展示出来。。。我需要为正确的游戏展示正确的图像,有人能帮我吗?这是密码,我知道它很差…: <!Doctype HTML> <html lang="pt-br"> <head> <title> R

我正在进行PHP培训,我还在学习,我正在尝试制作一个糟糕的视频游戏广告,我把我的PHP文件放在一个文件夹中,还有一个名为img的文件夹放在同一个文件夹中。嗯,我想知道的是,我该如何为不同的游戏展示不同的图像?我试着至少展示一下看门狗的img,但它没有展示出来。。。我需要为正确的游戏展示正确的图像,有人能帮我吗?这是密码,我知道它很差…:

<!Doctype HTML>
<html lang="pt-br">
<head>
    <title> Repetição em PHP </title>
</head>
<body>
    <?php 
    $arrayGames = array("Watch_Dogs " => " R$199,90",
                         "Dead Space " => " R$149,90",
                          "Wolfenstein " => " R$189,90");
        foreach($arrayGames as $titulo => $preco){
            echo"<p>Jogo: " . $titulo . "<img src='img/watch_dogs.jpg' /> </p>" . $preco;
        }
    ?>
</body>
</html>
显示的图像必须与php文件位置相关

例:


您可以将数据以多维形式存储在阵列上:

$games = array("Watch_Dogs " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150"),
               "Dead Space " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150"),
               "Wolfenstien " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150")
               );

$html = '';
foreach($games as $game_title =>$price_img)
{
    $html .= "<p>Jogo: {$game_title} <img src='{$price_img['img']}' /> {$price_img['price']} </p>";
}

echo $html;

您需要从数组键派生文件名的前导部分。在追加.jpg之前,需要删除键名上的尾随空格

在我建议的解决方案中,我选择修改键以删除尾随空格;如果尾随空格很重要,则需要在使用键之前对其进行rtrim

    <!Doctype HTML>
    <html lang="pt-br">
    <head>
    <title> Repetição em PHP </title>
    </head>
    <body>
    <?php
         $arrayGames = array("Watch_Dogs" => " R$199,90",
                             "Dead Space" => " R$149,90",
                             "Wolfenstein" => " R$189,90");
         foreach($arrayGames as $titulo => $preco ) {
             echo"<p>Jogo: " . $titulo . "<img src='img/" . $titulo . ".jpg'/> </p>" . $preco;
         }
    ?>
    </body>
    </html>

尝试添加一个/before'img/watch_dogs.jpg'Hey bro,所以,问题是它们不在before文件夹中,它们在img文件夹中,即使使用de/it也不起作用。。。我试图用代码创建一个html文件,img显示得非常完美。在浏览器中按CTRL+U可查看html源代码,查看是否有任何内容不正确。你应该可以点击“src=”中的链接,看看它会带你去哪里。你的图片的名称是什么?@nismoracerx这样做了,但似乎一切正常,但我点击了src链接,它不会带我去任何地方…Thx的帮助人,但我不能说它是否真的有效,因为img还没有出现。。。但我正在工作,一旦我能解决这个问题,我就会在这里发布!你是否使用了我提供的链接,你的链接必须是你图像的路径,我提供的链接只是为了演示。虽然我使用了正确的路径,但它不起作用:/嘿,伙计,thx,我只是不能说它是否真的起作用,因为IMG还没有显示,但我正在解决这个问题,我会尽快将它发布到这里!突然imgs出现了哈哈,thx人!谢谢所有帮助我的人。我想你给出了最好的答案!虽然IMG仍然没有显示,但我在页面中显示了ctrl+u,并且图像在正确的游戏中被正确指定。你能解释一下吗?
$games = array("Watch_Dogs " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150"),
               "Dead Space " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150"),
               "Wolfenstien " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150")
               );

$html = '';
foreach($games as $game_title =>$price_img)
{
    $html .= "<p>Jogo: {$game_title} <img src='{$price_img['img']}' /> {$price_img['price']} </p>";
}

echo $html;
    <!Doctype HTML>
    <html lang="pt-br">
    <head>
    <title> Repetição em PHP </title>
    </head>
    <body>
    <?php
         $arrayGames = array("Watch_Dogs" => " R$199,90",
                             "Dead Space" => " R$149,90",
                             "Wolfenstein" => " R$189,90");
         foreach($arrayGames as $titulo => $preco ) {
             echo"<p>Jogo: " . $titulo . "<img src='img/" . $titulo . ".jpg'/> </p>" . $preco;
         }
    ?>
    </body>
    </html>