显示带有查询结果的序列号

时间:2011-10-30 15:39:13

标签: codeigniter codeigniter-2

我正在尝试从我的数据库中获取数据,当我显示它时,我想显示序列号(就像列表一样),例如:

 **Serial Number**      Name          Country
     1.               John             USA
     2.              Srijon            UK

我尝试过使用PHP Loops,但我无法使其工作。请你帮帮我吗?请注意,通过序列号,我不是指从数据库中检索的值。

提前致谢:)

这是我的模特

 //Function To Create All Batch List
  function batch_list($perPage,$uri) { 
    $this->db->select('*');
    $this->db->from('batch');
        $this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
    $this->db->order_by('batchid','DESC');
    $getData = $this->db->get('', $perPage, $uri);
    if($getData->num_rows() > 0)
    return $getData->result_array();
    else
    return null;
    }
 //End of Function To Create All Batch List

这是mY控制器

 function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';

$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page']
                      ,$this->uri->segment(3));
    $data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);

}

这是我的观点

 <?php if(count($records) > 0) { ?>
 <table id="table1" class="gtable sortable">
<thead>
<tr>
    <th>Batch Name</th>
    <th>Class</th>
    <th>Batch Instructor</th>
    <th>Edit/Delete</th>
</tr>
</thead>

<tbody>
    <?php foreach ($records as $row){ ?>
<tr>
    <td><a href="<?php echo base_url(); ?>batch/<?php echo $row['batchid']; ?>"><?php echo $row['batchname'];?>     </a></td>
    <td><?php echo $row['class'];?></td>
    <td><?php echo $row['teachername'];?></td>
    <td> <a href="<?php echo base_url(); ?>updatebatch/get/<?php echo $row['batchid']; ?>" title="Edit"><img  src="<?php echo base_url(); ?>                                                            support/images/icons/edit.png" alt="Edit" /></a>
    <a href="#" title="Delete"><img src="<?php echo base_url();  ?>support/images/icons/cross.png" alt="Delete" /></a>
    </td>
 </tr>
            <?php  } ?>

 </tbody>
 </table>
  <?php } ?>
   <div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?> 
</div>

 </div>

3 个答案:

答案 0 :(得分:2)

 <?php if(count($records) > 0) { ?>
 <table id="table1" class="gtable sortable">
<thead>
<tr>
            <th>Serial</th>
    <th>Batch Name</th>
    <th>Class</th>
    <th>Batch Instructor</th>
    <th>Edit/Delete</th>
</tr>
</thead>

<tbody>
    <?php $i = $this->uri->segment(3); foreach ($records as $row){ $i++; ?>
<tr>
            <td><?php echo $i; ?>.</td>
    <td><a href="<?php echo base_url(); ?>batch/<?php echo $row['batchid']; ?>"><?php echo $row['batchname'];?>     </a></td>
    <td><?php echo $row['class'];?></td>
    <td><?php echo $row['teachername'];?></td>
    <td> <a href="<?php echo base_url(); ?>updatebatch/get/<?php echo $row['batchid']; ?>" title="Edit"><img  src="<?php echo base_url(); ?>                                                            support/images/icons/edit.png" alt="Edit" /></a>
    <a href="#" title="Delete"><img src="<?php echo base_url();  ?>support/images/icons/cross.png" alt="Delete" /></a>
    </td>
 </tr>
            <?php  } ?>

 </tbody>
 </table>
  <?php } ?>
   <div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?> 
</div>

 </div>

答案 1 :(得分:1)

在我的例子中,第四个和第五个参数分别是页码和每页行数 即:example.com/category/page/1/10 第一页,每页10行。

所以在这种情况下只需在循环之前使用它

$i = $this->uri->segment(4)? (
     $this->uri->segment(4) + $this->uri->segment(5)? $this->uri->segment(5):0
     )
     :0;

并从循环内部增加i的值。

foreach ($result_obj as $key => $value)
{
$i++;
...
}

答案 2 :(得分:0)

试试这个..像这样的工作...... 1,2,3,4,5

$i = 0
while($condition)
{
    echo '<td>'.$i++.'</td>';
}