用PHP在页面上发布回音或将其与CSS合并

用PHP在页面上发布回音或将其与CSS合并,php,html,css,echo,Php,Html,Css,Echo,我有一个包含多个页面的网站,在每个页面上我都使用CSS显示一个带有用户用户名和注销功能的标题。虽然在一个页面上,当我从数据库中回显一个表时,我的标题被推到了底部。表格在屏幕顶部回响,将我的标题推到下方。有人能告诉我如何将回声定位在屏幕中间的页面上,或者将回声移动到HTML的一部分。这是我页面的代码 多谢各位 <?php session_start(); include_once 'dbconnect.php'; if(!isset($_SESSION['user'])) { h

我有一个包含多个页面的网站,在每个页面上我都使用CSS显示一个带有用户用户名和注销功能的标题。虽然在一个页面上,当我从数据库中回显一个表时,我的标题被推到了底部。表格在屏幕顶部回响,将我的标题推到下方。有人能告诉我如何将回声定位在屏幕中间的页面上,或者将回声移动到HTML的一部分。这是我页面的代码

多谢各位

  <?php  
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
//maintain SESSION user_id

$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);



//Select video name and question

$query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion";

$result = mysql_query($query);

if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}

$enisatquestion = "<table >";

while ( $row = mysql_fetch_assoc($result) ) {


    $enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";

}

$enisatquestion .= "</table>";    

echo $enisatquestion;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>   
</div> 
</body>  
</html>  

欢迎-
NHSCT eNISAT教程
欢迎

标记之外输出html内容将产生无效标记-从技术上讲,它将在浏览器中呈现,但不受css和常规文档流的影响-请不要这样做

对于您的情况,如果需要,在html之前使用php进行记录集选择,但保存输出生成的html,直到在文档体中需要它为止,如下所示

<?php  

    session_start();
    include_once 'dbconnect.php';

    if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array( $res );

    $query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion";

    $result = mysql_query( $query );
    /* A default message if the query fails or there are no records */
    $enisatquestion='<h2>Sorry, there are no records</h2>';


    /* you cannot output content outside the html tags, not valid ~ it will work but NO */
    if( $result ) {/* if there is a recordset, proceed and generate html table */
        $enisatquestion = "<table >";
        while ( $row = mysql_fetch_assoc($result) ) {
            $enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";
        }
        $enisatquestion .= "</table>";    
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
    <?php
        /* output the html table here, below your header */
        echo $enisatquestion;
        /*
            If the query failed then the default gets displayed
        */
    ?>  
</div> 
</body>  
</html>

欢迎-
NHSCT eNISAT教程
欢迎

标记之外输出html内容将产生无效标记-从技术上讲,它将在浏览器中呈现,但不受css和常规文档流的影响-请不要这样做

对于您的情况,如果需要,在html之前使用php进行记录集选择,但保存输出生成的html,直到在文档体中需要它为止,如下所示

<?php  

    session_start();
    include_once 'dbconnect.php';

    if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array( $res );

    $query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion";

    $result = mysql_query( $query );
    /* A default message if the query fails or there are no records */
    $enisatquestion='<h2>Sorry, there are no records</h2>';


    /* you cannot output content outside the html tags, not valid ~ it will work but NO */
    if( $result ) {/* if there is a recordset, proceed and generate html table */
        $enisatquestion = "<table >";
        while ( $row = mysql_fetch_assoc($result) ) {
            $enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";
        }
        $enisatquestion .= "</table>";    
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
    <?php
        /* output the html table here, below your header */
        echo $enisatquestion;
        /*
            If the query failed then the default gets displayed
        */
    ?>  
</div> 
</body>  
</html>

欢迎-
NHSCT eNISAT教程
欢迎

Give
id
class
为PHP生成的元素提供样式。例如:
$enisatquestion.=”。现在来看看你的CSS:
.someClass{properties:values)
。在你的body标记中使用
,而不是在你的html标记的顶部使用这是无效的-你不能输出html标记之外的内容give
id
class
来为你的PHP生成的元素提供样式。例如:
$enisatquestion.=“”;
。现在,对于CSS:
.someClass{properties:values)
。在body标记内使用
,而不是在html标记顶部使用这是无效的-不能在html标记外输出内容