Php 如何显示MySQL数据库条目的摘录?
我在创建一个网页,其中包括一个博客部分,它目前显示在主页上的整个文章。我想将其设置为只显示条目的某一部分,即50个单词。然后,我希望能够设置它,以便在帖子下面有一个链接到帖子id的“阅读更多”按钮。我目前使用post.php?=#(#=不管帖子id是什么) 以下是网页:Php 如何显示MySQL数据库条目的摘录?,php,mysql,database,Php,Mysql,Database,我在创建一个网页,其中包括一个博客部分,它目前显示在主页上的整个文章。我想将其设置为只显示条目的某一部分,即50个单词。然后,我希望能够设置它,以便在帖子下面有一个链接到帖子id的“阅读更多”按钮。我目前使用post.php?=#(#=不管帖子id是什么) 以下是网页: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Blog Name</title&
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Blog Name</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<body>
<div id="upper-bar">
<div id="bar-content">
<a href="#">Home</a>
<a href="#">Archives</a>
<a href="#">Contact</a>
<a href="#">About</a>
<a href="#"><img src="images/twitter.png" id="tweet"></a><a href="#"><img src="images/feed.png" id="feed"></a>
</div>
</div>
<div id="clear">
</div>
<div class="main">
<h1>Blog Name</h1>
<div class="post-col">
<?php
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('tmlblog');
$sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 5";
$result = mysql_query($sql) or print ("Can't select entries 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']);
$id = $row['id'];
?>
<div id='post-info'><?php echo "<p id='title'><strong><a href=\"post.php?id=". $id . "\">" . $title . "</a></strong></p>"; ?><br /></div>
<div id="post">
<?php echo $entry; ?>
<!--<br /><br />
Posted on <?php echo $date; ?> !-->
</p>
</div>
</p>
</div>
<?php
}
?>
</div>
</div>
</body>
</html>
博客名
博客名
一定是文字吗?很长或很短的单词可能会导致不同的字符数。也许你应该做一个字符计数
如果是这样,您可以使用打印字符串的一部分(将50更改为所需的字符数):
。。。
或者,您可以使用select通过SQL选择子字符串
每个条目最多需要50个单词(假设单词用一个空格分隔)。在主页上,按如下方式查询您的博客帖子:
$sql = "SELECT id, title, blog_timestamp, LEFT(blog_contents, 50) AS txt
FROM php_blog ORDER BY timestamp DESC LIMIT 5";
然后,在处理结果时,修剪到最近的单词并添加一个链接。我该如何在我的博客中实现这一点?数字“50”是否限制文章显示50个字符?0是否做了任何重要的工作?(对不起,我太好奇了……我只是想学习!XD)是的。0表示“从字符串开头开始”。50表示“最多返回50个字符。”
SELECT SUBSTRING_INDEX(entry, ' ', 51) as entry FROM php_blog;
$truncatedEntry = substr($entry, 0, 50) . '...';
// truncate with word-wrapping
$truncatedEntry = substr($entry, 0, strpos(wordwrap($entry, 50), "\n")) . ' ...';
$sql = "SELECT id, title, blog_timestamp, LEFT(blog_contents, 50) AS txt
FROM php_blog ORDER BY timestamp DESC LIMIT 5";