如何使用跨度设置 PHP 回声输出的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13210552/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:59:40  来源:igfitidea点击:

How to style PHP echo output with span

phpsqlcsshtml

提问by Blackline

I'm trying to style the output of each echo.

我正在尝试为每个回声的输出设置样式。

Ideally I'd like to use <span class=""> </span>for each echo, but I'm not too sure how to achieve this.

理想情况下,我想<span class=""> </span>用于每个回声,但我不太确定如何实现这一点。

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
    echo $row['Date'];
    echo $row['Title'];
    echo $row['Message'];
    echo "<img src='".$row['Image']."'/>";
}

mysql_close($con);

回答by JvdBerg

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
    {
    echo "<span class=\"myclass\">$row['Date']</span>";
    echo "<span class=\"myclass\">$row['Title']</span>";
    echo "<span class=\"myclass\">$row['Message']</span>";
    echo "<img src='".$row['Image']."'/>";
    }

mysql_close($con);

or, much nicer, in a table:

或者,更好的是,在表格中:

$result = mysql_query("SELECT * FROM Blog");
echo "<table>"
while($row = mysql_fetch_array($result))  {
    echo "<tr>"
    echo "<td>$row['Date']</td>";
    echo "<td>$row['Title']</td>";
    echo "<td>$row['Message']</td>";
    echo "<td><img src='".$row['Image']."'/></td>";
    echo "</tr>"
}
echo "</table>"

mysql_close($con);

You then can style each row and column with a class.

然后,您可以使用一个类来设置每一行和每一列的样式。

回答by John V.

Try this:

尝试这个:

$prepend = "<span class=''>";
$append  = "</span>";

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
    {
    echo $prepend.$row['Date'].$append;
    echo $prepend.$row['Title'].$append;
    echo $prepend. $row['Message'].$append;
    echo $prepend."<img src='".$row['Image']."'/>".$append;
    }

mysql_close($con);

回答by Ikke

I'd create a function that does this:

我将创建一个执行此操作的函数:

function decorated_echo($text) {
    echo '<span class="myclass">' . $text . '</span>';
}

This way, you don't have to repeat this everytime you want this behaviour.

这样,您就不必每次想要这种行为时都重复此操作。

回答by luigif

You are guessing right, just add required html in the echo:

您猜对了,只需在回显中添加所需的 html:

echo '<span class="yourclass"><img src="'.$row['Image'].'" /></span>';

or you can just put inline style if no css file is loaded:

或者,如果没有加载 css 文件,您可以只放置内联样式:

echo '<span style="color:red;"><img src="'.$row['Image'].'" /></span>';