简体   繁体   English

在HTML页面上显示博客文章

[英]Displaying blog posts on HTML page

include "connection file";
$query = "SELECT * FROM Blog";
$result = mysqli_query($query);
$num_results = mysqli_num_rows($result);
for($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc($result);
echo "<div class="blogEntry"><h4>" . $row['title'] . "</h4><h5>" . $row['date'] . "</h5><p>"
    . $row['text'] . "</p></div>";
}  

Hi, so, I'm trying to loop through all my blog posts and display them to the page in order so the newest entry is first but I can't seem to get it too work. 嗨,所以,我试图遍历我所有的博客文章,并按顺序将它们显示在页面上,以便最新的文章排在第一位,但我似乎无法使其正常工作。 tried a few different ways and always white screen! 尝试了几种不同的方法,并且总是白屏! Any help would really be appreciated :). 任何帮助将不胜感激:)。 I'm also going to add a search function and filters, I don't need this now but any suggestions where to look to find info about implementing these would also be really helpful. 我还将添加一个搜索功能和过滤器,现在不需要了,但是任何寻找实现这些信息的建议都将非常有帮助。 Link to where I'm putting this code on my website: http://www.obeytoplay.com/ . 链接到我在网站上放置此代码的位置: http : //www.obeytoplay.com/ Thanks! 谢谢!

tried a few different ways and always white screen! 尝试了几种不同的方法,并且总是白屏!

That's because there's a syntax error in your code. 那是因为您的代码中存在语法错误。 Either escape the inner double quotes( " ) using backslash( \\ ) or use single quotes( ' ). 可以使用反斜杠( \\ )来转义内部的双引号( " )或使用单引号( ' )。

Method(1): 方法(1):

include "connection file";
$query = "SELECT * FROM Blog";
$result = mysqli_query($query);
$num_results = mysqli_num_rows($result);
for($i=0; $i<$num_results; $i++) {
    $row = mysqli_fetch_assoc($result);
    echo "<div class=\"blogEntry\"><h4>" . $row['title'] . "</h4><h5>" . $row['date'] . "</h5><p>" . $row['text'] . "</p></div>";
}  

Method(2): 方法(2):

include "connection file";
$query = "SELECT * FROM Blog";
$result = mysqli_query($query);
$num_results = mysqli_num_rows($result);
for($i=0; $i<$num_results; $i++) {
    $row = mysqli_fetch_assoc($result);
    echo "<div class='blogEntry'><h4>" . $row['title'] . "</h4><h5>" . $row['date'] . "</h5><p>" . $row['text'] . "</p></div>";
}  

I'm trying to loop through all my blog posts and display them to the page in order so the newest entry is first 我试图遍历我所有的博客文章,并将它们按顺序显示在页面上,以便最新的文章排在第一位

Use ORDER BY in conjunction with SELECT clause to reorder the result set, like this: ORDER BYSELECT子句结合使用可以对结果集重新排序,如下所示:

SELECT * FROM Blog ORDER BY column_name DESC/ASC;

Here's the reference: 这是参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM