简体   繁体   中英

Registration in codeigniter

I am trying to register in the following code. But when i register i got the error message undefined variable base.

View:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="<?php echo $base?>/<?php echo $css?>/style.css">
<link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/bootstrap.min.css">

<script src="<?php echo $base."/".$js?>/jquery.min.js"></script>
<script src="<?php echo $base."/".$js?>/jquery.js"></script>
<script src="<?php echo $base."/".$js?>/bootstrap.min.js"></script>
<title>Registration</title>
</head>

Config.php

$config['base_url'] = 'http://localhost/ASOFT/Projects/CG_Pack';
$config['site_url'] = 'http://localhost/ASOFT/Projects/CG_Pack/index.php';
$config['css'] = 'assets/css';
$config['js'] = 'assets/js';
$config['image'] = 'assets/image';

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
    var $data;
    function __construct(){
        parent::__construct(); 
        $this->load->library('form_validation');
        $this->load->helper('url');
    $this->load->model('user_model');
        $this->data = array(
        'site' => $this->config->item('site_url'),
             'base' => $this->config->item('base_url'),
             'css' => $this->config->item('css'),
            'js' => $this->config->item('js'),
            'image'=>$this->config->item('image')
        );

    }

public function index()
    {
        $data=$this->data;  
        $this->load->view('header',$data);
        }

public function register()
{
$this->load->view('header');
}
    public function do_register()
    {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('uname', 'Username', 'required|min_length[4]|max_length[15]');

    $this->form_validation->set_rules('pass', 'Password', 'required|min_length[4]');
    $this->form_validation->set_rules('titleimg','Title','required|min_length[4]');
    $this->form_validation->set_rules('descrip', 'Description', 'required|min_length[4]');


    if($this->form_validation->run() == FALSE)
    {


            /*$this->load->view('header');  

    }
    else
    {


 $path = $_FILES['image']['name'];

 $imgext=strtolower(strrchr($path,'.'));

    $imgname= $this->generateRandomString().$imgext;

   if($path!='')
   {

   $im= $this->config->item('base_url').'/uploads'.'/'.$imgname;
   $x=$this->do_upload($imgname);

  $data['img']=$im;


    $this->user_model->register_user($data);
   $this->load->view('header',$data);

}
}
}

 function generateRandomString()
  {
     $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
     $randomString = '';
     for ($i = 0; $i < 8; $i++) {
         $randomString .= $characters[rand(0, strlen($characters) - 1)];
     }
     return $randomString;
  } 

        function do_upload($img)
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024 ';
        $config['file_name'] = $img;    
        $this->load->library('upload',$config);

        if ( ! $this->upload->do_upload('image'))

        {
            $error = array('error' => $this->upload->display_errors());
            /*$data['error']=$error;
            return $data;*/
            return $error;

        }
        else
        {

            $data = array('upload_data' => $this->upload->data());
             return $data;
        }
        return;
    } 
}

When i register i got the error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: base_url

Filename: views/header.php

Line Number: 6

Backtrace:

File: C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\application\views\header.php
Line: 6
Function: _error_handler

File: C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\application\controllers\home.php
Line: 22
Function: view

File: C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\index.php
Line: 293
Function: include_once

/assets/css/style.css">

You are not passing the $data , when you load the header view in the register method.

Try it like this:

public function register()
{
   $this->load->view('header', $this->data);
}

That should get rid of the error.

Instead of using base_url by config variable, you can call base_url() method so you don't have to add it in each and every controller.

To do that you must first have the URL Helper loaded. This can be done either in application/config/autoload.php

$autoload['helper'] = array('url');

Or, manually:

$this->load->helper('url');

Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:

echo base_url();

Either use base_url() function in the view & this is the correct way to use it or make sure you are passing $base variable to view. I don't think you are passing any kind of data to view in the register method of your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM