2012-12-04 48 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