I have set the $routes['404_override'] = 'city/switch_site'
now in my city controller
class City extends CI_Controller {
function __construct() {
parent::__construct();
}
function switch_site( ) {
$this->load->helper('url'); // load the helper first
$city = $this->uri->segment(1);
$segment_cnt = 1;
$valid_url = '';
switch( $city ) {
case 'pune':
$segments = $this->uri->segment_array(2);
foreach($segments as $value) {
if($segment_cnt > 1) {
$valid_url .= $this->uri->slash_segment($segment_cnt);
}
$segment_cnt++;
}
$this->config->set_item('cityid',1);
$this->config->set_item('cityname','pune');
echo APPPATH.'controllers/'.$valid_url;
include_once(APPPATH.'controllers/'.$valid_url);
break;
case 'mumbai':
$segments = $this->uri->segment_array(2);
foreach($segments as $value) {
if($segment_cnt > 1) {
$valid_url .= $this->uri->slash_segment($segment_cnt);
}
$segment_cnt++;
}
$this->config->set_item('cityid',2);
$this->config->set_item('cityname','mumbai');
include_once(APPPATH.'controllers/'.$valid_url);
break;
default:
}
}
}
how do i now pass the correct url to codeigniter router
I think you are going about this all wrong. Inside your switch instead of including other controllers which will not work use the redirect() to take them where they should go.
Related
public function index() {
if ($this->session->userdata('admin_login') == 1)
redirect(base_url() . 'index.php?admin/admin_dashboard', 'refresh');
if ($this->session->userdata('teacher_login') == 1)
redirect(base_url() . 'index.php?teacher/teacher_dashboard', 'refresh');
if ($this->session->userdata('student_login') == 1)
redirect(base_url() . 'index.php?student/student_dashboard', 'refresh');
if ($this->session->userdata('parent_login') == 1)
redirect(base_url() . 'index.php?parents/parents_dashboard', 'refresh');
$this->load->view('backend/login');
}
function ajax_login() {
$response = array();
$email = $_POST["email"];
$password = sha1($_POST["password"]);
$response['submitted_data'] = $_POST;
$login_status = $this->validate_login($email, $password);
$response['login_status'] = $login_status;
if ($login_status == 'success') {
$response['redirect_url'] = '';
}
echo json_encode($response);
}
I want to pass index() through $response['redirect_url'] = ''; how to pass? i already tried by creating routes but not working.
index() is a function, which according to your code either redirects the user or shows a page. Neither are good for an ajax response as the redirection or view will only occur or render in the ajax request not the users viewport.
I assume you want to add the logic in your index function without repeating yourself too much. Simple solution is to make a function for redirect.
Create a helper and put this function in it:
function group_redirect() {
$ci = &get_instance();
$ci->load->helper('url');
if ($ci->session->userdata('admin_login') == 1)
return base_url() . 'index.php?admin/admin_dashboard';
if ($ci->session->userdata('teacher_login') == 1)
return base_url() . 'index.php?teacher/teacher_dashboard';
if ($ci->session->userdata('student_login') == 1)
return base_url() . 'index.php?student/student_dashboard';
if ($ci->session->userdata('parent_login') == 1)
return base_url() . 'index.php?parents/parents_dashboard';
return false;
}
Then the rest is easy:
public function __construct() {
$this->load->helper('name_of_helper_with_function');
}
public function index() {
$redir = group_redirect();
if ($redir) {
redirect($redir);
}
$this->load->view('backend/login');
}
function ajax_login() {
$response = array();
$email = $_POST["email"];
$password = sha1($_POST["password"]);
$response['submitted_data'] = $_POST;
$login_status = $this->validate_login($email, $password);
$response['login_status'] = $login_status;
if ($login_status == 'success') {
$response['redirect_url'] = $redir;
}
echo json_encode($response);
}
It create the session but does not go to index2 and index3 always redirect with else and go to index method but i want to go index2 and index3 to handle other panels also.
Session is created successfully for all just comming else condition all the time.
My form data and array is also showing when i using the print_r for my code to view if the data is comming or not.
Problem is it is showing no any error just redirect with file of index method.
My Controller
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Main_Model');
$this->load->helper('url');
$this->load->library('session');
$method = $this->router->fetch_method();
$methods = array('index','index2','index3');
if(in_array($method,$methods))
{
if(!$this->session->has_userdata('signup_email'))
{
redirect(base_url('Main/login'));
}
}
}
public function index()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('BKO/index');
}
}
public function index2()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Admin/index');
}
}
public function index3()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Owner/index');
}
}
public function login()
{
//$data['select'] = $this->Main_Model->get_select();
$this->load->view('login');
}
public function login_process()
{
//$roll = $this->input->post('select');
echo $email = $this->input->post('email');
echo $pass = $this->input->post('upass');
$query = $this->Main_Model->login_process($email,$pass);
if($query == TRUE)
{
$this->session->set_userdata('signup_email');
$session = array(
'signup_email' => $email
);
$this->session->set_userdata($session);
redirect(base_url('Main/check_login'));
}
else
{
$this->session->set_flashdata('error','Invalid Email or Password');
redirect(base_url('Main/login'));
}
}
public function check_login()
{
if($this->session->userdata() == 'admin#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index2'));
}
elseif($this->session->userdata() == 'owner#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index3'));
}
else
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index'));
}
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}
My Model
public function login_process($email,$pass)
{
//$this->db->select('*');
//$this->db->where('roll_id',$roll);
$this->db->where('signup_email',$email);
$this->db->where('signup_password',$pass);
$query = $this->db->get('signup');
if($query->num_rows() > 0)
{
$this->session->set_flashdata('signup_email');
return true;
}
else
{
return false;
}
}
You missed the parameter here
if($this->session->userdata() == 'admin#gmail.com')
instead it should be
if($this->session->userdata('signup_email') == 'admin#gmail.com')
Having real big problems with CodeIgniter sessions. I can't get any userdata, really unusual thing, who can help me? I don't know how to realize it, I'm not getting the session.
Here is code:
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin_model');
}
public function do_login($username,$password)
{
$right_login = $this->admin_model->get_username($username);
$right_password = $this->admin_model->get_password($password);
if( ! empty($right_login) && ! empty($right_password))
{
$session = array();
$session['admin_logined'] = 'yes';
$session['user_ip'] = $_SERVER['REMOTE_ADDR'];
$this->session->set_userdata($session);
redirect('admin/main');
}
else
{
redirect('admin');
}
}
public function do_logout()
{
$session = array();
$session['admin_logined'] = '';
$session['user_ip'] = '';
$this->session->unset_userdata($session);
redirect('admin');
}
public function check_admin()
{
if(($this->session->userdata('admin_logined') === "yes"))
{
return TRUE;
}
else
{
redirect('admin');
}
}
}
To get session working in codeigniter you have to loaded session library to your controller constructor .
$this->load->library('session'); for more detail Codeigniter session.
This is my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order extends CI_Controller {
public function index($id=-1) {
$this->load->model('order');
}
}
When i try to open the corresponding url i get Error 500. The strange thing is that i load the model the same way on another controller without a problem.
Here goes the route and the model just in case:
Route:
$route['order/(:num)'] = "order/index/$1";
Model:
<?php
class Order extends CI_Model {
var $fullname = '';
var $email = '';
var $address = '';
var $phone = '';
var $notes = '';
var $facebook = '';
//var $canvases = '';
var $admin_notes = '';
var $status = '';
var $id = '';
var $date = '';
var $price = '';
//var $emailStatus_recivedOrder = '';
//var $emailStatus_sendedOrder = '';
//var $emailStatus_askFeedback = '';
function __construct() {
// Call the Model constructor
parent::__construct();
}
function get($search_query='', $per_page=5, $skip=0) {
if($search_query != '') {
$this->db->or_where('id', $search_query);
$this->db->or_where('email', $search_query);
$this->db->or_where('fullname', $search_query);
$this->db->or_where('phone', $search_query);
}
$query = $this->db->get('entries', $per_page, $skip);
return $query->result();
}
function count_all($search_query='') {
if($search_query != '') {
$this->db->or_where('id', $search_query);
$this->db->or_where('email', $search_query);
$this->db->or_where('fullname', $search_query);
$this->db->or_where('phone', $search_query);
}
$this->db->from('entries');
return $this->db->count_all_results();
}
function get_by_id($id) {
return $this->db->get_where('entries', array('id' => $id), 1);
}
function get_active_orders_count() {
$this->db->where('status', '1');
$this->db->from('entries');
return $this->db->count_all_results();
}
function insert_entry() {
$this->fullname = $this->input->post('fullname');
$this->email = $this->input->post('email');
$this->address = $this->input->post('address');
$this->phone = $this->input->post('phone');
$this->facebook = $this->input->post('facebook');
$this->notes = $this->input->post('notes');
$this->admin_notes = $this->input->post('admin_notes');
$this->status = $this->input->post('status');
$this->date = date('Y-m-d H:i:s');
$this->db->insert('entries', $this);
}
function update_entry() {
$this->admin_notes = $this->input->post('admin_notes');
$this->db->update('entries', $this, array('id' => $this->input->post('admin_notes')));
}
}
The error is:
A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: core/Loader.php
Line Number: 346
You can't name your controller and model the same.
give them a different name and the problem should be fixed. So something like
Controller
class Order extends CI_Controller{ ... }
Model
class Order_model extends CI_Model{ ... }
In Objected oriented programming no two class can have the same name. As of codeigniter Controllers, Models are all classes. So, it is good practice to name your controller as ABC_Controller and model as ABC_Model to avoid class name conflict.
Controller
class ABC_Controller extends CI_Controller { .. }
Model
class ABC_Model extends CI_Model { .. }
I'm trying to match up suppliers from a postcode search:
Model code:
function get_suppliers(){
$this->db->from('suppliers');
$this->db->where('postcode', $data);
$this->db->select('name,type,site,contact,number');
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
Controller code:
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('postcode','Postcode', 'required|numeric|exact_length[4]');
$data = array(
'postcode' => $this->input->post('postcode')
);
if($this->form_validation->run() == FALSE)
{
## reload page ##
$this->load->view('welcome_message');
}
else
{
$this->load->model('site_model');
$this->site_model->add_record($data);
echo("postcode entered");
$data['rows'] = $this->site_model->get_suppliers($data);
print_r($data);
}
}
Obviously ignore the printers and echo thats just me bring to see whats going on I'm pretty sure i need to just change the $data in model to something just not sure what(tried heaps of things)
Model:
function get_suppliers($postcode = '*') // <-- Capture the postcode
{
$this->db->from('suppliers');
$this->db->where('postcode', $postcode); // <-- pass it in here
$this->db->select('name,type,site,contact,number');
$q = $this->db->get();
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
Controller:
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('postcode','Postcode', 'required|numeric|exact_length[4]');
$data = array(
'postcode' => $this->input->post('postcode')
);
if($this->form_validation->run() == FALSE)
{
## reload page ##
$this->load->view('welcome_message');
}
else
{
$this->load->model('site_model');
$this->site_model->add_record($data);
$data['rows'] = $this->site_model->get_suppliers( $data['postcode'] ); // <-- pass the postcode
echo("postcode entered: " . $data['postcode'] . "<pre>");
print_r($data);
}
}
In your model:
function get_suppliers($data = ''){
i.e.: You haven't included $data as the argument