【问题标题】:How to display Serial Numbers with mysql query result?如何使用 mysql 查询结果显示序列号?
【发布时间】:2011-10-28 22:52:34
【问题描述】:

我有一个脚本,可以显示“批处理”表中的这些字段的信息 - 批处理名称、类、批处理讲师。但是我想在显示数据时在左侧显示动态生成的序列号。例如:

 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>

【问题讨论】:

    标签: mysql codeigniter codeigniter-2


    【解决方案1】:

    也许我理解错了,但是......一个简单的计数器呢? (在您在 SQL 中订购记录之后)。 您需要将每页的项目数传递给视图(在此 sn-p 中:$per_page),然后从 URI($this-&gt;uri-&gt;segment(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;
     ?>
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 实际上,当您显示数据时,您可能希望以列出的顺序显示它。例如,您以这种方式写一些名字 - Bryan Adams、Bon Jovi、Steve Vai。但是,如果您按照 1. Bryan Adams 2. Bonjovi 3. Steve Vai 的列出顺序显示相同的内容。它看起来更好。这就是我要的。我可以猜到逻辑,那就是在您进行查询以获取数据之后,您计算结果中的行数,然后执行一些循环操作并显示。感谢您的帮助
      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 2015-12-18
      相关资源
      最近更新 更多