Search code examples
codeignitercodeigniter-2

Displaying serial numbers with query result


I am trying to fetch data from my database and when I display it I want to show serial numbers (Just like a list) , example:

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

I have tried something with PHP Loops, but I couldn't make it work. Would you please kindly help me? please note that by serial numbers I don't mean values retrieved from database.

Thanks in Advance :)

Here's my Model

 //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

Here's mY Controller

 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);

}

Here's My View

 <?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>

Solution

  •  <?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>