如何在 CodeIgniter 中将分页集成到我的搜索中?

2022-01-04 00:00:00 search php codeigniter pagination

我已经使用 codeigniter 进行了简单的搜索,没有 ajax 之类的,我想集成分页类,我已经阅读了一些示例,他们使用了这样的库 $this->table->generate($results); 并且我需要在每个结果中都有一个按钮来编辑该条目.我已经这样做了,但是现在用这种显示分页的方式我遇到了一个大问题,请问有人能帮我解决这个问题吗?我只需要在 CI 中找到线索或东西

我把我的模型、控制器和视图放在这里,以防万一

控制器

公共函数searchdescription(){//$keyword = $this->input->post('keyword');$keyword = $_POST['keyword'];$this->load->model('searchdesc/searchdesc_model');$data['retorno'] = $this->searchdesc_model->querydb($keyword);$this->load->view('searchdesc/searchresults_view', $data);}

型号

function querydb($data,$num, $offset){$queryresult = $this->db->query("select * from data where deleteflag=0 and title like '%$data%' or text like '%$data%' LIMIT $num, $offset ");返回 $queryresult->result_array();}

查看

foreach ($retorno as $val){回声 $val['id'];回声 $val['title'];回声 $val['text'];...这里有一些在每一行中自动创建的表单,我需要继续制作}

解决方案

这是一个例子(不使用模型)

公共函数 big(){$this->load->library('分页');$this->load->library('table');$config['base_url'] = base_url().'/site/big/';$where = "bot = '2' OR bot = '0'";$this->db->where($where);$config['total_rows'] = $this->db->count_all_results('visitors');$config['per_page'] = 15;//$config['num_links'] = 20;$config['full_tag_open'] = '

';$config['full_tag_close'] = '

';$this->分页->初始化($config);$where = "bot = '2' OR bot = '0'";$this->db->where($where);$this->db->select('id, ip, date, page, host, agent, spammer, country, total, refer');$this->db->order_by("date", "DESC");$data['records'] = $this->db->get('visitors', $config['per_page'], $this->uri->segment(3));$this->table->set_heading('Id', 'IP', 'Date', 'Page', 'Host', 'Agent', 'Spam', 'Country', 'Total', 'Referer');$this->db->select_sum('total', 'trips');$query = $this->db->get('visitors');$data['trips'] = $query->result();$this->load->view('site_view', $data);}

在我想要表格的视图中:

table->generate($records);?><?php echo $this->分页->create_links();?>

要在行中添加按钮,请执行以下操作

foreach($记录为$row){$row->title = ucwords($row->title);$this->table->add_row($row->日期,锚(main/blog_view/$row->id",$row->title),$row-> 状态,anchor("main/delete/$row->id", $row->id, array('onClick' => "return confirm('Are you sure you want to delete?')")),锚(main/fill_form/$row->id",$row->id));}$table = $this->table->generate();回声 $table;

当然你会修改以适应你的需要

I've used codeigniter to make an easy search, no ajax or something, and I want to integrate the pagination class, I have read some examples and they use a library like this $this->table->generate($results); and I need in each result a button to edit that entry. I already did, but now with that way of showing the pagination I have a big problem, please, could anyone help me with this issue? I just need a clue or something to find in CI

I put here my model, controller and view, just in case

controller

public function searchdescription()
     {
     //$keyword = $this->input->post('keyword');
     $keyword = $_POST['keyword']; 
     $this->load->model('searchdesc/searchdesc_model');
     $data['retorno'] = $this->searchdesc_model->querydb($keyword);
     $this->load->view('searchdesc/searchresults_view', $data);
}

model

function querydb($data,$num, $offset)
    {

    $queryresult = $this->db->query("select * from data where deleteflag=0  and title like     '%$data%' or text like '%$data%' LIMIT $num, $offset ");
    return $queryresult->result_array();
    }

view

foreach ($retorno as $val)
{
echo $val['id'];
echo $val['title'];
echo $val['text'];
...here are some forms autocreated in each row that i need to keep making it
}

解决方案

Here is an example (not using a model)

public function big()
{
    $this->load->library('pagination');
    $this->load->library('table');

    $config['base_url'] = base_url().'/site/big/';
    $where = "bot = '2' OR bot = '0'";
    $this->db->where($where);
    $config['total_rows'] = $this->db->count_all_results('visitors');
    $config['per_page'] = 15;
    //$config['num_links'] = 20;
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';

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

    $where = "bot = '2' OR bot = '0'";
    $this->db->where($where);
    $this->db->select('id, ip, date, page, host, agent, spammer, country, total, refer');
    $this->db->order_by("date", "DESC");
    $data['records'] = $this->db->get('visitors', $config['per_page'], $this->uri->segment(3));

    $this->table->set_heading('Id', 'IP', 'Date', 'Page', 'Host', 'Agent', 'Spam', 'Country', 'Total', 'Referer');

   $this->db->select_sum('total', 'trips');
   $query = $this->db->get('visitors');
   $data['trips'] = $query->result();

    $this->load->view('site_view', $data);      
}

In the view where I want the table:

<?php echo $this->table->generate($records); ?>
<?php echo $this->pagination->create_links(); ?>

to add buttons in the rows do something like this

foreach($records as $row){
$row->title = ucwords($row->title);

$this->table->add_row(
$row->date,
anchor("main/blog_view/$row->id", $row->title),
$row->status,    
anchor("main/delete/$row->id", $row->id, array('onClick' => "return confirm('Are you sure you want to delete?')")),
anchor("main/fill_form/$row->id", $row->id)
);
}
$table = $this->table->generate();
echo $table;

Of course you will modify to fit your needs

相关文章