我正在使用一个简单的PHP脚本来获取我博客的所有文章,并将其显示在带分页的页面上。
我将在我的主页上使用相同的脚本,但是,我想在第一个回显结果之后,在剩余的5个之前放置一个特殊的导航。
这是我正在使用的脚本:
<?php
$posts_sql = "SELECT * FROM publication_posts WHERE section = '".$id."' ORDER BY id DESC LIMIT ".$offset.", ".$display_limit.""; // 6 Posts
$posts_res = mysqli_query($con, $posts_sql);
while($post = mysqli_fetch_assoc($posts_res)){
$post_id = $post["id"];
...
$post_date = date("jS F, Y", strtotime($post_added));
$posts .= "
<div class=\"post\" id=\"".$post_id."\">
// Content
<div class=\"postDivide\"></div>
";
?>
<?php
$navigation = "
// Code for navigation bar
";
?>
<html>
<?php echo $posts; ?>
</html>
在第一篇文章被回显后,是否可以将$navigation
置于$posts
内?它是如何完成的?
答案 0 :(得分:2)
您是否考虑过这样做?
$posts = fetch_posts(6);
render_post($posts[0]);
render_navigation();
foreach (array_slice($posts, 1) as $post) {
render_post($posts);
}
<?php
function fetch_posts($section, $start, $num) {
global $con;
$posts_sql = "SELECT * FROM publication_posts WHERE section = '".$id."' ORDER BY id DESC LIMIT ".$start.", ".$num."";
$posts_res = mysqli_query($con, $posts_sql);
$posts = [];
while($post = mysqli_fetch_assoc($posts_res)){
$posts[] = $post;
}
return $posts;
}
function render_post($post) {
$post_id = $post["id"];
$post_date = date("jS F, Y", strtotime($post_added));
return "
<div class=\"post\" id=\"".$post_id."\">
// Content
<div class=\"postDivide\"></div>
";
}
function render_navigation() {
return "
// Code for navigation bar
";
}
?>
<html>
<?php
$section_id = "??";
$posts = fetch_posts($section_id, 0, 6);
echo render_post($posts[0]);
echo render_navigation();
foreach (array_slice($posts, 1) as $post) {
echo render_post($posts);
}
?>
</html>