Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中显示默认值_Php_Codeigniter - Fatal编程技术网

Php 在codeigniter中显示默认值

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

在主页中,应显示所有主题。单击主题时,必须显示其各自的章节。第一次,即当主题未被选择时,必须显示主题id为1的章节。在下面的代码中,我是否必须从索引中调用get_chapters(),并使get_chapters()返回章节或其他更好的方法

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);
}