我从数据库中获取数据并且必须在我的视图部分中显示它,实际上,我不知道如何在我的视图中获取所获取的数据?
并且还没有正确创建控制器,那么控制器中还剩下什么?
这是模型
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();
}
,控制器是
$veddingPlanData = $this->PlanModel->getPlanById($planId);
$veddingPlanTaskMappingData = $this->PlanModel->getPlanAndTaskMappingByPlanId($planId);
$allVedingTasks = $this->VeddingTaskModel->getAllVeddingTask();
当我盯着学习Codeigniter时,我问过这个问题。 所以我想添加正确的方法来将查询结果加载到视图中。将查询结果传递给控制器,然后加载该视图:
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
]);
}
答案 0 :(得分:3)
将您的控制器更新为以下代码
$data['veddingPlanData'] = $this->PlanModel->getPlanById($planId);
$data['veddingPlanTaskMappingData'] = $this->PlanModel->getPlanAndTaskMappingByPlanId($planId);
$data['allVedingTasks'] = $this->VeddingTaskModel->getAllVeddingTask();
$this->load->view('front/view', $data);
将 'front/view'
替换为您查看文件的路径/名称。
访问数据,在您的视图文件var_dump()
或print_r()
变量$veddingPlanData
,$veddingPlanTaskMappingData
或$allVedingTasks
答案 1 :(得分:2)
首先,您需要调整模型 - 下面我将向您展示一个示例
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;
}
现在您的控制器应该看起来像
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);
}
}
最后是你的观点的一个例子
foreach($arrVeddingPlanData AS $objWeddingPlan)
{
var_dump($objWeddingPlan);
}
除了给定的例外,我强烈建议 - 不要只在模型中返回结果,而应该在模型中根据需要准备数据并将数据数组返回给传递的控制器