使用带有 <img src="" 的 php 显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27847441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display image using php with <img src=""
提问by Akinn
I have stupid problem with the html/php rules. I'm trying to show an image from an apache
server with this code using a table:
我有 html/php 规则的愚蠢问题。我正在尝试apache
使用表格使用此代码显示来自服务器的图像:
<?php
//code
while($row = mysqli_fetch_array($result)) {
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src=\"<?php echo $url; ?>\"/></td>
</tr>';
}
//code
?>
But obviusly the inner php script is considered as normal text and no run!
但很明显,内部的 php 脚本被认为是普通文本,不能运行!
采纳答案by developerwjk
Don't echo large blocks of HTML in PHP like that. Its bad practice. No, actually, its horrible practice. Instead learn to open and close the PHP tag as needed, like:
不要像那样在 PHP 中回显大块的 HTML。它的坏习惯。不,实际上,这是可怕的做法。而是学习根据需要打开和关闭 PHP 标记,例如:
<?php
//code..code...code...
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td> <?php echo $row['x']; ?> </td>
<td> <?php echo $row['y']; ?> </td>
<td> <?php echo $row['z']; ?> </td>
<td> <?php echo $row['f']; ?> </td>
<td> <?php echo $row['g']; ?> </td>
<td> <?php echo $row['d']; ?> </td>
<td><img src="<?php echo $url; ?>"/></td>
</tr>
<?php
}
//code..code...code
?>
There are several benefits to this, including that its less likely to break syntax highlighting and your code is not defaced with as many \"
all over the place.
这样做有几个好处,包括它不太可能破坏语法高亮显示,并且您的代码不会\"
到处乱写。
回答by Mureinik
You're already inside PHP - you shouldn't open another <?php
scope:
你已经在 PHP 里面了——你不应该打开另一个<?php
作用域:
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="' .$url . '"/></td>
</tr>';
回答by Bruno H. Paes
Try it:
尝试一下:
<?php
while($row = mysqli_fetch_array($result)){
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="'.$url.'"/></td>
</tr>';
}
?>
It just a problem in concatenation, you need just combine those strings, not set another 'echo script'.
这只是连接的问题,您只需要组合这些字符串,而不是设置另一个“回显脚本”。
回答by Rizier123
Just change this:
只需改变这个:
<td><img src=\"<?php echo $url; ?>\"/></td>
to:
到:
<td><img src="' . $url .'"/></td>
回答by sabatmonk
you can simply close the php section and put plain html, then reopen php when needed like this :
您可以简单地关闭 php 部分并放置纯 html,然后在需要时重新打开 php,如下所示:
<?php
$test = array( "a test","also","a","test");
$itteraror = 0;
$url = "#";
?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
</head>
<body>
<table>
<?php
while($itteraror<sizeof($test)) {
?>
<tr>
<td> <?php echo $test[$itteraror] ; ?></td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td><img src="<?php echo $url ; ?>" alt="my image"></td>
</tr>
<?php
$itteraror++;
}
?>
</table>
</body>
</html>
i did make some modification to your code in order for it to be standalone for testing purpose.
我确实对您的代码进行了一些修改,以使其独立用于测试目的。