2011-10-30 114 views
0

我想從我的數據庫獲取數據,當我展示它,我想顯示的序列號(就像列表),例如:顯示與查詢結果序號

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

我曾嘗試一些與PHP循環,但我無法讓它工作。你能請你幫忙嗎?請注意,通過序列號我並不是指從數據庫中檢索到的值。

感謝提前:)

這裏是我的模型

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

這裏是我的控制器

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> 

回答

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> 
+0

@ Hailwood。非常感謝您的幫助。我嘗試了您發佈的代碼,它顯示了我想要的序列號,但當我點擊下一頁(我正在使用codeigniter的分頁庫)時,它會自動重置並重新從1開始顯示。例如:在第一頁中序列號是這樣的> 1,2,3 ..但是當我移動到下一頁時,它又從1開始。請您好好看看這個嗎?在此先感謝:) –

+0

嗯,做我兩件事,a)當你進入第三頁(第一頁,第二頁,第三頁)時,你的網址是什麼樣的?例如example.com/pages/30?或者是什麼? b)每頁顯示多少條記錄? – Hailwood

+0

非常感謝Hailwood對你的幫助。我的URL看起來像這樣 - http:// localhost/sundial/studentlist_active /頁面加載時(第一頁),當我點擊進入下一頁時,URL看起來像這樣>>> http:// localhost/sundial/studentlist_active/index/5我每頁顯示5條記錄。再一次非常感謝你。 :) –

1

在我的情況下,第四個和第五個參數分別是每個頁面的頁碼和行數 ie: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; 

前和使用內循環只是增加我的價值。

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

嘗試this..work像this..1,2,3,4,5

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