Codeigniter视图正在加载视图两次

时间:2017-08-30 13:15:52

标签: php codeigniter

这是控制器中的整个索引函数

public function index(){

 if (isset($_POST['age_search'])) {

    $config['base_url'] = base_url()."school/school_reports/";
    $config['per_page'] = 20;
    $config['total_rows'] = $this->school_info_model->get_count_reports($this->session->userdata('school_name'));
    $config['num_links'] = 5;

    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] = "</ul>";

    $config['first_link'] = "First";
    $config['first_tag_open'] = "<li>";
    $config['first_tag_close'] = "</li>";

    $config['next_tag_open'] = "<li>";
    $config['next_tag_close'] = "</li>";

    $config['cur_tag_open'] = "<li><a href='#'>";
    $config['cur_tag_close'] = "</a></li>";

    $config['prev_tag_open'] = "<li>";
    $config['prev_tag_close'] = "</li>";

    $config['num_tag_open'] = "<li>";
    $config['num_tag_close'] = "</li>";

    $this->pagination->initialize($config);


    if($this->uri->segment(3)){
        $page = $this->uri->segment(3);
    }else{
        $page = 0;
    }

     $age = $_POST['age'];

     $data['list'] = $this->school_info_model->get_school_all_reports_list($age, $config['per_page'], $page);
     $data['links'] = $this->pagination->create_links();
     $data['report'] = array('a','b','c','d','e','f1','g','h');



  }elseif(isset($_POST['show'])) {

            $annexure = $_POST['reports'];

             $ids=$this->school_model->get_schools_data_generic_uids();

             if ($ids) {


        $data['students'] = $this->school_model->get_school_students_data($ids);    



        foreach ($data['students'] as $h) {
                $pids[] = $h['uid'];
                    }
                $total = array_unique($pids);

                $data['total_students'] =count($total);


            if ($annexure == 'a') {


                $this->load->view('analyst/header');
                $this->load->view("analyst/age/annexure_a", $data);
                $this->load->view('analyst/footer');

            }elseif ($annexure == 'b') {


                $this->load->view('analyst/header');
                $this->load->view("analyst/age/annexure_b", $data);
                $this->load->view('analyst/footer',$data);

            }elseif ($annexure == 'c') {


                $this->load->view('analyst/header');
                $this->load->view("analyst/age/annexure_c", $data);
                $this->load->view('analyst/footer_d',$data);

            }elseif ($annexure == 'd') {


                $this->load->view('analyst/header');
                $this->load->view("analyst/age/annexure_d", $data);
                $this->load->view('analyst/footer_d',$data);

            }

 }else{

      echo "No record found";
 }

 }

      $data['classes']=$this->users_model->get_classes();
      $data['cities']=$this->general_model->get_cities();

      $this->load->view('analyst/header');
      $this->load->view("school/School_reports_generic",$data);
      $this->load->view('analyst/footer');

}

这是我的代码我想知道为什么这部分代码加载视图,即使在上述块中条件为真后

  $data['classes']=$this->users_model->get_classes();
  $data['cities']=$this->general_model->get_cities();

  $this->load->view('analyst/header');
  $this->load->view("school/School_reports_generic",$data);
  $this->load->view('analyst/footer');

此视图正在视图之后加载,该视图处于真实状态时。

我希望当条件变为真时,只有在条件体应该加载时查看。

2 个答案:

答案 0 :(得分:1)

  

这是我的代码我想知道为什么这部分代码甚至会加载视图   如果条件在上面的块中为真

因为在加载视图后的if / else块中,没有returndie()exit(),所以在if / else块内执行代码后,块控制来了直到函数/方法结束,最后你有以下语句

$data['classes']=$this->users_model->get_classes();
$data['cities']=$this->general_model->get_cities();

$this->load->view('analyst/header');
$this->load->view("school/School_reports_generic",$data);
$this->load->view('analyst/footer');

答案 1 :(得分:0)

试试这种方式

if (!$this->input->post()) {
    $data['classes']=$this->users_model->get_classes();
    $data['cities']=$this->general_model->get_cities();

    $this->load->view('analyst/header');
    $this->load->view("school/School_reports_generic",$data);
    $this->load->view('analyst/footer');
} esle {
    // whole first part
}