Basic Codeigniter codes

PHOTO EMBED

Mon Feb 21 2022 07:11:41 GMT+0000 (Coordinated Universal Time)

Saved by @patdevwork

$config['base_url'] ='http://feedback.form/'; // in order for redirect work ok.. set the base url based on the virtual host.
$config['index_page'] = ''; // I removed index php here so it will hide on the browser


$route['result'] = 'main/result'; // The left side is the one on the browser...
<a href="/">Return</a>  // routing on page sample
<form action="/result" method="post">

empty($form_data) ? redirect('/users') : $this->load->view('users/create');// In controller when form is empty redirect to a new place otherwise proceed
  
$this->input->post('username', TRUE); // returns the username and cleanse the data
$this->input->post(); // returns all POST items without XSS filter
$this->input->post(NULL, TRUE); // returns all POST items with XSS filter 

more example:
$this->input->get(); //$_GET 
$this->input->cookie(); //$_COOKIE 
$this->input->server(); //$_SERVER

$this->session->set_userdata($newdata); adds data to session	
$this->session->set_userdata('some_key', 'some_value'); updata data in session
$this->session->unset_userdata($array_items); deletes data in session
$counter = $this->session->userdata('times_visited') + 1; // counter in session
$this->session->set_userdata('times_visited', $counter);

if($this->session->has_userdata('times_claimed'))
{
    $winner = $this->session->times_claimed + 1;
    $this->session->set_userdata('times_claimed', $winner); // another version if it starts with 0
}
else
{
    $this->session->set_userdata('times_claimed', 0);  
}
To avoid this issue, always change the SESSION data and THEN echo or var_dump the data.
$old_counter = $this->session->userdata('counter');
$this->session->set_userdata('counter', rand(20,50));
echo "session counter was" . $old_counter;
echo "changed session counter to be" . $this->session->userdata('counter');

$this->session->set_flashdata('key', 'value');
$this->session->flashdata('$array');
$this->session->keep_flashdata('item');
// To style flashdata:
$this->session->set_flashdata('errors', validation_errors('<div class="errors">','</div>'));

Note: Flash variables are prefixed with "flash_" so avoid this prefix in your own session names to avoid conflict.

***************************Model *************************
 $this->db->query() - this literally lets us specify the full query string.
result_array() - this method runs our query and returns all results in a multi-dimensional array.
row_array() - this method runs our query and returns first row only in a simple array.

$this->output->enable_profiler(TRUE) // for database controller

Also, before we really use a specific model class, of course, we must load it first.
Ex: $this->load->model('Manufacturer'). 														Then, we can now call a method in that model in this format:									 $this->Model_name->function_name(parameter);.

Other Important Information
MODELS and POST/SESSION - Models should not directly access POST or SESSION data although the controller can pass POST or SESSION data for the models to access. Again, the model's job is not to manipulate POST or SESSION (that's the controller's job) but merely to do the job necessary for updating the database or for retrieving proper information from the database.
The Singular name for the models - Models are usually named Singular while the Controllers are named Plural. Please use this convention without having to name your controller, say Controller_User and your model Model_User. Instead, name your controller 'Users' and name your model 'User'. This is a good convention and what most developers do.
Have 'database' in your library autoloader - If you don't have the database library in your autoloader, you won't have access to any of the db->query methods. Make sure you automatically load this library, especially if you've migrated to CodeIgniter 3. Check below for an example:
$autoload['libraries'] = array('database', 'session');


**** timezone setting ****
  application/config/config.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

date_default_timezone_set('Asia/Kolkata');
content_copyCOPY