Showing posts with label Codeigniter. Show all posts
Showing posts with label Codeigniter. Show all posts

Friday, 11 September 2015

How To Remove index.php From URL in Codeigniter

Once we install the default Codeigniter 3.0 then by default there is index.php in the URL. So the URL looks like http://www.yourdomain.com/index.php/controller/function
For SEO point of view we require a clean URL and codeigniter provide easy way to remove index.php for URL rewrite functionality. It can be easily achieve by using .htaccess and few changes in config file.
Follow the steps to remove index.php from URL:

Step 1: Go to “application/config/config.php”
Find below code:-
$config['index_page'] = 'index.php';
$config['index_page'] = '';

Step 2: create htaccess file on your project root location and add following lines : PHP
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

After that your website URL has been cleaned and new URL will be http://www.yourdomain.com/ controller/function

How To Create Layouts In Codeigniter 3.0

Codeigniter 3.0 does not have any layouts feature by default. We have to write some basic php code for enabling layout concept in codeigniter. In this tutorial we will explain how to create custom layout or template for CodeIgniter 3.0. In the below steps we assumes the page have 4 sections e.g header, footer, left panel and body area.
Follow the mentioned steps to create layouts or templates in codeigniter 3.0:

Step 1: Create a “MY_Controller.php” under “application/core/” and add below code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class MY_Controller extends CI_Controller
    {
        //set the class variable.
        public $template  = array();
        public $data      = array();
        /*Loading the default libraries, helper, language */
        public function __construct(){
            parent::__construct();
            $this->load->helper(array('form','language','url'));
            $this->lang->load('english');
        }
        /*Front Page Layout*/
        public function layout() {
            // making template and send data to view.
            $this->template['header']   = $this->load->view('layout/header', $this->data, true);
            $this->template['leftpanel']   = $this->load->view('layout/leftpanel', $this->data, true);
            $this->template['body'] = $this->load->view($this->body, $this->data, true);
            $this->template['footer'] = $this->load->view('layout/footer', $this->data, true);
            $this->load->view('layout/front', $this->template);
        }
    }
Step  2: Create a folder “layout” under “application/views/”.

Step 3: Create a file “header.php” under “application/views/layout/” for header section and add mentioned code

<div style="height:100px; width:100%; border:1px solid #000;"> Your Website Header Section </div>

Step  4: Create a file “footer.php” under “application/views/layout/” for Footer section and add mentioned code
Step  6: Create front.php under “application/views/layout/” and add mentioned code

<html>
    <head>
        <title>StepBlogging.COM Layouts</title>
    </head>
    <body>
    <?php
        if($header) echo $header ;
        if($left)   echo $left ;
        if($middle) echo $middle ;
        if($footer) echo $footer ;
    ?>
    </body>
</html>


Step 7: Create a file “home.php” under “application/views/” for main body content

<div style="height:500px; width:70%; border:2px solid #000; float:left;"> Middle Area</div>
Step  8: Create a controller “Home.php” under “application/controllers/” and add below code :PHP

Thursday, 23 July 2015

Confirm box on click anchor link to delete record

HTML  Code


<a href="javascript:void(0);" onclick="delete(<?php echo $row->id;?>);">Delete</a>
 
Javascript Code

<script type="text/javascript">
    var url="<?php echo base_url();?>";
    function delete(id){
       var r=confirm("Do you want to delete this?")
        if (r==true)
          window.location = url+"user/deleteuser/"+id;
        else
          return false;
        } 
</script>
 

Monday, 20 July 2015

Active Record Caching



While not "true" caching, Active Record enables you to save (or "cache") certain parts of your queries for reuse at a later point in your script's execution. Normally, when an Active Record call is completed, all stored information is reset for the next call. With caching, you can prevent this reset, and reuse information easily.
Cached calls are cumulative. If you make 2 cached select() calls, and then 2 uncached select() calls, this will result in 4 select() calls. There are three Caching functions available:

$this->db->start_cache()

 

This function must be called to begin caching. All Active Record queries of the correct type (see below for supported queries) are stored for later use.

$this->db->stop_cache()

 

This function can be called to stop caching.

$this->db->flush_cache()

 

This function deletes all items from the Active Record cache.
Here's a usage example:

$this->db->start_cache();
$this->db->select('field1');
$this->db->stop_cache();

$this->db->get('tablename');

//Generates: SELECT `field1` FROM (`tablename`)

$this->db->select('field2');
$this->db->get('tablename');

//Generates: SELECT `field1`, `field2` FROM (`tablename`)

$this->db->flush_cache();

$this->db->select('field2');
$this->db->get('tablename');

//Generates: SELECT `field2` FROM (`tablename`)


Note: The following statements can be cached: select, from, join, where, like, group_by, having, order_by, set

Method Chaining



Method chaining allows you to simplify your syntax by connecting multiple functions. Consider this example:

$this->db->select('title')->from('tablename')->where('id', $id)->limit(10, 20);

$query = $this->db->get();
 
Note: Method chaining only works with PHP 5.

Deleting Data in Codeigniter



$this->db->delete();

Generates a delete SQL string and runs the query.

$this->db->delete('tablename', array('id' => $id));

// Produces:
// DELETE FROM tablename
// WHERE id = $id
 
The first parameter is the table name, the second is the where clause. You can also use the where() or or_where() functions instead of passing the data to the second parameter of the function:

$this->db->where('id', $id);
$this->db->delete('tablename');

// Produces:
// DELETE FROM tablename
// WHERE id = $id

An array of table names can be passed into delete() if you would like to delete data from more than 1 table.

$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);

If you want to delete all data from a table, you can use the truncate() function, or empty_table().

$this->db->empty_table();

Generates a delete SQL string and runs the query. $this->db->empty_table('tablename');

// Produces
// DELETE FROM tablename

$this->db->truncate();

Generates a truncate SQL string and runs the query.

$this->db->from('tablename');
$this->db->truncate();
// or
$this->db->truncate('tablename');

// Produce:
// TRUNCATE tablename