Php 在codeigniter中显示默认值
在主页中,应显示所有主题。单击主题时,必须显示其各自的章节。第一次,即当主题未被选择时,必须显示主题id为1的章节。在下面的代码中,我是否必须从索引中调用get_chapters(),并使get_chapters()返回章节或其他更好的方法Php 在codeigniter中显示默认值,php,codeigniter,Php,Codeigniter,在主页中,应显示所有主题。单击主题时,必须显示其各自的章节。第一次,即当主题未被选择时,必须显示主题id为1的章节。在下面的代码中,我是否必须从索引中调用get_chapters(),并使get_chapters()返回章节或其他更好的方法 class Home extends CI_Controller { private $data = array(); public function index() { $this->load->model('subj
class Home extends CI_Controller {
private $data = array();
public function index()
{
$this->load->model('subjects_model');
if($query = $this->subjects_model->get_all()) {
$data['subjects'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(!is_null($id)) {
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
} else {
$query = $this->chapters_model->get('chapters');
}
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
}试试看
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
如果
$id
为null
,则默认的主题id
将变为1
或LSE,它将使用现有的主题id
进行搜索,并通过index()函数调用它
public function index($id=null)
{
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}