显示数据库值

时间:2011-10-27 11:28:18

标签: codeigniter codeigniter-2

我正在尝试使用Codeigniter的表和分页库来显示数据库中的数据。在我的模型中,除了其他列之外,我想从我的表“batch”中获取列“batchid”的信息,但是当我显示其他数据时,不希望在视图文件中显示它。

但是因为我已经将“batchid”包括在内 - (以下)

              $this->db->select('batchname, class, batchid, batchinstructor'); 

它显示了视图中“batchid”列的所有信息,这是我不想要的。我只是想检索batchid的值,用它来锚定“batchname”。

我已经尝试了很多,但它不起作用。请你帮帮我吗?

先谢谢

这是我的模特

//Function To Create All Student List
    function batch_list()
    {   
        $config['per_page'] = 15;
        $this->db->select('batchname, class,batchid, batchinstructor');
        $this->db->order_by("batchid", "desc"); 
        $rows = $this->db->get('batch',$config['per_page'],$this->uri->segment(3))->result_array();

        $sl = $this->uri->segment(3) + 1; // so that it begins from 1 not 0
        foreach ($rows as $count => $row)
            {
                array_unshift($rows[$count], $sl.'.');
                 $sl = $sl + 1;

             $rows[$count]['batchname'] = anchor('batch_list/get/'.$row['batchid'],$row['batchname']);
             $rows[$count]['Edit'] = anchor('update_student/update/'.$row['batchname'],img(base_url().'/support/images/icons/edit.png'));
             $rows[$count]['Delete'] = anchor('report/'.$row['batchname'],img(base_url().'/support/images/icons/cross.png'));


            }
        return $rows;
    }
//End of Function To Create All Student List 

这是我的控制器

   function index(){
            $this->load->helper('html');  
            $this->load->library('pagination');
            $this->load->library('table');
          $this->table->set_heading('Serial Number','Batch Name','Class','Batch Instructor','Edit','Delete');

            $config['base_url'] = base_url().'batchlist/index';
            $config['total_rows'] = $this->db->get('batch')->num_rows();
            $config['per_page'] = 15;
            $config['num_links'] = 5;
            $config['full_tag_open'] = '<div class="pagination" align="center">';
            $config['full_tag_close'] = '</div>';

            $this->pagination->initialize($config);
            $data['tab'] = "Batch List";                
            $this->load->model('mod_batchlist');
            $data['records']= $this->mod_batchlist->batch_list();
            $data['main_content']='view_batchlist';
            $this->load->view('includes/template',$data);

        }   

1 个答案:

答案 0 :(得分:0)

我不确定您在视图中执行的操作,但您可以通过循环遍历数据集来手动添加行:

foreach ($records->result() as $r)
{
    $this->table->add_row($r->serial, 
                          $r->batchname, 
                          $r->class, 
                          $r->batchinstructor,
                          $r->edit,
                          $r->delete
                  );
}
echo $this->table->generate();

通过这种方式,您可以控制数据传输到您的表格。