我知道如何创建分页并在codeigniter中生成包含数据库信息的表,但我不知道如何显示表中每行的序列号。例如:
SL. Name Email
1. Srijon example@yahoo.com
2. Jake jake@yahoo.com
请您告诉我该怎么做?提前致谢
这是我如何创建分页和生成表(没有序列号)的控制器
function index(){
$this->load->library('pagination');
$this->load->library('table');
$this->table->set_heading('Student ID','Student Name','Batch','Edit','Delete');
$config['base_url'] = 'http://localhost/coaching/index.php/student_list/index';
$config['total_rows'] = $this->db->get('student')->num_rows();
$config['per_page'] = 15;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div id="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['tab'] = "Student List";
$this->load->model('mod_studentlist');
$data['records']= $this->mod_studentlist->student_list();
$data['main_content']='studentlist';
$this->load->view('includes/template',$data);
}
这是我的模特
function student_list()
{
$config['per_page'] = 10;
$this->db->select('studentid, studentname, batch');
$this->db->order_by("studentid", "desc");
$rows = $this->db->get('student',$config['per_page'],$this->uri->segment(3))->result_array();
foreach ($rows as $count => $row)
{
$rows[$count]['studentname'] = anchor('student_list/get/'.$row['studentid'],$row['studentname']);
$rows[$count]['Edit'] = anchor('update_student/update/'.$row['studentid'],'Update');
$rows[$count]['Delete'] = anchor('report/'.$row['studentid'],'Delete');
}
return $rows;
}
这是我的视图文件
<?php echo $this->table->generate($records);?>
<?php echo $this->pagination->create_links();?>
<script type="text/javascript" charset="utf-8">
$('tr:odd').css('background','#EAEAEA');
</script>
答案 0 :(得分:2)
您应该在模型函数中创建一个变量:
<?php
function student_list()
{
$config['per_page'] = 10;
$this->db->select('studentid, studentname, batch');
$this->db->order_by("studentid", "desc");
$rows = $this->db->get('student',$config['per_page'],$this->uri->segment(3))->result_array();
$sl = $this->uri->segment(3) + 1; // so that it begins from 1 not 0
foreach ($rows as $count => $row)
{
array_unshift($rows[$count], $sl.'.');
$sl = $sl + 1;
$rows[$count]['studentname'] = anchor('student_list/get/'.$row['studentid'],$row['studentname']);
$rows[$count]['Edit'] = anchor('update_student/update/'.$row['studentid'],'Update');
$rows[$count]['Delete'] = anchor('report/'.$row['studentid'],'Delete');
}
return $rows;
}
并修改您的控制器:
<?php
function index() {
$this->load->library('pagination');
$this->load->library('table');
$this->table->set_heading('SL.', 'Student ID','Student Name','Batch','Edit','Delete');
....
答案 1 :(得分:0)
Codeigniter有一个很棒的表库来显示html表格式的数据。在表中添加序列号是有时需要的,但是MySQL不容易提供行号,因此在使用表库时添加了此函数来完成工作。
以下是代码:
/**
* Set the serial no
*
* Add serial no to left of table
*
* @access public
* @param mixed
* @return mixed
*/
function add_sr_no(&$arr)
{ if (!is_array($arr)) return $arr;
$i=1;
foreach ($arr as &$values) {
if (!is_array($values))
break;
else
array_unshift($values, $i++);
}
return $arr;
}
只需将这些行添加到库中的table.php即可。 用法:
$rows = $query->result_array();
$this->table->add_sr_no($rows);
不要忘记在set_heading中添加第一列'Sr No'。 阅读帖子here。 希望这会有所帮助。
答案 2 :(得分:0)
您是否考虑过在客户端执行此操作? javascript库datatables很棒。它可以在用户端为您处理排序,并且可以使用ajax动态检索信息,因此在大型表上加载时间很快。 for under&lt;虽然可能不需要100行左右。我有排序/过滤表&gt; 100 000行,没有任何问题(使用ajax)
除非序列号具有语义含义,否则不向客户显示。它只是为他们过滤的东西。如果您的代码需要它们,但用户无需查看,只需将它们隐藏在data属性或其他内容中。