Php 显示块中有一个带有介绍的主帖子和几个只带有链接的帖子
我在盒子里展示新闻。每个盒子都来自不同的类别 以下是我尝试完成的屏幕截图: 但是只检查设计,留下PHP代码。这是一个过时的问题。(很抱歉放置此链接,但我无法附加图像。分数较低) 在主帖子之后,我尝试显示其余的新闻,但我只能显示主帖子,然后显示第一篇帖子重复的帖子列表 这是我的密码:Php 显示块中有一个带有介绍的主帖子和几个只带有链接的帖子,php,Php,我在盒子里展示新闻。每个盒子都来自不同的类别 以下是我尝试完成的屏幕截图: 但是只检查设计,留下PHP代码。这是一个过时的问题。(很抱歉放置此链接,但我无法附加图像。分数较低) 在主帖子之后,我尝试显示其余的新闻,但我只能显示主帖子,然后显示第一篇帖子重复的帖子列表 这是我的密码: public function category_box($category_id, $category_name) { $this->_db->query("SELECT * from
public function category_box($category_id, $category_name) {
$this->_db->query("SELECT * from posts WHERE category_id = '$category_id'");
$this->current_category_posts = $this->_db->resultset();
$posts = array();
foreach ($this->current_category_posts as $post) {
if ($post['category_id'] === $category_id) {
$posts[] = $post;
}
}
include VIEWS_PATH . 'homepage/category_box.php';
}
这是类别框的一个视图文件:
<div class="category-box">
<div class="category-name">
<?php print $category_name ?>
</div><!-- end div.category-name -->
<div class="category-box-content">
<h2><a href="#"><?php print $post['post_title']; ?></a></h2>
<div class="published-date">
<?php print $post['post_published_date']; ?>
</div><!-- end div.published-date -->
<div class="main-photo">
<a href="#"><img src="<?php print BASE_URL . 'public/uploads/images/' . $post['post_photo']; ?>" alt=""></a>
<span class="photo-caption">
<?php print $post['post_photo_caption']; ?>
</span><!-- end div.photo-caption -->
</div><!-- end div.main-photo -->
<div class="excerpt">
<a href="#"
title="<?php print $post['post_content']; ?>"><?php print $post['post_content']; ?></a>
</div><!-- end div.excerpt -->
<div class="latest-posts">
<div class="more-publications">
More publications in <?php print $category_name ?>
</div>
<ul>
<?php
foreach ($this->current_category_posts as $post_in_list) {
if ($post_in_list['category_id'] === $category_id) {
print '<li><a href="#" title="' . $post_in_list['post_title'] . '" >' .
$post_in_list['post_title'];
'</a></li>';
}
}
?>
</ul>
<a href="#" class="all-publications">All publications »</a>
<div class="clearfix"></div>
</div><!-- end div.latest-posts -->
</div><!-- end div.category-box-content -->
</div><!-- end div.category-box -->
我怎样才能做到这一点
多谢各位 好吧,如果我是对的,你想从分类框中排除主帖,即使该分类适用于主帖。现在在视图中执行此操作的部分:
<ul>
<?php
foreach ($this->current_category_posts as $post_in_list) {
if ($post_in_list['category_id'] === $category_id) {
print '<li><a href="#" title="' . $post_in_list['post_title'] . '" >'
. $post_in_list['post_title'] .
'</a></li>';
}
}
?>
</ul>
如果将主帖子id与类别帖子id进行比较,如下所示:
<ul>
<?php
foreach ($this->current_category_posts as $post_in_list) {
if ($post_in_list['category_id'] === $category_id && $post_in_list['id'] !== $main_post_id) {
print '<li><a href="#" title="' . $post_in_list['post_title'] . '" >'
. $post_in_list['post_title'] .
'</a></li>';
}
}
?>
</ul>
你已经到了。但是,建议您将PHP逻辑保留在视图之外,如果您的目标是可读性和可维护性的代码。很难说出您的实际问题是什么(以及它与第一个问题有何不同)。主要的问题是,第一篇文章之后的剩余文章是否显示为“仅链接”,而不是完整的文章视图?那么,您可以简单地向方法
category\u框
添加第三个参数,将其命名为$show\u full\u view
或其他什么–然后对第一个调用传递true
,对以下所有调用传递false
。在这个方法中,你可以根据这个值来决定显示什么。嗯!有趣。听起来很难,但我会试试!谢谢你的建议。:)哦参数在整个框中,并带有帖子。。。嗯,嘿!塔克斯!但是$main_post_id是什么?它来自哪里?我没有它,我也不知道如何使用它。我所有的帖子都一样。您必须自己将其传递给控制器中的视图。您还可以在分类框功能中检查主帖子id。但是您还必须自己将其传递给函数。坦白说,没有你的全部代码,我无法告诉你应该从哪里得到它。
<ul>
<?php
foreach ($this->current_category_posts as $post_in_list) {
if ($post_in_list['category_id'] === $category_id && $post_in_list['id'] !== $main_post_id) {
print '<li><a href="#" title="' . $post_in_list['post_title'] . '" >'
. $post_in_list['post_title'] .
'</a></li>';
}
}
?>
</ul>