Codeigniter updating records in db table - codeigniter

I want to change a specific record in the database using codeigniter. The url should be like this mysite.com/users/edit/10.
Here the user having id=10 is being edited
users is the controller name and edit is a method.
Usually I do in this way
//code of the rest of controller
.....
function edit(){
$uid =$_REQUEST['uid'];
//database update code
}
...
Where a form is being posted deliberately to change the record

You're not clear at all on what you want, I just can give you some pointers.
In CI, you don't need (don't have to) use superglobals to access url parameters. It has a native system to manage uri segments, which became automatically accessible without the need to call them; they're available as arguments of the method you're accessing.
So, in a url like yours, mysite.com/users/edit/10, you'll have
Controller:
class Users extends CI_Controller {
public function edit($uid)
{
// $uid is automatically passed to this method and is already available
// here you do your operations
//for. ex.
$this->load->model('user_model');
$this->user_model->update_user($uid);
}
}
Model:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function update_user($id)
{
$this->db->where('id',$id);
$fields = array('field1' => 'value1','field2' => 'value2'...);
$this->db->update('users',$fields);
}
}
If you provide further information I could expand my answer.

Related

Get a route prefix variable in the abstract base controller

So we are building a CMS for a school project and we need to make it dynamic in the way that there are multiple organisations. In the prefix of a group I add a $gid variable that represents the ID of this organisation.
Route::middleware('auth')->prefix('{$gid}/admin')->group(function()
So I also created an abstract base controller that every controller in the admin section will inherit.
abstract class BaseAdminController extends Controller
What I want now is the $gid variable from the route and parse it to the base controller in the constructor.
protected $gid;
public function __construct() {
$this->gid = .....;
}
Now I can access this ID everywhere instead of having to parse it to each individual controller and function.
You don't need to "parse" it in every action but you should keep it because otherwise you wouldn't be able to access the routes' other parameters because the first one is missing and Laravel wouldn't be able to assign the next one's correctly. And you don't need the dollar sign at the start of a parameter.
// routes/web.php
// ↓ No dollar sign here
Route::middleware('auth')->prefix('{group}/admin')->group(function() {
// Routes
});
// In any of your Controllers
use Illuminate\Http\Request:
class SomeController extends BaseController
{
public function index(Request $request, Group $group) // you might add more parameters here
{
// Access your $group model
}
}
If you don't need the group model, don't define the type of the parameter and the related data will not be loaded:
public function index(Request $request, $group) // Now $group is just a number

recall the construct in codigniter

How to recall the construct as it contains all the required data for the page?
class Abc extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('xyz_m');
$this->data['info'] = $this->xyz_m->get(); //get data
}
public function 123()
{
/*view page code*/
}
public function 456()
{
/*insert code here*/
$this->123(); // redirect, need to load 123() with updated data from construct.
}
}
So, how do you make the __construct initiate again so you get a new updated results from database?
You should name your methods with letter first i.e. there is convention for method names uses descriptive words getProducts() or get_books or you will get PHP error for using numbers as method names. So in your case method names should be like a123() or b_456().
Second thing, regarding your need in question, since you assign data from DB using model to array $this->data, you would use it like:
class Abc extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('xyz_m');
$this->data['info'] = $this->xyz_m->get(); //get data
}
public function a123()
{
$this->load->view('a123_view', $this->data);//loading file APPPATH . 'a123_view.php' and passing created array to it
}
public function b_456()
{
/*insert code here*/
$this->a123(); // redirect, need to load 123() with updated data from construct.
}
}
In your APPPATH . 'a123_view.php':
<?php var_dump($info);//here you would call key of array you passed from controller as variable ?>
Check basics in CodeIgniter documentations. All this is described in General Topics section.

Session keep track of whose logged in and only show to members

I'm not really sure what to do when it comes to dealing with session.
After I log in and set a session data.
How do I keep private pages to only show to those who are logged in.
Do I have to run that kind of validation to all my views?
<?php
if($this->session->userdata('is_loggedin')!=1)
{
redirect('KGindex/index','refresh');
}
?>
I'm not even sure if that is correct, right now it messed up my code. functions don't work anymore.
Where do I run session validations?
You want to be doing this check in your controller rather than in the view. For example
class Account extends CI_Controller {
public function index()
{
if($this->session->userdata('is_loggedin')!=1)
{
redirect('KGindex/index','refresh');
}
}
}
If you know that every function in your controller requires the user to be logged in then you could include the check in the __construct() function as this is called whenever the class is accessed. Therefore you would only need the put the code in one place.
class Account extends CI_Controller {
public function __construct()
{
if($this->session->userdata('is_loggedin')!=1)
{
redirect('KGindex/index','refresh');
}
}
public function index()
{
//__construct() has already been called
}
}

White Screen When posting in CodeIgniter

Codeigniter gives a white screen every time a form is posted:
Here is the controller logic [controllers/account.php]:
class Account extends CI_Controller
{
public function create()
{
if($this->input->post(NULL, TRUE)){
$params = $this->input->post();
//add validation layer
$accountOptions = array($params are used here)
$this->load->model('account/account', 'account');
$this->account->initialize($accountOptions);
$this->account->save();
}
$header['title'] = "Create Free Account";
$this->load->view('front_end/header', $header);
$this->load->view('main_content');
$content['account_form'] = $this->load->view('forms/account_form', NULL, TRUE);
$this->load->view('account/create', $content);
$footer['extraJs'] = "account";
$this->load->view('front_end/footer', $footer);
}
}
Here is the Account Model logic [models/account/account.php]:
class Account extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function initialize($options)
{
//initialize
}
}
The view first loads fine then after filling the form and clicking submit, just white page.
I tried to add __construct to the controller and load account/account from there, the form does not even load. Any ideas?
I just found the problem:
- The Model account has duplicated definition and the error_reporting was off!
You shouldn't have two classes with the same name Account (the controller and model). Check your server and/or Codeigniter log it should show up there.
I advise you to call your controller class Account and your model class M_Account. You can then rename the model to account whenever you load it, just like you did:
$this->load->model('account/m_account', 'account');
public function __construct()
{
parent::__construct();
$this->load->model("account/account");
}
you load model,library and helper files in construct dont load to inside a function

base controller and apply it to all existing controller

I need to create codeigniter base controller to check allowed ip address in database by mobel function if the ip is exists then user should go to home page but if the ip address is not exists and show 404 page in codeigniter, i can't find core folder in application folder
First, you need to extend a core class, call it MY_Controller.php
Save that file in: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('ip_table_model');
$this->load->library('input');
// assuming there's a function called "check_ip($ip_address)" in ip_table_model
if (!$this->ip_table_model->check_ip($this->input->ip_address()) {
redirect('error_404');
}
}
}
Now, we're assuming you have a model called ip_table_model which connects to database with list of IP addresses, and there's a function called check_ip which will validate whether user has access or not. This is relatively simple, and I won't show any examples on this.
The redirect('error_404'); page does not yet exist, you need to create a controller which shows your 404 page.
Now, for any other controllers in your project, instead of extends CI_Controller, make them extend MY_Controller instead.
Here's an example:
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
Explanation: We're extending CI_Controller to create our own core controller, called MY_Controller. Inside, we're checking if user has access or not through the constructor, which will be called in every other controller in the project.
References:
http://codeigniter.com/user_guide/general/core_classes.html
http://codeigniter.com/user_guide/libraries/input.html
Answer is here (section Extending Core Class).
1.7.2 has a different structure to 2.0.*, therefore there is no core folder in application
In Core Create a new Class .
Name MY_Controller.php
class MY_Controller extends CI_Controller {
// Write your functions here which you wanna use throughout the website
public function abc (){
echo "Helllo";
}
}
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function your_custom_fuctions()
{
$this->abc(); //echo Hello...
//Anything you want to do
}
}
function admin_view($view_name = "", $header_info = NULL, $sidebar_info=NULL,$page_info = NULL, $footer_info = NULL, $data_info = ""){
$this->load->view('Admin/includes/header', $header_info);
$this->load->view('Admin/includes/Left_sidebar', $sidebar_info);
$this->load->view($view_name, $page_info);
$this->load->view('common/footer', $footer_info);
}

Resources