2012-12-04 120 views
0
收集SQL數據

我使用的MVC框架,基於PHP控制器功能在PHP

在我的控制,我有以下功能:

public function listing($tag=null, $page=1) 
{ 
    $this->posts('`online`=1', $tag, $page, '%s/%d/'); 
    $this->locations('`parent`=1'); 
} 

在同一文件中,我有2個功能收集從2個MySQL表,一個是位置DATAS:

private function locations($query) 
{ 
    // Collect Locations 
    $locations = new Locations('WHERE '.$query.' ORDER BY `name` ASC'); 
    $total_locations = $locations->count(); 

    // Template 
    require Template::render('Posts', 'listing.html'); 
} 

,另一個用於帖子:

private function posts($query, $tag, $page) 
{ 
    // Collect Posts 
    $posts = new Post('WHERE '.$query.' ORDER BY `date` DESC'); 
    $total_posts = $posts->count(); 
    $pages = ceil($total_posts/24); 

     if($page < 1) $page = 1; 
     if($page > $pages) $page = $pages; 

     $min = $page - 4; 
     $max = $page + 4; 

     if($min < 1) 
     { 
      $max += 1 - $min; 
      $min = 1; 
     } 

     if($max > $pages) 
     { 
      $min += $pages - $max;   
      if($min < 1) $min = 1;   
      $max = $pages; 
     } 

     $posts->query('LIMIT '.($page*24-24).',24'); 


    // Template 
    require Template::render('Posts', 'listing.html'); 
} 

在公共函數(列表)中,當第一行是$this->posts...時,它正確顯示帖子但不顯示位置(它只顯示「Array」。

如果我先放$this->locations...,那麼它會正確顯示Loctions而不是Posts。

我沒有你更多的代碼顯示,因爲我相信它來自一個書面方式錯誤在我listing function

我怎樣才能既顯示陣列?

在我的視圖頁面,我展示位置/職位這樣的:

<?php while($location = $locations->fetch($i)): ?> 
    <p> 
     <?php echo $location->title; ?> 
    </p> 
<?php endwhile; ?> 

回答

0

通過合併2 private functions together和剛剛從public function

去除$this->locations('=1');設計被稱爲2次,一次與地點和另一個時間與崗位解決了這個問題,因爲我所謂的模板頁面2倍。

解決方案只是將兩個功能合併在一起!

0

我認爲這個問題是在這裏

require Template::render('Posts', 'listing.html'); 

不知現在這些參數的方法,但我的猜測是什麼第一個是模板中的變量名稱,對於它們兩個,您將其設置爲Posts。

+0

很好的捕獲,但實際上第一個參數是文件夾'my_path/Posts/listing.html'的名稱 所以它們都是相同的,因爲我想要2個數組顯示在同一頁上 – PhilMarc

+0

可以包含源代碼這個文件的。 – Gustek

+0

除了<?php 類廣告延伸控制器 {'在開頭 – PhilMarc