2
votes

Here is my controller function as far as i can see there is no error but it still showing me this message .what does it mean .


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

     class User extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            // Your own constructor code

            //$this->load->model('user_model');
    }

    public function index()
    {
        $this->load->view('user/signup');
    }
    public function registration()
    {

        if($this->input->post())
        {
            $fullname=$this->input->post('fullname');
            $fullnameSplit= explode(" ", $fullname);

            $reg_data = array(
                'membershipid'=>"Ls-".rand(),
                'role'=>1,
                'firstname'=>$fullnameSplit[0],
                'middlename'=>$fullnameSplit[1],
                'lastname'=>$fullnameSplit[2],
                'Company_name'=>$this->input->post('companyname'),
                'email'=>$this->input->post('email'),
                'password'=>sha1($this->input->post('password')),

            );
            print_r($reg_data);
            //$this->user_model->register_user($reg_data);
            //redirect('user/login');

            //echo '<pre>'; print_r($reg_data); die;


        }else 
        {

            //$data['current_page'] = 'Register';
            //$this->load->view('registration',$data);
        }
    }


}

This error is showing .


A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: controllers/User.php

Line Number: 35

Backtrace:

File: /var/www/overtribe.com/public_html/listspread/application/controllers/User.php
Line: 35
Function: _error_handler

File: /var/www/overtribe.com/public_html/listspread/index.php
Line: 293
Function: require_once

1
is there space above your php tag in the user controller?if yes delete the unwanted space,also just comment the print_r($reg_data); - Sanjuktha
Don't depend on user input. Always make dummy proof code. Best way should be to have more input fields i.e. $f_name, $m_name, $l_name and in controller check those if not empty before using them. - Tpojka

1 Answers

0
votes

use this line

'lastname'=>$fullnameSplit[2],

as

'lastname'=>isset($fullnameSplit[2]) ? $fullnameSplit[2] : '',

The error undefined offet:2 means the array $fullnameSplit with key [2] does not exists or is not set. So you need to check condition isset() to avoid that message