2013-02-17 132 views
0

我有一个看起来像这样的方法的控制器的数据组合时,创建分页链接:如何从两个不同的查询

public function browsecategory($category_id) 
    {    

     //find any subcategories for this category 
     $this->load->model('category/category_model'); 
        $this->load->model('category/product_category_model'); 
     $records['categories'] = $this->category_model->find_all_by('parent_id', $category_id); 

        //add some product data too. 
        $records['products'] = $this->product_category_model->find_all_by('category_id', $category_id); 
     Template::set('records', $records); 
     Template::render(); 
    }//end browsecategory 

我见过的笨分页“东西”所有的例子是使用一个查询。 我需要结合两个数据集并在一个视图上提供服务。

有什么建议吗?

编辑1

我试着按照以下MDeSilva的建议。尽管分页对象正确计算了要创建的链接数量,但所有项目都显示在所有页面上。

这里是在模型中获取数据的代码:

public function get_categories_and_products($limit=12, $offset=0, $category_id=null) 
{ 
    print '<BR>the function got the following offeset:'.$offset; 
    $query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight "; 
    $query = $query."FROM bf_categories cat "; 
    $query = $query."WHERE cat.parent_id=".$category_id; 
    $query = $query." AND cat.category_id <>".$category_id; 
    $query = $query.") UNION ("; 
    $query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight"; 
    $query = $query." FROM bf_product p "; 
    $query = $query."Inner join bf_product_category cp "; 
    $query = $query."on p.product_id=cp.product_id "; 
    $query = $query."Where cp.category_id=".$category_id.")"; 

    $this->db->limit($limit, $offset); 
    $catsandprods= $this->db->query($query); 
    return $catsandprods->result() ; 
} 

而这里的控制器代码:

public function browsecategory($category_id, $offset=0) 
    { 
      $this->load->library('pagination'); 

      $total = $this->product_model->get_cats_prods_count($category_id); 

      $config['base_url'] = site_url('/product/browsecategory/'.$category_id); 
      $config['uri_segment'] = 4; 
      $config['total_rows'] = $total; 
      $config['per_page'] = 5; 
      $config['num_links'] = 10; 

      $this->pagination->initialize($config); 
      $offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0; 
         print $offset; 
      //Call the model function here to get the result, 
      $records= $this->product_model->get_categories_and_products(5,$offset,$category_id); 
      //add to breadcrumb trail 
      $this->build_bread_crumb_trail($category_id); 
      $breadcrumbs = $this->breadcrumbs->expand_to_hyperlinks(); 

      Template::set('currentcategory',$category_id); 
      Template::set('breadcrumbs', $breadcrumbs); 
      Template::set('records', $records); 
      Template::render();  
    } 

我调试,我可以看到的代码行“$ this-> db-> limit($ limit,$ offset);”在模型中不起作用。它总是返回完整的记录集... 你能告诉我我错过了什么吗? 谢谢。

+0

你的问题到底是什么?你想为产品和类别分页链接吗? – Jeemusu 2013-02-18 02:58:07

回答

0

分页类并不关心数据源是如何构建的,只是让你把它想要的数据结果对象交给它。因此,您只需在您的数据查询中通过限制&偏移量,或者拉出所有数据并在之后进行分片。

但是,我并不真正了解您如何将这些不同的数据位合并为一个结果来显示 - 您是否尝试显示所有类别,然后对产品进行分页?如果是这样,你是设置错误

+0

不,我打算一起展示所有类别和产品,每个类别和产品都有自己的超链接到不同的页面。类别超链接将显示另一组产品/类别...其中产品超链接会将您带到产品详细信息页面。你能给我一个特定的代码例子,说明我会如何完成这个任务吗?我在发布之前尝试了你描述的内容......但第一个分页对象只是覆盖了第二个分页...所以当你说:“你只需将限制和偏移量传递到你的数据查询中”就可以扩展你的意思。 – dot 2013-02-18 01:25:51

0

只需使用PHP函数array_merge

<?php $beginning = 'foo'; $end = array(1 => 'bar'); 
$result = array_merge((array)$beginning, (array)$end); 
$this->load->view('view.php', $result); 
?> 

和提取根据用于数组的键。 这是非常简单的&工作

1

这是产生在CI分页链接的方式,为您的要求有连接的查询,

public function index($offset = 0) { 
    $language_id = 1; 

    $artwork_id = null; 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    { 
     $artwork_id = $this->input->post('serach_artwork_id', TRUE) ? $this->input->post('serach_artwork_id', TRUE) : null; 
     $data['artwork_id'] = $artwork_id; 

    } 

    $this->load->library('pagination'); 
    $limit = 10; 

    $total = $this->Artwork_model->get_artwork_count($language_id, $artwork_id); 

    $config['base_url'] = base_url().'artwork/index/'; 
    $config['total_rows'] = $total; 
    $config['per_page'] = $limit; 
    $config['uri_segment'] = 3; 

    $config['first_link'] = '<< First'; 
    $config['last_link'] = 'Last >>'; 
    $config['next_link'] = 'Next ' . '&gt;'; 
    $config['prev_link'] = '&lt;' . ' Previous'; 
    $config['num_tag_open'] = '<span class="number">'; 
    $config['num_tag_close'] = '</span>'; 

    $config['cur_tag_open'] = '<span class="current"><a href="#">'; 
    $config['cur_tag_close'] = '</a></span>'; 

    $this->pagination->initialize($config); 

    //Call the model function here to get the result, 
    $data['artworks'] = $this->Artwork_model->get_artworks($language_id, $limit, $offset, $artwork_id); 

    $this->template->write('title', 'Artwork : Manage Artwork'); 
    $this->template->write_view('content', 'artwork/index', $data); 
    $this->template->render(); 
} 

下面是一个例子与多个查询中加入模拟

public function get_artworks($language_id = 1, $limit = 10, $offset = 0, $arwork_id = null) 
    {   
     $this->db->select('a.id, a.price, a.is_shop, at.title,at.status,at.date_added,ats.name as artist_name'); 
     $this->db->from('artworks a'); 
     $this->db->join('artwork_translations at', 'a.id = at.artwork_id'); 
     $this->db->join('artists ats', 'a.artist_id = ats.id'); 
     $this->db->where('at.language_id', $language_id); 
     if(!is_null($arwork_id) && !empty($arwork_id) && $arwork_id != 'all') 
     { 
      $this->db->where('a.id =', $arwork_id); 
     } 
     $this->db->order_by('a.id DESC'); 
     $this->db->limit($limit, $offset); 

     $artworks = $this->db->get(); 

     return $artworks->result(); 
    } 

在查看

<?= $this->pagination->create_links(); ?> 
+0

你可以在我的文章中查看编辑1吗?谢谢! – dot 2013-02-20 01:47:01

+0

其实..忽略上面的评论。我可能已经找出了我的问题。 – dot 2013-02-20 02:01:51

+0

MDeSilva,我试着按照你的例子,但它不适合我。你可以在我的文章中查看Edit 1吗?谢谢! – dot 2013-02-20 03:07:15

相关问题