Not able to load model in Codeigniter - codeigniter

I have a model called register_model.php, which I have loaded in a function in my controller (register_controller.php). Model file is placed into model folder itself. Still, I get this error.
An Error Was Encountered
Unable to locate the model you have specified: register_model
register_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
// function index()
// {
//
// }
public function register() {
if ($this->session->userdata('logged_in'))
{
//user is already logged in
redirect('index.php');
}
else {
//init
//$data['country_list']=$this->config->item('um_country_list');
$data['username'] = '';
$data['firstname'] = '';
$data['lastname'] = '';
$data['email'] = '';
// $data['password'] = '';
//$data['userlevel'] = '';
//load rules
$rules = $this->config->item('um_register_rules');
//default msg
$data['msg'] = $this->lang->line('um_form_msg');
$this->load->model('register_model');
if (isset($_POST['submit'])) {
//the user has submitted the form
//get the user input
$data['username'] = $this->input->post('username');
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
//$data['userlevel'] = $this->input->post('userlevel');
$this->form_validation->set_rules($rules); //check with the rules
if ($this->form_validation->run() === FALSE) {
//validation failed
$data['msg'] = $this->lang->line('um_form_error');
$this->load->view('user_register_form', $data);
} else {
//validation passed
$dbdata = array(
'username' => $this->input->post('username'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
//'userlevel' => $this->input->post('userlevel')
);
$this->register_model->register_user($dbdata);
$data['msg']=$this->lang->line('um_form_activate');
//render the view
$this->load->view('um_msg', $data);
}
} else {
//render the view
$this->load->view('user_register_form', $data);
}
}
}
}
?>
register_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function register_user($dbdata) {
$this->db->insert('users', $dbdata);
}
}
?>

Probably you should include your model inside your controller, try to include after parent::__construct
This should look like so:
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
or if you using this model in most controllers you can include it using autoload, go to /config/autoload.php search for $autoload['model'] and add your model inside the suitable array

First of all load your model in your controller ..
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
This will work for you...

Related

Codeigniter Page Not Found - Migrating To Live Server

I was uploading my code based on CI to a live website and it keeps getting error 404.
The web structure is like this :
web
-Application
-Assets
-Cache
-..so on
index.php
Here's my Application/Config/Routes.php file:
$route['default_controller'] = 'H';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
This is my H controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class H extends CI_Controller {
function __construct(){
parent::__construct();
if(empty($this->session->userdata("userData")['id'])) redirect('/login');
}
public function index()
{
$data = array();
if(isset($_GET['guid'])){
$data['post'] = getListPost($_GET['guid']);
}else{
$data['post'] = getStickyPost();
}
$this->load->view('partials/header');
$this->load->view('home',$data);
$this->load->view('partials/sidebar-home');
$this->load->view('partials/footer');
}
public function materi()
{
$this->load->view('partials/header');
$this->load->view('management/post/lists');
$this->load->view('partials/footer');
}
}
And this is my Login Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index($slug="")
{
if(isset($_POST['username'])){
$cek = $this->checkLogin($_POST['username'], $_POST['password']);
if($cek){
$newdata = array("userData"=>array(
'name' => $cek[0]['nama'],
'id' => $cek[0]['id'],
'nip' => $cek[0]['nip'],
'jabatan' => $cek[0]['jabatan'],
'unit_kerja' => $cek[0]['unit_kerja'],
'foto' => $cek[0]['foto'],
'hak_akses' => ($cek[0]['hak_akses_knowledge_management']=="")?"pengguna":$cek[0]['hak_akses_knowledge_management'],
'unit_kerja_atasan' => $cek[0]['unit_kerja_atasan'],
'email' => $_POST['username'],
'logged_in' => TRUE));
$this->session->set_userdata($newdata);
redirect('/');
}else{
$this->session->set_flashdata('login_status','false');
redirect('/login');
}
}
// $this->load->view('partials/header');
// $this->load->view('partials/login');
// $this->load->view('partials/footer');
$this->load->view('partials/single_login');
}
public function logout(){
$this->session->sess_destroy();
redirect('/');
}
public function newlogin(){
$this->load->view('partials/single_login');
}
function checkLogin($email,$password){
$this->db->where("nip",$email);
$this->db->where("password",$password);
$query = $this->db->get("data_pegawai");
return $query->result_array();
}
}
When I change the routes to default it works, somehow when i return to 'H' it keeps getting error 404.
This is the structure of views folder :
management
infografis
post
ebook
partials
header.php
single_login.php
footer.php
sidebar-home.php
login.php
category
infografis.php
ebook.php
errors
home.php
post.php
welcome_message.php
Routes are expected to be lowecase, even if Controller file names and class names begin with an uppercase character.
So, H.php, class H extends CI_Controller are correctly capitalized. However, $route['default_controller'] should be assigned a lowercase value.
Try using
$route['default_controller'] = 'h'; instead of 'H' and you should be up and running

Unable to set validation rule for alphabetical characters in codeigniter

I am using Codeigniter 3, and I referred the various stackoverflow links having similar problem like mines. But I was not able to solve this issue. The callback function customAlpha is called from the file->MY_Form_validation.php, which is currently located in the libraries folder of my project. I changed the regular expression several times,but no use. Please help.
Controller->Login_c.php
<?php
class Login_c extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('encrypt');
$this->load->model('User/login_m');
$this->load->library('session');
$this->load->library('form_validation');
}
public function register_user()
{
$this->form_validation->set_rules(
'uname', 'Full Name',
'trim|xss_clean|required|min_length[4]|callback_customAlpha',
array(
'required' => 'Please provide %s.',
'customAlpha' => 'Only characters are allowed in Full Name field'
)
);
$this->form_validation->set_rules('upass', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('cpass', 'Password Confirmation', 'required|matches[upass]');
$this->form_validation->set_rules('uemail', 'Email', 'required|valid_email|is_unique[user.uemail]');
$this->form_validation->set_rules('umobile', 'Mobile Number', 'required|min_length[10]|max_length[10]');
if($this->input->post('oemail'))
{
$this->form_validation->set_rules('oemail', 'Email', 'required|valid_email|is_unique[user.oemail]');
$oemail = $this->input->post('oemail');
}
if ($this->form_validation->run() == FALSE)
{
$errors['err'] = validation_errors();
$this->load->view('User/signup.html',$errors);
}
else
{
//Code to store user entered data in database
}
}
}
?>
MY_Form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
public function __construct($rules = array())
{
parent::__construct($rules);
}
public function customAlpha($str)
{
//Validation for alphabetical characters and spaces
if ( !preg_match('/^(?:[A-Za-z]+(?:\s[A-Za-z]+|$)){1}$/', $str) )
{
return false;
}
else
{
return true;
}
}
}
?>
I finally was able to solve this issue on my own. Below is the solution that worked.
MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array())
{
parent::__construct($config);
}
public function customAlpha($str)
{
if ( !preg_match('/^([a-z]+(-| )?)+$/', $str) )
{
return false;
}
else
{
return true;
}
}
}
Login_c.php
class Login_c extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('encrypt');
$this->load->model('User/login_m');
$this->load->library('session');
$this->load->library('form_validation');
}
public function register_user()
{
$this->form_validation->set_rules(
'uname', 'Full Name',
'required|min_length[4]',
array(
'required' => 'Please provide %s.'
)
);
$this->form_validation->set_rules('upass', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('cpass', 'Password Confirmation', 'required|matches[upass]');
$this->form_validation->set_rules('uemail', 'Email', 'required|valid_email|is_unique[user.uemail]');
$this->form_validation->set_rules('umobile', 'Mobile Number', 'required|min_length[10]|max_length[10]');
$validation = $this->form_validation->customAlpha($this->input->post('uname'));
if($this->input->post('oemail'))
{
$this->form_validation->set_rules('oemail', 'Email', 'required|valid_email|is_unique[user.oemail]');
$oemail = $this->input->post('oemail');
}
else if ($this->form_validation->run()== FALSE)
{
$errors['err'] = validation_errors();
$this->load->view('User/signup.html',$errors);
}
else if($validation == false)
{
$errors['err'] = "Error in name field";
$this->load->view('User/signup.html',$errors);
}
else
{
//Code to store user entered data in database
}
}
}
?>

Codeigniter submit form not working, blank page

I'm attempting to submit a form with codeigniter and when i submit the form a get a blank page. Where did i go wrong?
View
echo "<form class='order_ctn_parent' method='POST' action='add/insert_orders'>";
//inputs
echo "</form>";
Controller
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
}
public function insert_orders()
{
$this->load->model('order_model', 'order');
$this->order->insert_orders();
redirect('view_orders', 'location');
}
}
Model
class Order_model extends CI_Model {
public function insert_orders()
{
$DB2 = $this->load->database('orders', TRUE);
$timber_array = $this->input->post("timber_choose");
$products_array = $this->input->post("product_choose");
$qty_array = $this->input->post("quantity");
$price_array = $this->input->post("price");
$qty_array = $this->input->post("quantity");
$loop = 0;
foreach($products_array as $product) {
$price = str_replace("£","",$price_array[$loop]);
$data = array(
'product_code' => "",
'timber_type' => $timber_array[$loop],
'product' => $product,
'quantity' => $qty_array[$loop],
'price' => $price
);
$DB2->insert('timber_order_products', $data);
$loop++;
}
}
}
Try something like this
?>//end of php
<form class='order_ctn_parent' method='POST' action='<?php echo base_url()?>add/insert_orders'>
</form>
and load model in __construct(), like this
public function __construct()
{
parent::__construct();
$this->load->model('order_model', 'order');
}
and no need of check $DB2 = $this->load->database('orders', TRUE);
and Codeigniter insert should be Like This
Please check your url after submitting . i am sure your form action is not working . give proper controller and its function url into Form action . example is here .
<?php echo site_url('add/insert_orders);?>
where add will be your controller and insert_orders will be function of add controller.

Codeigniter - Passing data from MY_Controller to Main_controller to view

This is the complete code (I want to select the view through a variable set on the MY_Controller):
I would like to pass that variable but it doesn't "reach" the view it gives me
$ses_group = "not_logged_in" with a test echo and I'm not setting anything on the MY_Controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
$this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
$this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
$this->special_data['ses_group'] = 'admin';
}else{
// $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}
The Main_Controller:
function index(
$data = array(
'ses_group' => $this->special_data
);
$this->load->view('auth/descricao_anuncio', $data);
)
The view:
<?php if($ses_group="not_logged_in"){ ?>
<li>Login</li>
<?php }elseif($ses_group="users"){ ?>
<li>Your Area</li>
<li>Logout</li>
<?php }elseif($ses_group="empresas"){ ?>
<li>Empresa</li>
<li>Logout</li>
<?php }?>
echo $ses_group;
Thanks againg!
Controller
<?php
class MY_Controller extends CI_Controller {
protected $special_data = array();
function MY_Controller () {
parent::Controller();
}
function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}
}
You can send your value from controller to views by using $this-> in your MY_Controller
Controller
class Main_controller extends MY_Controller {
function __construct() {
parent::MY_Controller();
}
$this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
$this->load->view('view_x', $this->data);
}
Views
<?php echo $group; ?>
Is this what you need
controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}
class Main_controller extends MY_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$data = array(
'group' => $this->special_data('a')
);
$this->load->view('view_x', $data);
}
}
view
<?php echo $group; ?>

How to pass variables in codeigniter 2

I have following
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller {
var $name = 'test';
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->name;
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
}
My question is how to I pass the $this->name = 'Andy'; to you() ??
Since it is being set in a different method of the controller, which equates to another request in your code, you will need to store it in a session variable to have it persist across page requests.
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->session->set_userdata('name', $this->name);
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->session->userdata('name');
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
If its a value that is part of the class you can put it in the constructor
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
// will be available to any method in the class
$this->name = 'andy';
}

Resources