如何用mysql查询结果显示序列号?

发布于 2024-12-12 12:28:10 字数 3919 浏览 0 评论 0原文

我有一个脚本,显示“batch”表中这些字段的信息-batchname、class、batchinstructor。但我想在显示数据时在左侧显示动态生成的序列号。例如:

 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>

I have a script that displays information of these fields- batchname, class, batchinstructor from the table "batch". But I want to display dynamically generated serial number on the left side when I show the data. For example:

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

I have tried a lot but its not working. Would you please kindly help me to solve this? Please note that these serial number are not from database.

Here's my Controller:

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

                }   

}
?>

Here's my Model:

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

Here's my View

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天 2024-12-19 12:28:10

也许我误解了,但是......一个简单的计数器怎么样? (在 SQL 中订购记录之后)。
您需要将每页的项目数传递给视图(在此代码段中:$per_page),然后从 URI 检索当前页面($this->uri-> ;段(n))。

在第1页,计数器从(20*0)+1开始,即1。在第2页,从(1*20)+1开始,即21,在第3页,从(2*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;
 ?>

您可以将计数器放置在任何您想要的位置,为了简单起见,我添加了一列,但如果您想要将其放置在其他地方,只需将计数器放置在那里即可。

Maybe I misunderstood, but...what about a simple counter?? (after you ordered your records in SQL).
You need to pass to the view the number of items per page (in this snippet: $per_page), then you retrieve the current page from the URI ($this->uri->segment(n)).

At page 1, counter starts from (20*0)+1, i.e. 1. At page 2, starts from (1*20)+1 ie 21, at page 3 from (2*20)+1 ie 41 and so on...

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

You can place the counter wherever you want, I added a column for simplicity but if you want it somewhere else just place the counter there.

迎风吟唱 2024-12-19 12:28:10

你说序列号不是来自数据库。那么他们来自哪里呢?如果您希望数据库生成它们,您可以创建一个名为 id 的列并将其设置为主键并使其自动递增。

You said the serial numbers are not from the database. Where are they from then? If you want the database to generate them you can create a column named id and set it as the primary key and have it auto increment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文