我目前正在使用一个代码,我可以在两列中调用数据库中的帖子。我对两个列使用不同的查询,意味着我按类别调用帖子,左列中的几个类别和右列中的休息。两列均显示10条最新帖子。现在我插入更多的功能,这将加载更多的帖子。我更喜欢在两列下面使用单个加载更多按钮,而不是在每列中放置两个加载更多按钮。所以我的问题是这个加载更多的按钮会在点击时加载更多的帖子,直到他们没有帖子,现在假设我在左列中调用帖子的类别比右列的类别更多帖子它会使左列比右栏更长这是我不想要的。那么他们是一个更好的方式来调用两列中的帖子,而不是将一半的类别调用到一个列表中,而在其他列表中休息。
我目前使用的代码是:
<div id='container'>
<div id='lhs' class='column' style='width:370px;margin:10px 0px 10px 10px;'>
<?php
$query = "SELECT blogs_id, title, body, posted_by, full_name, bio, posted, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category='cat1' OR category='cat2' OR category='cat3' OR category='cat4' ORDER BY blogs_id desc LIMIT 12";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
$title = $row['title'];
$body = $row['body'];
$posted_by = $row['posted_by'];
$full_name = $row['full_name'];
$bio = $row['bio'];
$posted = $row['posted'];
$get_user_info = "SELECT * FROM userss WHERE username='$posted_by'";
$get_info = mysqli_query($con,$get_user_info);
$get_row = mysqli_fetch_assoc($get_info);
$profilephoto_info = $get_row['profile_photo'];
if ($profilephoto_info == "") {
$profilephoto_info = "./img/avatar_pic.png";
}
else {
$profilephoto_info = "./userdata/profile_photos/".$profilephoto_info;
}
echo "<div class='db'>
<h2>$title</h2>
<p>" .
(strlen($row['body'])>500?
substr( $row['body'],0,500)."...<a href='blog.php?blogId=" . $row['blogs_id'] . "' style='font-size:16px;color:black;background-color:#ffffff;text-decoration:none;'>Read more</a>":
$row['body']).
"</p><br />
<img src='$profilephoto_info' height='60'><br />
By: <a href='$posted_by'>$full_name</a>
<p>$bio</p>
<p>$posted</p><br /><hr /><br /><br /></div>";
}
?>
</div>
<div id='rhs' class='column' style='width:370px;margin:10px 10px 10px 0px;'>
<?php
$query = "SELECT blogs_id, title, body, posted_by, full_name, bio, posted, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category='cat5' OR category='cat6' OR category='cat7' ORDER BY blogs_id desc LIMIT 10";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
$title = $row['title'];
$body = $row['body'];
$posted_by = $row['posted_by'];
$full_name = $row['full_name'];
$bio = $row['bio'];
$posted = $row['posted'];
$get_user_info = "SELECT * FROM userss WHERE username='$posted_by'";
$get_info = mysqli_query($con,$get_user_info);
$get_row = mysqli_fetch_assoc($get_info);
$profilephoto_info = $get_row['profile_photo'];
if ($profilephoto_info == "") {
$profilephoto_info = "./img/avatar_pic.png";
}
else {
$profilephoto_info = "./userdata/profile_photos/".$profilephoto_info;
}
echo "<div class='db'>
<h2>$title</h2>
<p>" .
(strlen($row['body'])>500?
substr( $row['body'],0,500)."...<a href='blog.php?blogId=" . $row['blogs_id'] . "' style='font-size:16px;color:black;background-color:#ffffff;text-decoration:none;'>Read more</a>":
$row['body']).
"</p><br />
<img src='$profilephoto_info' height='60'><br />
By: <a href='$posted_by'>$full_name</a>
<p>$bio</p>
<p>$posted</p><br /><hr /><br /><br /></div>";
}
?>
答案 0 :(得分:0)
它不是重复代码的好方法。除了编码太多,您可以更改HTML设计。
<div id='container'>
<ul style="width:740px; margin:10px 0px 10px 10px;">
<?php
$query = "SELECT blogs_id, title, body, posted_by, full_name, bio, posted, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category='cat1' OR category='cat2' OR category='cat3' OR category='cat4' ORDER BY blogs_id desc LIMIT 12";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
$title = $row['title'];
$body = $row['body'];
$posted_by = $row['posted_by'];
$full_name = $row['full_name'];
$bio = $row['bio'];
$posted = $row['posted'];
$get_user_info = "SELECT * FROM userss WHERE username='$posted_by'";
$get_info = mysqli_query($con,$get_user_info);
$get_row = mysqli_fetch_assoc($get_info);
$profilephoto_info = $get_row['profile_photo'];
if ($profilephoto_info == "") {
$profilephoto_info = "./img/avatar_pic.png";
}
else {
$profilephoto_info = "./userdata/profile_photos/".$profilephoto_info;
}
echo "<li style='float:left; width:49%;'>
<div class='db'>
<h2>$title</h2>
<p>" .
(strlen($row['body'])>500?
substr( $row['body'],0,500)."...<a href='blog.php?blogId=" . $row['blogs_id'] . "' style='font-size:16px;color:black;background-color:#ffffff;text-decoration:none;'>Read more</a>":
$row['body']).
"</p><br />
<img src='$profilephoto_info' height='60'><br />
By: <a href='$posted_by'>$full_name</a>
<p>$bio</p>
<p>$posted</p><br /><hr /><br /><br /></div>
</li>";
}
?>
</ul>
<div style="clear:both;"></div>
</div>
使用&#34; li&#34;标签左侧浮动,宽度50%,两列布局。 所以不需要使用&#34;加载更多&#34;链接两次。只需调用一次并在&#34; ul&#34;
的最后添加记录检查此布局。它比负载更多的代码更好。使用这种布局更容易加载更多。