![](/img/trans.png)
[英]CSS styling in PHP lost after “onclick” script — need help keeping HTML table with original CSS styling
[英]Need help styling PHP in HTML
這是我的PHP編碼,我使用select where語句從數據庫中獲取數據並將其輸出。 我正在嘗試更改打印語句上的文本屬性,例如字體,顏色和大小,任何人都可以通過該方法來實現嗎?
<?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);
?>
您可以使用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>
您可以這樣輸出markdown:
print "<div class = 'sampleclass'> - Name: " . $row["fname"]. " " .
然后通過將以下內容包含在附加的css
文件中來設置樣式
.sampleclass {
font-size: 15px;
}
這僅涉及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>
您始終可以使用CSS文件並定義值。 例如:
body {
font-family: arial;
font-size: 15px;
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
php txt
</body>
</html>
另外,您可以使用<span>
標記並設置其樣式。 例如,使用php的'print'打印此范圍:
<span style="font-family: arial; font-size: 15px;">text</span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.