gpt4 book ai didi

php - 为什么我在 View 中的显示 Controller 数组中出现错误 undefined variable ?

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:35 24 4
gpt4 key购买 nike

我的 Controller :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyAppointment extends CI_Controller {

function __construct()
{
parent::__construct();


$this->load->model('appointment_model','',TRUE);

}

function index()
{
$this->load->library('form_validation');

$this->form_validation->set_rules('rdatetimepicker', 'Appointment date', 'trim|required|xss_clean');

if($this->form_validation->run() == FALSE)
{
$this->load->view("pages/appointment");
}
else
{
$this->appointment_model->insert();
$data['posts'] = "hih";

$this->load->view('pages/appointment',$data);
}

}

}

我的看法:

<?php

echo $posts;
?>

最佳答案

你收到错误是因为当你初始化 $data['posts']=""; 根据您的编码风格在开始或每个 if 条件中

 function index()
{
$this->load->library('form_validation');

$this->form_validation->set_rules('rdatetimepicker', 'Appointment date', 'trim|required|xss_clean');

if($this->form_validation->run() == FALSE)
{
$data['posts']=""; //declare posts here
$this->load->view('pages/appointment',$data);

}
else
{
$this->appointment_model->insert();
$data['posts'] = "hih";

$this->load->view('pages/appointment',$data);
}

}

或者你可以在 View 之前检查数组,像这样;如果你不想改变你的 Controller

<?php
if(isset($posts))
echo $posts;
?>

关于php - 为什么我在 View 中的显示 Controller 数组中出现错误 undefined variable ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com