2014-03-03 12 views
1

我顯示與新聞箱。每個盒子都來自不同的類別。帶有一個主要帖子的介紹塊和介紹和幾個僅鏈接

這裏是什麼,我盡力去完成的截圖:

Display boxes with news from every category

但只檢查設計,讓PHP代碼。這是過時的問題。 (對不起,把這個鏈接,但我不能附加圖像,低分)

後主試圖顯示其餘的新聞,但我只能顯示主要職位,之後列出的職位在第一篇文章是重複的。

這裏是我的代碼:

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 &raquo;</a> 
      <div class="clearfix"></div> 
     </div><!-- end div.latest-posts --> 
    </div><!-- end div.category-box-content --> 
</div><!-- end div.category-box --> 

我這是怎麼顯示每箱(也許不是這個問題很重要):

   $post->category_box("1", "World"); 
       $post->category_box("2", "Politics"); 
       $post->category_box("3", "Culture"); 
       $post->category_box("4", "Technologies"); 
       $post->category_box("5", "Sports"); 
       $post->category_box("6", "Art"); 

我該如何做到這一點?

非常感謝!

+0

很難說出你的實際問題在這裏(以及它與第一個不同)。在第一個帖子顯示爲「僅鏈接」之後,剩下的帖子是否會顯示,因此不是_full_張貼視圖,主要問題?那麼你可以簡單地爲方法'category_box'添加第三個參數,將其命名爲'$ show_full_view'或者其他內容 - 然後在第一次調用時傳遞'true',而對於以下所有內容傳遞'false'。並且,在該方法中,您根據該值決定要顯示的內容。 – CBroe

+0

嗯!有趣。聽起來很難,但我會嘗試!謝謝你的想法。 :) –

+0

哦..參數在帖子的整個框中...嗯.. –

回答

0

那麼,如果我是對的,即使該類別適用於主帖子,您也希望將主帖子從類別框中排除。現在,在您的視圖的一部分,你這樣做:

<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邏輯放在視圖之外。 ,如果你的目標是可讀和可維護的代碼。

+0

嘿!感謝名單!但是,$ main_post_id是什麼,它來自哪裏?我沒有它,我看不到我可以如何使用。我的所有帖子都是一樣的。 –

+0

嗨。你必須自己將它傳遞給控制器​​中的視圖。您也可以在類別框功能中檢查主帖子ID。但是你也必須自己把它傳遞給函數。坦率地說,沒有你的全部代碼,我不能告訴你你應該從哪裏得到它。 –