• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用PHP设置页面标题

用户头像
it1352
帮助1

问题说明

我希望将我的网页标题设置为Ultan.me-任何帖子标题.我希望它显示帖子标题.这些帖子将提交到MySQL数据库,标题行称为"title".这个小问题会给您任何帮助.

I wish to set the title of my webpage to Ultan.me - Whatever the post title. I want it to display the post title. The posts are submitted to a MySQL database and the title row is called "title". Any help is appreciated with this small question.

更新:

现在是页面本身,但不显示标题.我应该打开php文档并连接到与当前位置不同的地方的数据库吗?

Here is the page itself now but it doesn't display the title. Should I open the php document and connect to my database somewhere different to it's current locations?

代码(唯一必要的部分是开头):

The Code (The only necessary piece is the beginning):

<html>
<head>

<meta name="keywords" content="Mac user Ultan Casey TheCompuGeeks UltanKC">
<title>Ultan.me - <?echo $title;?></title>

<link rel="stylesheet" href="https://www.it1352.com/css/styles.css" type="text/css" />
<script type="text/javascript"
src="http://ajax.谷歌apis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" 
src="https://www.it1352.com/js/jquery.labelify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(":text").labelify();
});
</script>
<style>
a {text-decoration:none}

</style>
</head>
<body>
<div id="main">

<!-- Menu Start -->
<div id="menu">
<ul>
<li><a href="https://www.it1352.com/index.php">home</a></li>
<li><a href="https://www.it1352.com/index.php">about me</a></li>
<li><a href="https://www.it1352.com/index.php">archives</a></li>
<li><a href="https://www.it1352.com/index.php">contact</a></li>
<li><a href="https://www.it1352.com/index.php">gallery</a></li>

</ul>
</div>
<!-- Menu End --> 

<img src="https://www.it1352.com/images/banner.png" />
<div id="content">
<div id="posts">
<?php
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid ID specified.");
}

$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE   LIMIT 1";

$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());

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

    $date = date("l F d Y", $row['timestamp']);

    $title = stripslashes($row['title']);
    $entry = stripslashes($row['entry']);
    $get_categories = mysql_query("SELECT * FROM php_blog_categories WHERE `category_id` = $row[category]");
    $category = mysql_fetch_array($get_categories);


    ?>
<p><?php echo "<p id='post-title'><strong><a href=\"post.php?id=". $id . "\">" . $title . "</a></strong></p>"; ?><br /><br />
<div id="entry"><?php echo $entry; ?>
</div><br /><br />
<p id="date">Posted in <a href="https://www.it1352.com/category.php?category=<?php echo $row['category']; ?>"><?php echo $category['category_name']; ?></a> on <?php echo $date; ?></p>
</p>
<h2 id="share-title">Share This Post</h2>
<div id="social-share">
<li id="link-right"><a href="http://twitter.com/home?status=
I just read <?php echo $title; ?> at http://ultan.me/post.php?id=<?php echo $id; ?>"><center>Twitter</center></a></li>
<li id="link-left"><a href="http://digg.com/submit?url=http://ultan.me/post.php?id=<?php echo $id; ?>"><center>Digg</center></a></li>
<br>
<li id="link-right"><a href="http://www.facebook.com/sharer.php?u=http://ultan.me/post.php?id=<?php echo $id; ?>&t=<?php echo $title; ?>"><center>Facebook</center></a></li>
<li id="link-left"><a href="http://www.谷歌.com/buzz/post?url=http://ultan.me/post.php?id=<?php echo $id; ?>
"><center>Google Buzz</center></a></li>
<div class="clr"></div>
</div>
<h2 id="comments-title">Comments</h2>
 <div id="comment-list">
    <?php

}
$commenttimestamp = strtotime("now");

$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
    $timestamp = date("l F d Y", $row['timestamp']);
      printf("<div class='comment-ind'><p id='comments'><a   href=\"%s\">%s</a> %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
    print("<p class='comments'>" . stripslashes($row['comment']) . "</p><div class='clr'><br></div></div>");

  }
?>
<div class="clr"></div>
<form   method="post" action="process.php">

<p><input type="hidden" name="entry"   value="<?php echo $id; ?>" />

<input type="hidden" name="timestamp"   value="<?php echo $commenttimestamp; ?>">

<input type="text" name="name"   title="Name (required)" /><br />

<input type="text" name="email"   title="Mail (will not be published) (required)" /><br />

<input type="text" name="url"   title="Website" value="http://" /><br />

<br />
<textarea  title="Your Comment Goes Here" name="comment" id="comment"></textarea></p>

<p><input type="submit" name="submit_comment"   value="Add Comment" /></p>

</form>
</div>
<div id="pages">

<?php
$total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) AS num FROM php_blog"));
$total_pages = ceil($total_results['num'] / $blog_postnumber);
if ($page > 1) {
    $prev = ($page - 1);
    echo "<a href=\"?page=$prev\">&lt;&lt;  Newer</a> ";
}
for($i = 1; $i <= $total_pages; $i  ) {
    if ($page == $i) {
        echo "$i ";
    }
    else {
        echo "<a href=\"?page=$i\">$i</a> ";
    }
}
if ($page < $total_pages) {
   $next = ($page   1);
   echo "<a href=\"?page=$next\">Older &gt;&gt;</a>";
}

?>
</div>
</div>
</div>
<!-- Sidebar Start -->
<div class="sidebar">

<!-- Item 1 -->
<div id="side-item"> 
<h2>
<a href="http://www.dailybooth.com/UltanCasey">
<img src="https://www.it1352.com/images/db-icon.jpg">Dailybooth
</a></h2>
<div id="side-item-content">
<center>
<img src="http://dailybooth.com/UltanCasey/latest/medium.jpg" />
</center>
</div>

</div>

<!-- Item 2 -->

<div id="side-item">
<h2><img src="https://www.it1352.com/images/connect.jpg" />Connect</h2>
</div>
<div id="side-item-content">
<div class="tweet-title"><p><a href="http://www.twitter.com/UltanKc">Latest Tweet:</a></p></div>
<div id="tweet">
<?php

function getTwitterStatus($userid){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=1";

function auto_link_twitter ($text)
{
    // properly formatted URLs
    $urls = "/(((http[s]?:\/\/)|(www\.))?(([a-z][-a-z0-9] \.)?[a-z][-a-z0-9] \.[a-z] (\.[a-z]{2,2})?)\/?[a-z0-9._\/~#&=;% ?-] [a-z0-9\/#=?]{1,1})/is";
    $text = preg_replace($urls, " <a href='https://www.it1352.com/$1'>$1</a>", $text);

    // URLs without protocols
    $text = preg_replace("/href=\"www/", "href=\"http://www", $text);

    // Twitter usernames
    $twitter = "/@([A-Za-z0-9_] )/is";
    $text = preg_replace ($twitter, " <a href='http://twitter.com/$1'>@$1</a>", $text);

    // Twitter hashtags
    $hashtag = "/#([A-Aa-z0-9_-] )/is";
    $text = preg_replace ($hashtag, " <a href='http://hashtags.org/$1'>#$1</a>", $text);
    return $text;
}

$xml = simplexml_load_file($url) or die("could not connect");

       foreach($xml->status as $status){
       $text = $status->text;
       }
       echo auto_link_twitter ($text);
 }


getTwitterStatus("UltanKC");

?>
</div>
<br>
<ul>
<li id="social"><a href="https://www.it1352.com/1824228.html">YouTube</a></li>
<li id="social"><a href="https://www.it1352.com/1824228.html">Twitter</a></li>
<li id="social"><a href="https://www.it1352.com/1824228.html">LastFM</a></li>
<li id="social"><a href="https://www.it1352.com/1824228.html">Email</a></li>
</ul>

</div>
<!-- Item 2 End-->
<div id="side-item">
<h2><img src="https://www.it1352.com/images/archive.jpg" />Archives</h2>
</div>
<div id="archive-side">
<?php
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');

$result = mysql_query("SELECT FROM_UNIXTIME(timestamp, '%Y') AS get_year, COUNT(*) AS entries FROM php_blog GROUP BY get_year");

while ($row = mysql_fetch_array($result)) {
    $get_year = $row['get_year'];
    $entries = $row['entries'];

    echo "<li id='tag'><a href=\"archives.php?year=" . $get_year . "\">Entries from " . $get_year . "  (" . $entries . ")<br /></a></li>";
}

$result1 = mysql_query("SELECT * FROM php_blog_categories ORDER BY category_name ASC");

while($row = mysql_fetch_array($result1)) {

    $result2 = mysql_query("SELECT COUNT(`id`) AS entries FROM php_blog WHERE category = $row[category_id]");
    $num_entries = mysql_fetch_array($result2);

    echo '<li id="tag"><a href="https://www.it1352.com/category.php?category=' . $row['category_id'] . '">' . $row['category_name'] . ' (' . $num_entries['entries'] . ')</a></li>';

}
?>
</div>

</div>

<div   />
</div>
<!-- Sidebar End  -->
<div id="footer">
<p> &copy; Ultan Casey 2010</p>
<p style="margin-top: -18px; float:right"><a href="https://www.it1352.com/index.php">Home</a> | <a href="https://www.it1352.com/about.php">About Me</a> | <a href="mailto:ultankc@thecompugeeks.com">Email Me</a></p>
</div>
</div>
</div>
</body>
</html>
?>

正确答案

#1

这是我使用的方法(用于类似的事情,而不仅仅是标题):

Here's the method I use (for similar things, not just title):

<?
ob_start (); // Buffer output
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title><!--TITLE--></title>
</head>

<body>
<?
$pageTitle = 'Title of Page'; // Call this in your pages' files to define the page title
?>
</body>
</html>
<?
$pageContents = ob_get_contents (); // Get all the page's HTML into a string
ob_end_clean (); // Wipe the buffer

// Replace <!--TITLE--> with $pageTitle variable contents, and print the HTML
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
?>

PHP通常可以执行任何代码位并将所有输出直接打印到浏览器.如果您说"echo 'Some text here.';",该字符串将被发送到浏览器并从内存中清空.

PHP usually works be executing any bits of code and printing all output directly to the browser. If you say "echo 'Some text here.';", that string will get sent the browser and is emptied from memory.

输出缓冲的作用是说将所有输出打印到缓冲区.请保留在缓冲区上.在我告诉你之前不要向浏览器发送任何内容."

What output buffering does is say "Print all output to a buffer. Hold onto it. Don't send ANYTHING to the browser until I tell you to."

所以它的作用是将所有页面的HTML缓冲到缓冲区中,然后最后在标记之后,它使用ob_get_contents()获取缓冲区的内容(通常是页面的所有HTML源代码)将会发送给浏览器的代码)并将其放入字符串中.

So what this does is it buffers all your pages' HTML into the buffer, then at the very end, after the tag, it uses ob_get_contents () to get the contents of the buffer (which is usually all your page's HTML source code which would have been sent the browser already) and puts that into a string.

ob_end_clean()清空缓冲区并释放一些内存.我们不再需要源代码,因为我们将其存储在$ pageContents中.

ob_end_clean () empties the buffer and frees some memory. We don't need the source code anymore because we just stored it in $pageContents.

然后,最后,我进行一个简单的查找&在页面的源代码($ pageContents)上替换"的任何实例,并将其替换为$ pageTitle变量所设置的内容.当然,它将用您的页面标题替换<title><!--TITLE--></title>.之后,我会像浏览器一样回显$ pageContents.

Then, lastly, I do a simple find & replace on your page's source code ($pageContents) for any instances of '' and replace them to whatever the $pageTitle variable was set to. Of course, it will then replace <title><!--TITLE--></title> with Your Page's Title. After that, I echo the $pageContents, just like the browser would have.

它有效地保留了输出,因此您可以在将其发送到浏览器之前对其进行操作.

It effectively holds onto output so you can manipulate it before sending it to the browser.

希望我的评论很清楚. 如果您想确切了解它的工作原理,请在php手册( http://php.net/ob_start )中查找ob_start().你应该):)

Hopefully my comments are clear enough. Look up ob_start () in the php manual ( http://php.net/ob_start ) if you want to know exactly how that works (and you should) :)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgabfe
系列文章
更多 icon
同类精品
更多 icon
继续加载