Code igniter controller view mysql - codeigniter

I have one controller abc_con with view abc_ view.
There is second controller cart_con with cart_view.
And I have third controller xyz_con with view xyz_view.php
The flow of my website is like when I click on abc_view then values go to abc_con and in abc_con there is function cartload(). This function load cart_view view. When cart_view submit all value go to xyz_con controller. In xyz_con there is function loadxyz(). This function load the xyz_view.
In abc_view there is an input with name pin code. This input value I can easy fetch in abc_con
I just wanted to transfer this $data t
Directly to xyz_view without storing in database.

You have to extend those controllers together
like this
1).in oneController.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/controllers/twoController.php';
class oneController extends BaseController
{
$this->abc();
}
2).in twoController.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/controllers/threeController.php';//parent class is threeController now you can access to function abc using inheriting like this
class twoController extends threeController
{
$this->abc();
}
}
3).in threeController.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class threeController extends CI_Controller
{
function abc()
{
your code
}
}//end of 3rd controller

Related

How to use session variable in every method by only declaring in class

I want to set my session variable in every function of the class. If the session is not set in any of the function , then it should be redirected to home page
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
public function masteradmin()
{
if($this->session->userdata('name')){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
else
{
redirect('/', 'refresh');
}
}
public function reseller()
{
}
}
Use constructor for that. In OOP constructor is the first method of any class which is called. So, what you can do is, create a constructor and check for the session variable. If the variable exists, do nothing, it will automatically call the function. If the session variable does not exist, redirect the user to the home page.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
function __construct(){
parent::__construct();
if(!$this->session->userdata('name')){
redirect('/home');
}
}
public function masteradmin(){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
public function reseller(){
}
}
?>
create super class in core folder like MY_Controller.php
and there you can define session and then call that super class in every controller extends. for example:
class App_config extends MY_Controller {
}

How to redirect old php routes in codeigniter

I have old urls like http://example.com/products.php?s_productid=231 and now in my proyect whit url would be http://example.com/products/ver/231
How can I make a rule in routes.php to redirect?
Thanks
you don't need to do any change in routes.php, all you need is to create a Controller "Product", with one Method "ver", which receives 231 (it's id I guess) as a parameter, like this:
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
// do your things
}
I hope it helps.
You may need to set the routes in config/routes.php
$route['product/ver/(:num)'] = 'product/ver/$1';
http://example.com/index.php/products/ver/231
Then the controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
/*
echo $id;
*/
}

CodeIgniter + PHPExcel: What is best way to call this library to CodeIgniter?

I have PHPExcel, and I want to add it to CodeIgniter.
Where is the best to copy the files from all CodeIgniter folders, and how do I call it in my controllers/models for regular usage?
EDIT:
It's a whole folder that I need to move, and call the main file: PHPExcel\IOFactory.php
You may add PHPExcel to library in Codeigniter, E.g:
application > libraries > PHPExcel.php
Let say in PHPExcel.php library file:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class PHPExcel
{
public function __construct()
{
//
}
public function some_function()
{
return 'some_function';
}
}
To call PHPExcel library, in your Controller. Let say I named it as
My_controller.php:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class My_controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//Call PHPExcel class
$this->load->library('PHPExcel');
}
public function index()
{
echo $this->PHPExcel->some_function();
}
}
I followed this tutorial: https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
Got my answer !

Redirect loop while extend controller Codeigniter

I have a problem in login application in codeigniter , the problem is like this:
I create MY_Controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then I extended it on my library that i created, this is my "Petugas_Controller" library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('petugas_m');
} // end contructor.
public function check(){
if ($this->uri->uri_string() !== 'index' && ! $this->petugas_m->is_logged_in() == TRUE){
redirect('admin/petugas/index');
}
}
}
I'm using that library to be used in my "Petugas" controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('petugas_controller');
$CI = &get_instance();
$CI->check();
}
public function index(){
echo "You have to login";
}
public function dashboard(){
echo "Welcome to dashboard";
}
}
In "Petugas_Controller" library, I loaded model that is called "Petugas_m", here is the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_m extends CI_Model {
public function __construct(){
parent::__construct();
}
public function is_logged_in(){
return FALSE;
}
}
That's all what i was trying, and my problem is I want to check if username is logged or not, by doing $this->check() in "Petugas_library" that loads "Petugas_m" model, i could see an dashboard echo if the "check" method in "Petugas_m" is set to TRUE but it's always redirect me a loop if it FALSE.
I was searching for tutorials and still have this crazy problem :( ,
Thank in advance, and sorry for my bad english.
As long as your petugas_controller library is located within the libraries folder, you can access the check method via
$this->petugas_controller->check();
Also try changing the if statement in your check to something more like this
if ($this->uri->uri_string() !== 'index' && $this->petugas_m->is_logged_in() !== TRUE){
lol, I've just realized my mistake here, I've just tried to make another controller, it's called login, then in my "petugas_controller" library I redirect the user if not login to this "login" controller, it's totally worked ...
Youy guys are inspiring me so much,Thanks :)

CodeIgniter Creating Core Base Classes not working like User Guide

I want to extend CI_Controller as a base class for my other classes according do my own need.
I read the user guid. Acted as what it told me.
I created application core:
./application/core/MY_ControllerPermission.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_ControllerPermission extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
And this is my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(class_exists('MY_ControllerPermission'))
echo 'clase does exist';
else
echo 'clase does not exist';
class Users extends MY_ControllerPermission
{
....
In my config file:
$config['subclass_prefix'] = 'MY_';
It just show blank page and "clase does not exist"
So where is the problem?
UPDATE
If I add this line:
include_once(APPPATH . 'core/MY_ControllerPermission.php');
before my controller, it would work. Doesn't CodeIgniter load core PHP files automatically?
Rename MY_ControllerPermission.php to MY_Controller.php and you should be good to go.
You can leave everything else the same, you don't even need to use the name MY_Controller for your class if you don't want to.

Resources