在数据库中获取的数据旁边对表进行编号

时间:2015-05-01 07:20:43

标签: php mysql codeigniter

我想使用codeigniter在数据库中提取数据。

我为每个数据生成了一个表行,并希望在实际数据描述之前有一个编号。

这是我的看法: 请注意<td>#</td>上的<tbody></tbody>。那是我想要编号的地方。

<div class="table-responsive">
        <table class="table table-hover table-bordered table-striped table-condensed">
            <caption><h3><strong>Master List of Suppliers</strong></h3></caption>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Supplier Code</th>
                    <th>Description</th>
                    <th>Address</th>
                </tr>
            </thead>
            <tbody>
            <?php
                foreach ($view_masterlist_suppliers as $row) {
                    echo 
                        '<tr>
                            <td>#</td>
                            <td><a href="'.site_url("site/itemspersupplier?i=".$row->SUP_CODE).'">'.$row->SUP_CODE.'</a></td>
                            <td>'.$row->SUP_DESC.'</td>';
                            if(empty($row->SUP_ADD1)){ echo '<td><strong>- NO ADDRESS HAS BEEN ENCODED -</strong></td>';}
                            else if(empty($row->SUP_ADD2)){ echo '<td>'.$row->SUP_ADD1.'</td>';}
                            else{ echo '<td>'.$row->SUP_ADD1.', '.$row->SUP_ADD2.'</td>';}
                    echo    '</tr>';
                }
            ?>
            </tbody>
        </table>
    </div>

这是我的控制者:

public function itemspersupplier(){         
        $data["title"] = "Supplier Master List";

        $i = $_GET['i'];
        $this->load->model("get_model");
        $data["view_items_per_suppliers"] = $this->get_model->view_items_per_suppliers($i);

        $this->load->view("templates/header", $data);
        $this->load->view("templates/nav", $data);
        $this->load->view("pages/items_per_supplier", $data);
        $this->load->view("templates/footer", $data);   }

这是我的模型:注意:我必须使用数据库,但没有什么可担心的:)

function view_items_per_suppliers($i){
        //Load db2 and run query
        $CI = &get_instance();
        //Seeting the second parameter to TRUE (Boolean) the function will
        //return the database object.
        $this->db2 = $CI->load->database('db2', TRUE);
        $this->db2->select('TBL_SUPPLIER.SUP_CODE,TBL_SUPPLIER.SUP_DESC,TBL_SUPPLIER.SUP_ADD1,TBL_SUPPLIER.SUP_ADD2,TBL_SUPPLIER.SUP_TEL1,TBL_SUPPLIER.SUP_TEL2,TBL_SUPPLIER.SUP_FAX1,TBL_SUPPLIER.SUP_FAX2,TBL_SUPPLIER.SUP_CONT,TBL_MASTER_ITEMS.IN_CODE,TBL_MASTER_ITEMS.ITE_DESC,TBL_MASTER_ITEMS.UNIT,TBL_MASTER_ITEMS.UNT_COST');
        $this->db2->from('TBL_SUPPLIER');
        $this->db2->join('TBL_MASTER_ITEMS', 'TBL_SUPPLIER.SUP_CODE = TBL_MASTER_ITEMS.SUP_CODE', 'inner');
        $this->db2->where('TBL_MASTER_ITEMS.SUP_CODE', $_GET['i']);
        //$this->db->limit('25');

        $view_items_per_suppliers = $this->db2->get();
        return $view_items_per_suppliers->result();
    }

感谢。

2 个答案:

答案 0 :(得分:1)

只需将您的代码更新为。这将为你工作

<tbody>
<?php
    $i = 1;
    foreach ($view_masterlist_suppliers as $row) {
       echo 
       '<tr>
       <td>'.$i++.'</td>
       <td><a href="'.site_url("site/itemspersupplier?i=".$row->SUP_CODE).'">'.$row->SUP_CODE.'</a></td>
       <td>'.$row->SUP_DESC.'</td>';
       if(empty($row->SUP_ADD1)){ echo '<td><strong>- NO ADDRESS HAS BEEN ENCODED -</strong></td>';}
       else if(empty($row->SUP_ADD2)){ echo '<td>'.$row->SUP_ADD1.'</td>';}
       else{ echo '<td>'.$row->SUP_ADD1.', '.$row->SUP_ADD2.'</td>';}
                echo    '</tr>';
      }
?>     

可以简单地实现

foreach ($view_masterlist_suppliers as $key => $row) {
echo 
'<tr>
<td>'.$key+1.'</td>

OR

foreach ($view_masterlist_suppliers as $key => $row) {
echo 
'<tr>
<td>'.++$key.'</td>

答案 1 :(得分:1)

你可以使用任何一个人

 foreach ($view_masterlist_suppliers as $index=>$row) {
        echo
        '<tr>
            <td>'.$index+1.'</td>
             //your rest code

    $index=0;
    foreach ($view_masterlist_suppliers as $index=>$row) {
        echo
        '<tr>
            <td>'.++$index.'</td>
            //your rest code