我正在尝试使用外部样式表将 h3 元素和“profile_content”div-id 置于“profile”类的中心。我知道如何只用一个 html 和 css 文件来做到这一点,但是当涉及到 php 时,我感到很困惑!请你也能帮我解决语法问题,这样我就知道以后该怎么做了。
php代码
<?php
session_start();
// Connect to the database with '$mysqli' as the connection variable name
$mysqli = new mysqli ("localhost", "root", "", "folder");
//Check connection
// '$mysqli->connect_errno' = Returns the error code from last connect call
// '->' is used to access an object method or property
if ($mysqli -> connect_errno) {
die('Connect Error: ' . $mysqli -> connect_errno);
}
?>
<html>
<header>
<link rel = "stylesheet" type = "text/css" href = "mystyle_friends.css">
</header>
<div class = "heading">
<h1>FRIENDS PAGE</h1>
</div>
<p>Welcome <?php echo $_SESSION['username'] ?></p>
<div class = "profile_content">
<div id = profile_heading>
<h3>Profiles</h3>
</div>
<?php
//Perform query against database. Requires 2 parameters = (connection, query)
$result = mysqli_query($mysqli, "SELECT * FROM users");
while ($row = mysqli_fetch_array($result)) {
echo "<div id = 'profile_data'>";
echo "<p> First Name: " .$row ["user_f_name"] . "</p>";//$user_first = $row["user_f_name"];
echo "<p> Surname: " .$row ["user_surname"] . "</p>";//$user_sur = $row["user_surname"];
echo "<p> Score: " .$row ["user_score"] . "</p>";//$user_score = $row["user_score"];
echo '<img src="data:user_images/jpeg;base64,' . base64_encode( $row['user_image'] ) . '" />';
echo "</div>";
}
?>
</div>
css 外部样式表
.heading {
text-align: center;
}
.profile {
border-style: solid;
border-width: 5px;
}
h3. profile {
text-align: center;
}
#profile_content. profile {
text-align: center;
}
最佳答案
冬青通心粉!
首先更正您的 HTML:
<?php
session_start();
// Connect to the database with '$mysqli' as the connection variable name
$mysqli = new mysqli ("localhost", "root", "", "folder");
//Check connection
// '$mysqli->connect_errno' = Returns the error code from last connect call
// '->' is used to access an object method or property
if ($mysqli -> connect_errno) {
die('Connect Error: ' . $mysqli -> connect_errno);
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle_friends.css">
</head>
<body>
<div class="heading">
<h1>FRIENDS PAGE</h1>
</div>
<p>Welcome <?php echo $_SESSION['username'] ?></p>
<div class="profile_content">
<div id="profile_heading">
<h3>Profiles</h3>
</div>
<?php
//Perform query against database. Requires 2 parameters = (connection, query)
$result = mysqli_query($mysqli, "SELECT * FROM users");
while ($row = mysqli_fetch_array($result)) {
echo "<div id=\"profile_data\">";
echo "<p>First Name: " . $row["user_f_name"] . "</p>"; //$user_first = $row["user_f_name"];
echo "<p>Surname: " . $row["user_surname"] . "</p>"; //$user_sur = $row["user_surname"];
echo "<p>Score: " . $row["user_score"] . "</p>"; //$user_score = $row["user_score"];
echo "<img src=\"data:user_images/jpeg;base64," . base64_encode( $row['user_image'] ) . "\" />";
echo "</div>";
}
?>
</div>
</body>
</html>
之后你必须修复你的 CSS-Selectors:
// Selectors:
// element {} Elements can appear as many times as you need them per Document
// .class {} Classes can appear as many times as you need them per Document
// #id {} IDs can appear just once per Document
.heading {
text-align: center;
}
#profile_heading h3 {
text-align: center
}
.profile_data {
text-align: center;
}
// There is no Element with the Class "profile"
/*
.profile {
border-style: solid;
border-width: 5px;
}
*/
// There is no Element inside of the h3 with the Class "profile"
/*
h3. profile {
text-align: center;
}
*/
// There is no Element with ID "profile_content"
/*
#profile_content .profile {
text-align: center;
}
*/
但首先:https://www.google.ch/?gfe_rd=cr&ei=6eISWYObOqLC8gfkzp3wBg#q=html+css+for+dummies
关于php - 如何为 html 和 php 使用外部 css 样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43888452/