我想将登录用户的名称作为页面标题。但我不知道该怎么做。我尝试过几种方法,但每种方法都显示出一个解析错误。这是我的用于登录用户然后显示其详细信息的PHP代码。
<?php
//if the login session does not exist therefore meaning the user is not logged in
if(strcmp($_SESSION['uid'],"") == 0){
//display and error message
echo "<center>You need to be logged in to user this feature!</center>";
}else{
//otherwise continue the page
//this is out update script which should be used in each page to update the users online time
$time = date('U')+50;
$update = mysql_query("UPDATE `employer` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
$display_query = mysql_query("SELECT * FROM employer WHERE `id` = '".$_SESSION['uid']."'");
echo "<table id='pageTable'><tbody><th>Your Details</th>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
echo "<tr><td>Name: </td><td>".$row['name']."</td><tr>";
$titlename = $row['name'];
echo "<tr><td>E-Mail ID: </td><td>".$row['email']."</td><tr>";
echo "<tr><td>Contact No.: </td><td>".$row['contact']."</td><tr>";
echo "<tr><td>Company: </td><td>".$row['company']."</td><tr>";
echo "<tr><td>Designation: </td><td>".$row['designation']."</td><tr>";
}
echo "</tbody>";
echo "</table>";
echo "<table><tr><td>";
echo '<div class="button"><a href="functions/logout.php">Logout</a></td></tr></table>';
//make sure you close the check if they are online
}
?>
答案 0 :(得分:2)
确保正确标记您的页面:doctype,html-head-body等。您可以这样做,当正文仍然“打开”时,只需从<?php
开始,然后按脚本。
然后,您的loginname-as-title代码的相关部分:
<head>
<title><?php echo $loginName ?></title><!-- thanks to Berry Langerak for noting 'echo' was missing -->
</head>
<body>
<?php
其中$loginName
当然是您想要显示的登录ID的var。
答案 1 :(得分:2)
在输出页面的<head>
部分之前,您需要获取所需的数据,然后在其中加入<title>
元素。
<title><?php echo htmlspecialchars($myTitle); ?></title>
答案 2 :(得分:0)
使用下面的代码将loginname显示为标题
echo '<script language="javascript">';
echo 'document.title = \''.$row['name'].'\'';
echo '</script>'