关于CI活动记录类使用的SQL问题

时间:2011-08-17 02:30:48

标签: php codeigniter activerecord php-5.3

我正在使用CodeIgniter Active Record Class来生成表,我的代码片段如下。

我尝试从DateBase表生成两个表; 以及名为Table_2010和Table_2011的两个HTML表。

/////////////////// YEAR 2010 ////////////////
// It works quite well to generate 2010 result table.

$this->db->select('year, distance, gender, rank, name, chiptime, racenumber');

                $this->db->order_by("year", "desc");

                $this->db->order_by("distance, gender, rank", "asc");

                $topNum = 5;
                $year = 2010;                   
                $array = array('rank <=' => $topNum, 'year' => $year);
                $this->db->where($array); 


                $this->load->model('eventmain_model');

                $data['results_2010'] = $this->eventmain_model->get_result_top5($city);

                $tmpl =  $this->common_model->html_table_config(); 

                $this->load->library('table');

                $this->table->set_template($tmpl);

                $this->table->set_heading(array('Year', 'Dis', 'gender','Rank', 'Name', 'ChipTime', 'RaceNo'));



/////////////////// YEAR 2011 ////////////////
// I need query several different fields from the same data base table.
// such as I query 'lap', otherwise 'chiptime'
// It doesn't work well, since it always included 2010 and 2011 data to my 2nd 2011 new table. 
// I want to remove 2010 rows from the 2011 table. How can I fix this?
// Is ther an method to new the db select object and generate my 2011 table?      
//Thanks.                   
                // load database class

                $this->db->select('year, distance, gender, rank, name, lap, racenumber');

                $this->db->order_by("year", "desc");

                $this->db->order_by("distance, gender, rank", "asc");

                $topNum = 5;
                $year = 2011;
                $this->db->where('rank <=', $topNum);
                $this->db->where('year', $year);



                $this->load->model('eventmain_model');

                $data['results_2011'] = $this->eventmain_model->get_result_top5($city);

                $tmpl =  $this->common_model->html_table_config(); 

                $this->load->library('table');

                $this->table->set_template($tmpl);

                $this->table->set_heading(array('Year', 'Dis', 'gender','Rank', 'Name', 'Lap', 'RaceNo'));

1 个答案:

答案 0 :(得分:0)

从CI官方论坛得到答案。

$this->table->clear();

我通过将代码添加到我的View页面来修复此问题。感谢。

echo 'Table_2010<br>';

$this->table->set_heading(array('Year', 'Dis', 'gender','Rank', 'Name', 'ChipTime', 'RaceNo')); 

echo $this->table->generate($results_2010);
$this->table->clear();

echo '<br>';
echo 'Table_2011<br>';
$this->table->set_heading(array('Year', 'Dis', 'gender','Rank', 'Name', 'Lap', 'RaceNo'));
echo $this->table->generate($results_2011);