Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why I am getting error undefined variable in display controller array in view?

My 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);
        }

    }

}

My view:

<?php

    echo $posts;
?>
like image 502
Sudheer Babu Avatar asked Dec 21 '25 01:12

Sudheer Babu


1 Answers

You getting error because view dont get $posts when validation condition is false for that you initialize $data['posts']=""; in the start or in every if condition as per your coding style

 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);
        }

    }

or you can check array before in view, like this; if you don't want to change your controller

<?php
    if(isset($posts))
    echo $posts;
?>
like image 199
Prafulla Avatar answered Dec 22 '25 14:12

Prafulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!