black_belt
black_belt

Reputation: 6799

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>

Upvotes: 0

Views: 5693

Answers (3)

Prakash Tank
Prakash Tank

Reputation: 49

Try this..work like this..1,2,3,4,5

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

Upvotes: 0

Danish Backer
Danish Backer

Reputation: 61

In my case the fourth and fifth parameters where page number and rows per page respectively ie: example.com/category/page/1/10 first page with 10 rows per page.

so in this case just use this before the loop

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

and from inside the loop just increment the value of i.

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

Upvotes: 1

Hailwood
Hailwood

Reputation: 92641

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

Upvotes: 2

Related Questions