在PHP的页面上发布一个回显,或者将其与CSS合并


Postiton an echo on the page in PHP or incorporate it with CSS

我有一个包含多个页面的网站,在我的每个页面上,我都使用 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>  

<html></html> 标签之外输出 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>