2011-10-28 28 views
0

我有一個腳本,顯示這些字段的信息 - batchname,class,批處理表中的batchinstructor。但是當我顯示數據時,我想在左側顯示動態生成的序列號。例如:如何使用mysql查詢結果顯示序列號?

Serial Number BatchName  Class    Batch Instructor 
    1.    Solar  Class Five    John 
    2.    Lunar  Class six     Bon Jovi 

我試了很多,但它不工作。你能否幫我解決這個問題?請注意,這些序列號不是來自數據庫。

這裏是我的控制器:

<?php 
    class Batchlist extends CI_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); 

       } 

} >

這裏是我的模型:

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

這裏是我查看

<h1>Batch List </h1> 
      <?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> 

回答

0

你說的序列數字不是來自數據庫。他們從哪裏來?如果您希望數據庫生成它們,您可以創建一個名爲id的列並將其設置爲主鍵並使其自動遞增。

+0

實際上,當您顯示數據時,您可能想按照列出的順序顯示數據。例如,你寫了一些名字像Bryan Adams,Bon Jovi,Steve Vai。但是,如果你按照列出的順序顯示這個相同的東西,比如1. Bryan Adams 2. Bonjovi 3. Steve Vai。它看起來更好。這就是我要的。我可以猜出這個邏輯,那是在查詢獲取數據之後,你計算結果中的行數,然後做一些循環和顯示。謝謝你的幫助 –

4

也許我誤解了,但是...簡單的計數器呢? (在SQL中訂購記錄之後)。 您需要傳遞每個頁面上的項目數量(在此代碼段中:$per_page),然後從URI($this->uri->segment(n))中檢索當前頁面。 (2 * 20)+1即從第(1 * 20)+1即21開始,第3頁從第(1 * 20)+1開始, 41等等...

<?php 
    $cur_page = $this->uri->segment(n) ? intval($this->uri->segment(n)) : 1; 
    $i = (($cur_page-1) * $per_page) +1; 
    foreach ($records as $row) : 
    ?> 
    <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 
     ++$i; 
     endforeach; 
?> 

,無論你想你可以把櫃檯上,我添加了一個列簡單,但如果你想在其他地方只需將櫃檯那裏。