Search code examples
phpcodeignitercodeigniter-4

While passing values from one function to another function the view files are not loading in codeigniter 4?


I need help. In a Controller when I am calling one function from another function the view files are not loading. Only showing the blank page.

Here in the below code, I need to pass the array $calc from calc() function to sem1() function. The array values are passed in the function but the view files in the sem1() function are not loading, showing only a blank page. I am using CodeIgniter 4. Please help.

    public function calc()
    {
        $calc = [
            'regulation'    => $this->request->getPost("regulation"),
            'branch'        => $this->request->getPost("branch"),
            'calculation'   => $this->request->getPost("calculation"),
            "semester"      => $this->request->getPost("semester")
        ];

        if ($calc['calculation'] == "GPA") {
            if ($calc['semester'] == "Semester 1") {
                $this->sem1($calc);
            }
        }
    }

    public function sem1($calc = '')
    {
        $this->builder->select('elective, sub_code, sub_name');
        $this->builder->where('semester', $calc['semester']);
        $this->builder->where('branch', $calc['branch']);
        $query = $this->builder->get();
        $isElective = [0, 0, 0, 0, 0];

        return view('au/auform', ['query' => $query, 'semester' => $semester, 'isElective' => $isElective]);
    }

Solution

  • You should add an echo to render view:

    public function calc(){
        $calc = [
            'regulation'    => $this->request->getPost("regulation"),
            'branch'        => $this->request->getPost("branch"),
            'calculation'   => $this->request->getPost("calculation"),
            "semester"      => $this->request->getPost("semester")
        ];
    
        if ($calc['calculation'] == "GPA") {
            if ($calc['semester'] == "Semester 1") {
                echo $this->sem1($calc);
            }
        }
    }
    

    For more look up the docs - LINK