controller
elseif ($param1 == 'add') {
$data['employee_deparatments'] = $this->department->get_departments($fldCompanyStringID);
if ($this->input->post('viewType') == 'add' && $this->input->post('submit')) {
$this->form_validation->set_rules('fldUserBranchName', 'Branch Name', 'required');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error_msg', validation_errors());
} else {
$this->employee->insert_employee($fldCompanyStringID);
}
}
}
Model
class M_employee extends CI_Model {
function get_employee ($fldCompanyStringID){
}
}
You created a model with file name M_employee but in controller you called employee.
First load the model in controller :
$this->load->model('M_employee');
Next call the function by this type:
$this->M_employee->insert_employee($fldCompanyStringID);
you can also add namespace for the model ,ilke
First load the model in controller :
$this->load->model('M_employee','employee');
Next call the function by this type:
$this->employee->insert_employee($fldCompanyStringID);
no need to edit the controller function
Related
I'm use Grocery CRUD. I create custom button using add_action.
The button for change data to 0. So, after click the button database update column to 0.
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Attendance extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_builder');
}
public function attendance($visitor_id)
{
$attendance_status = array(
'attendance_status' => 0
);
$update = $this->Attendace_model->update_attendance_status($visitor_id,$attendance_status);
if($update)
{
$this->load->view('Attendance');
}
else
{
alert("error");
}
}
}
Model :
<?php
class Attendance_model extends MY_Model {
function __construct()
{
parent::__construct();
}
function update_attendance_status($visitor_id,$attendance_status)
{
$this->db->where('id', $visitor_id);
$this->db->update('invitation_codes', $attendance_status);
}
}
What code to use in function for update the data to 0/
Controller:
public function attendance($visitor_id)
{
$attendance_status = array(
'attendance_status' => 0
);
$update = $this->YOUR_MODEL_NAME- >update_attendance_status($visitor_id,$attendance_status);
if($update)
{
return true;
}
else
{
return false;
}
}
Model :
public function update_attendance_status($visitor_id,$attendance_status)
{
$this->db->where('id', $visitor_id);
$this->db->update('invitation_codes', $attendance_status);
}
I've pretty much gone through a lot of link and solutions provided here and other locations, but I am just not able to solve the callback issue that I am facing. I'm using Codeigniter with HMVC the code is below.
The following code is from My_Form_validation.php:
class MY_Form_validation extends CI_Form_validation {
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
Below if the Callback function :
public function _unique_email($str) {
// Check if user already exists
// Process only for current user
$id = $this->uri->segment(4);
$this->db->where('email', $this->input->post('email'));
!$id || $this->db->where('id !=', $id);
$user = $this->mdl_admin_users->get();
if (count($user)) {
$this->form_validation->set_message('_unique_email', 'User already exists. Please check %s.');
return FALSE;
}
return TRUE;
}
and the function :
public function user_edit($id = NULL) {
// Fetch a user or set a new one
if ($id) {
$data['user'] = $this->mdl_admin_users->get($id);
count($data['user']) || $data['errors'][] = 'User could not be found';
}
else {
$data['user'] = $this->mdl_admin_users->get_new();
}
// setup the form
$rules = $this->mdl_admin_users->rules_admin;
$id || $rules['password'] = '|required';
$this->form_validation->set_rules($rules);
//process the form
if ($this->form_validation->run($this) == TRUE) {
$data = $this->mdl_admin_users->array_from_post(array('firstname', 'lastname', 'email', 'password'));
$data['password'] = $this->mdl_admin_users->hash($data['password']);
$this->mdl_admin_users->save($data, $id);
redirect('admin/user');
}
// Load the view
$data['title'] = 'Edit Users';
$data['module'] = 'admin';
$data['header_file'] = 'header_admin';
$data['nav_file'] = 'nav_admin';
$data['view_file'] = 'edit_users';
$data['footer_file'] = 'footer_admin';
echo Modules::run('template/base_template', $data);
}
Would be a great help if someone could point me at the right direction to resolve the issue. Thanks in advance
Naveen
According to wiredesignz,
When using form validation with MX you will need to extend the CI_Form_validation class as shown below,
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly.
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}
This will remove HMVC related callback problem without any changes to your code.
First of you are missing in the rules
$rules['email'] = 'required|callback__uniqueemail';
Also call back function should be not like this callback__unique_email for some reason I found codeigniter call back not like extra gap this better callback__uniqueemail
If private do not work make public function removing underscore
public function uniqueemail() // no need $str
When make public do not for get to remove extra underscore from here callback_uniqueemail
Another thing with echo Modules run best to be loaded from view only.
In your controller replace echo Modules run with $this->load->view();
You need to add $this->form_validation->run($this) add $this to run after create library below.
And Create a new library
<?php
class MY_Form_validation extends CI_Form_validation {
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
Tutorial Best https://www.youtube.com/watch?v=8fy8E_C5_qQ
When I wrote the following code in controller folder, naming the file LoginController.php,
<?php
class LoginController extends CI_Controller
{
public function index()
{
$this->load->view('login');
}
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else{}
}
?>
the following parsing warning was signalled:
-Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in C:\xampp\htdocs\CodeIgniter\application\controllers\LoginController.php on line 14
You have written if clause outside the check function it should be like this
<?php
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else{}
}
?>
Your If Else doesnt include in any function... put it in function braces above this way
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == FALSE){
$this->load->view('login');
}
else{
}
}
you have defined if-else out side of the function, add it inside the function
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
}
}
I am new in CodeIgniter. I am getting a problem. My procedure seems logical but it would not work!
I have a controller: User
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index($slug = FALSE) {
$data['title'] = "User List";
if($slug === FALSE) {
$data['user'] = $this->user_model->get_user();
} else {
$data['user'] = $this->user_model->get_user($slug);
}
if (empty($data['user'])){
show_404();
}
$this->load->library('table');
$this->load->view('user_view', $data);
}
public function authen() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('phone', 'phone', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login_form');
} else {
if($this->user_model->do_login()===TRUE) {
$this->index();
}
$this->authen();
}
}
}
The "authen" method is not working properly with its last conditions. Page does not redirect to controller.
Can anybody help?
Thanks
You can use: redirect('user') it will redirect u to the controller user class
Instead of
$this->index();
try this
header('Location:http://myipaddress/mysite/user');
You are try to load the login page in case there is any authentication error.
For for this you will need to bring user to the login page again.
use redirect('user/login'); and store the error message in session flash.
or display validation errors..
you cannot call controller from a controller. as $this->index();
CodeIgniter 2.1.2
I have a class that contains two methods, for the purpose of this questions showone and view. The latter returns all items of a small database and can also perform a search. The other one is for permalinks like domain.com/showone/firstname-lastname
<?php
class Pages extends CI_Controller {
public function view($page)
{
//this includes a mysql search
}
public function showone($slug)
{
//abbreviated version:
$query = "SELECT * FROM mytable WHERE slug = '" . $slug . "'";
$result = $this->db->query($query);
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
?>
So if a user decides to directly enter a URL that doesn't return anything from the database, I would like to direct him to search results instead of showing a 404.
So how do I set up a function searchdatabase($query) to be used by both showone and view?
You can use your controller functions inside your controller:
public function view($page)
{
$this->showone($slug);
}
Define that function in your model, load your model and then call the model->method.
<?php
//Your Model would look something like this.
class Search_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
public function showone($slug)
{
//abbreviated version:
//Its best to use active record for building your queries
$this->db->where->('slug', $slug);
$result = $this->db->get('mytable');
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
And then in your controller you would do this:
<?php
//Your Controllerwould look something like this.
class Index extends CI_Controller {
public function __construct(){
parent::__construct();
//model will be loaded for each method.
//if you're going to use this model across several controllers
//its best to autoload it, set that in autoload.php under /app/config
$this->load->model('search_model');
}
public function index(){
$searchResults = $this->search_model->showone('slugone');
}
Update
I just realized you are wanting to show all results if no results were returned. In which case you would perform that logic in your model as well..
In your conditional statement you would do the following:
if ($result->num_rows() == 0)
{
return $this->showall();
}
else
{
return $this->view($slug);
}
}
your showall() and view() methods would return $result->result();