如何加载一个';s在php的数组变量中


How can I load an image url that's in an array variable with php?

我正在使用php从数据库加载图像url。以下是数据的加载方式:

        <?php
 while ($row = mysqli_fetch_array($result))
 {
 $i++;
 $this->resultSet[$i] = $row[1] . " " . "<br />" . $row[2] . " " . "<br />" . " " . $row[3] . " " . "<br />" . $row[4] . " " . "<br />" . $row[5] . "<br />";
 $this->gameUrl = $row[5];
 }

在foreach循环中,我想在每次迭代后加载url。如果我把数组变量放在image标记之外,它会显示url,这样我就知道它被访问了。我的问题是image标记的格式。我试过这个:

 "<img src='"$gameImageUrl'"/>"

它只显示了最后一张图片,这意味着我的问题是在最后连接支架。当我尝试这个:

 "<img src='"$gameImageUrl[$i]'"/>"

没有负载。最后,我尝试了硬编码,只是在条形码中输入一个数字,如下所示:

"<img src='"$gameImageUrl[2]'"/>"

没有任何回报。下面列出了我的代码。有人能解释一下如何用数组变量格式化图像标签吗?非常感谢你的帮助。

 <?php
 $QueryResult = new GameInfo();
 $searchResult = $QueryResult->getGames();
 $gameImageUrl = $QueryResult->getGameImageUrl();
 if ($searchResult)
 {
 $i = 0;
 echo<ul class='result'>";
 foreach($searchResult as $returnedResult)
    {
    $i++;
    echo "<a href='#'id='game_a_1'>GAME" . " $i" . "<div id='divGame12' class='fluid        '>" . "<img src='"$gameImageUrl[$i]'"/>" . "       </div>" . "<div id='divGame12A' class='fluid '>" . "<p id='P_game12'>" . "$gameImageUrl[$i]" . "$returnedResult" . "</p>" . " </div>" . "</a>";
    }
 echo "</ul>";
 }
 else
 {
  echo "<p>Sorry!  Something went wrong</p>";
}
?>

您需要将变量括起来,如:

"<img src='"${gameImageUrl[$i]}'"/>"

注意${variable}

您的代码存在多个串联和封装问题。试试这个:

$QueryResult = new GameInfo();
$searchResult = $QueryResult->getGames();
$gameImageUrl = $QueryResult->getGameImageUrl();
if ($searchResult)
 {
 $i = 0;
 echo '<ul class="result">';
 foreach($searchResult as $returnedResult)
    {
    $i++;
    echo
      '<a href="#" id="game_a_1">GAME '.$i.'
          <div id="divGame12" class="fluid"><img src="'.$gameImageUrl[$i].'"/></div>
            <div id="divGame12A" class="fluid">
               <p id="P_game12">'.$gameImageUrl[$i].' '.$returnedResult.'</p>
           </div>
       </a>';
    }
 echo "</ul>";
 }
 else
 {
  echo "<p>Sorry!  Something went wrong</p>";
}

哦,天哪,好吧,请你先清理你的代码,帮助我们澄清问题可能在哪里。之所以会发生这种情况,是因为你在回显img url,而不是先从数据库中分配一个变量,将其连接到回显迭代中。示例echo"$rs[照片]。$rs变量应该连接到您的数据库。