【问题标题】:how to change background colour in echo如何在回声中更改背景颜色
【发布时间】:2026-02-11 20:20:06
【问题描述】:

我正在尝试使用内联样式在 echo 中应用背景颜色,但它没有应用背景颜色,而是仅更改文本颜色。我想更改代码特定部分的背景颜色

echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>"

程序代码是

    class db_access
    {
    private $_uname;
    private $_pass;
    private $_db;
    private $_server;

    //_construct connects databaseand fetchest he result
        public function __construct($server,$user_name,$password,$d_b)
        {
        $this->_server=$server;
        $this->_pass=$password;
        $this->_uname=$user_name;
        $this->_db=$d_b;
        $con=mysql_connect($this->_server,$this->_uname,$this->_pass);

        $db_found=mysql_select_db($this->_db,$con);
        if($db_found)
            {

            $sql="SELECT * FROM login";
            $result = mysql_query($sql);
                if ($result)
                {
                    while ( $db_field = mysql_fetch_assoc($result) ) 
                    {
                    static $rec_num=1;
//inline css
                    echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>";
                    print $db_field['ID'] . "<BR>";
                    print $db_field['u_name'] . "<BR>";
                    print $db_field['pass'] . "<BR>";
                    print $db_field['email'] . "<BR><br><br>";
                    $rec_num++;
                    }
                    //returns the connection name that is used as a resource id in __destruct function
                    return $this->_con=$con;        
            }
                else {die(mysql_error());}

            }

        else 
        {return die(mysql_error());}
        } 


        // destruct function closes database    
        public function __destruct()
        {

        $close=mysql_close($this->_con);
        if($close)
        {print "connection closed";}
        else {die(mysql_error());}
        }   

    }
    $db=new db_access("127.0.0.1","root","","fabeeno");

    //var_dump($db);

【问题讨论】:

  • echo "

    "."记录号:".$rec_num. "

    "."
    ";

标签: php colors background echo


【解决方案1】:

或者你可以只调整带有样式的打印行

print "<p style='color: red;border: 1px solid black;height:20px;box-shadow: 1px 1px 7px;'>"."message!: ".$db_field['TEXTAREA1'] . "<br>";

在这之后,我在文本周围得到了一个花哨的盒子 =) 我在很多方面都喜欢盒子

所以没有回声只放置print {}函数

祝你好运

ps ['textarea1']

【讨论】:

  • 确实调用了整个集合的简单样式,只是为了绕过我需要为表格调用回声样式,没什么大不了的,但最容易使用,同时我想出了如何只要我使用 ".""." 在 de print 中添加额外的图标
  • 我的例子是 print ""."".""."sometext:".$db_field['TextArea1'] ." ";
【解决方案2】:

试试

echo "<div style='background-color:red;'><p style='color:orange'>"."record number:     ".$rec_num. "</p></div>"."<br>";

【讨论】:

    【解决方案3】:

    试试看

    echo "<p style='color:orange;background-color:red;'>record number: ".$rec_num. "</p><br>";
    

    您必须在 background-color 样式之后结束 ' 单引号。

    【讨论】: