需要帮助在HTML中设置PHP样式

时间:2018-03-09 09:20:11

标签: php html css database text-styling

这是我的PHP编码,我从中使用select where语句从数据库中获取数据并输出它。我试图在print语句中更改文本属性,如字体,颜色和大小,任何人都可以帮助如何做到这一点?

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        print " - Name: " . $row["fname"]. " " . $row["sname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

4 个答案:

答案 0 :(得分:2)

您可以使用HTML <ul><li>元素设置列表样式。

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Stylists</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    echo "<ul>";

    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<li>Name: " . $row["fname"]. " " . $row["sname"]. "</li>";
    }
    echo "</ul>";
} else {
    echo "0 results";
}

mysqli_close($conn);
?></body>
</html>

css/style.css中放了这样的东西:

li {
   color: red;
   font-weight: bold;
   font-size: 12pt;
}

您还可以将样式内联到头部:

<style type="text/css">
li {
   color: red;
   font-weight: bold;
   font-size: 12pt;
}
</style>

答案 1 :(得分:1)

您可以输出这样的降价:

print "<div class = 'sampleclass'>  - Name: " . $row["fname"]. " " . 

然后通过在附加的css文件中包含以下内容来设置样式

.sampleclass {
    font-size: 15px;
}

答案 2 :(得分:0)

这只是关于HTML和CSS,您可以使用echo直接输出HTML标签中的字符串。该脚本需要封装到HTML的主体和预定义的CSS类中以进行样式化:

<!doctype html>
<html>
  <head>
    ....
    <style type="text/css">
      .redBold {
         color: red;
         font-weight: bold;
      }
    </style>
  </head>
  <body>
    <?php
      ....
      echo '<span class="redBold">This text is red and bold</span>';
    ?>
  </body>
</html>

当然,您也可以使用内联样式:

<span style="color: red; font-weight: bold;">Also this works</span>

答案 3 :(得分:0)

您始终可以使用CSS文件并定义值。 例如:

body {
  font-family: arial;
  font-size: 15px;
}
<html>
  <head>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    php txt
  </body>
</html>

此外,您可以使用<span>标记并设置样式。 例如,使用&#39; print&#39;打印此跨度php:

<span style="font-family: arial; font-size: 15px;">text</span>