phpcodeignitermodel-view-controller

load multiple query result to the view


I fetch data from the database and have to show it in my view part and actually, I don't know how to get my fetched data in my VIEW?

And also don't create controller properly, so what is remaining in my controller?

This is the models

public function getPlanById($planId){    
    $this->db->where('plan_id', $planId);           
    $query = $this->db->get('tbl_plan');       
 }

public function getPlanAndTaskMappingByPlanId($planId){
    $this->db->where('plan_id', $planId);
    $query = $this->db->get('plan_task_mapping');
    return $query->result();    
}    

public function getAllVeddingTask(){        
    $query = $this->db->get('vedd_plan_task');
    return $query->result();
}

and the controller is

$veddingPlanData = $this->PlanModel->getPlanById($planId);  
$veddingPlanTaskMappingData = $this->PlanModel->getPlanAndTaskMappingByPlanId($planId);
$allVedingTasks = $this->VeddingTaskModel->getAllVeddingTask();

I have asked this question when i stared to learn Codeigniter. So I like to add the correct way to load query resul to the view.Pass the query result to the controller and then to load thet view:

public function controller_function(){
    $veddingPlanData = $this->PlanModel->getPlanById($planId);   
    $veddingPlanTaskMappingData = $this->PlanModel->getPlanAndTaskMappingByPlanId($planId);
    $allVedingTasks = $this->VeddingTaskModel->getAllVeddingTask();

    $this->load->view('your_view',['veddingPlanData' => $veddingPlanData, 
    'veddingPlanTaskMappingData'=>$veddingPlanTaskMappingData,
    'allVedingTasks'=>$allVedingTasks
    ]);
}

Solution

  • First of all you've to adapt your models - below i'll show you an example

    public function getPlanById($planId)
    {
    
        $this->db->where('plan_id', $planId);
        $query = $this->db->get('tbl_plan');
    
        $arrData = array();
    
        if ($query->num_rows() > 0)
        {
            foreach($query->result() AS $row)
            {
                $arrData[] = $row;
            }
        }
    
        return $arrData;
    }
    
    public function getPlanAndTaskMappingByPlanId($planId)
    {
        $this->db->where('plan_id', $planId);
        $query = $this->db->get('plan_task_mapping');
    
        $arrData = array();
    
        if ($query->num_rows() > 0)
        {
            foreach($query->result() AS $row)
            {
                $arrData[] = $row;
            }
        }
    
        return $arrData;
    }
    
    
    public function getAllVeddingTask()
    {
        $query = $this->db->get('vedd_plan_task');
        $arrData = array();
    
        if ($query->num_rows() > 0)
        {
            foreach($query->result() AS $row)
            {
                $arrData[] = $row;
            }
        }
    
        return $arrData;
    }
    

    now your controller should look like

    class YourController extends CI_Controller
    {
        public function your_method()
        {
            $this->load->model("PlanModel");
            $this->load->model("VeddingTaskModel");
    
            $arrViewData = array(
                "arrVeddingPlanData" => $this->PlanModel->getPlanById($planId),
                "arrVeddingPlanTaskMappingData" => $this->PlanModel->getPlanAndTaskMappingByPlanId($planId),
                "arrAllVedingTasks" => $this->VeddingTaskModel->getAllVeddingTask()
            );
    
    
            $this->load->view("yourview", $arrViewData);
        }
    }
    

    and finally an example for your view

    foreach($arrVeddingPlanData AS $objWeddingPlan)
    {
        var_dump($objWeddingPlan);
    }
    

    besides of the given exmaple i strongly recommend - instead of returning just a result in your models you should prepare the Data for your needs in the model and return an array of data to the passing controller