Menus

Sunday 18 August 2019

Export HTML table to excel



       This is simple example for data export to excel from PHP or HTML file using java script. It is very useful for web application developers.

Let's try this

Create studentlist.php or studentlist.html file


<html>
<head>
<title>Export to excel</title>

<Script>

function exportExcel(){

    var tableID = "studentslist";
    var filename = "student_list.xls";

    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
}

</Script>

</head>

<body>

<table id="studentslist" width="750" border="1">

<tr><td colspan="2" align="center">Student List</td></tr>

<tr>
<td>SL No</td>
<td>Name</td>
</tr>

<tr>
<td>1</td>
<td>Sahneesh</td>
</tr>

<tr>
<td>2</td>
<td>Aneesh</td>
</tr>

</table>

<button onclick="exportExcel()">Export To Excel File</button>

</body>
</html>






Tuesday 13 August 2019

Codelgniter - Views


Views

A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.

Creating a View

Using your text editor, create a file called blogview.php, and put this in it:
<html>
<head>
        <title>My Blog</title>
</head>
<body>
        <h1>Welcome to my Blog!</h1>
</body>
</html>
Then save the file in your application/views/ directory.

Loading a View

To load a particular view file you will use the following method:
$this->load->view('name');

Adding Dynamic Data to the View

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:
$data = array(
        'title' => 'My Title',
        'heading' => 'My Heading',
        'message' => 'My Message'
);

$this->load->view('blogview', $data);
And here’s an example using an object:
$data = new Someclass();
$this->load->view('blogview', $data);




More Details




Codelgniter - Models



What is a Model?

Models are PHP classes that are designed to work with information in your database.
You might have a model class that contains functions to insert, update, and retrieve your blog data.


Anatomy of a Model

Model classes are stored in your application/models/ directory. 
The basic prototype for a model class is this:
class Model_name extends CI_Model {

}
Where Model_name is the name of your class. 
Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
The file name must match the class name. For example, if this is your class:
class User_model extends CI_Model {

}

Loading a Model

Your models will typically be loaded and called from within your controller methods.


$this->load->model('model_name');
If your model is located in a sub-directory, include the relative path from your models directory. For example, if you have a model located at application/models/blog/Queries.php you’ll load it using:
$this->load->model('blog/queries');
Once loaded, you will access your model methods using an object with the same name as your class:
$this->load->model('model_name');

$this->model_name->method();
parameter of the loading method
$this->load->model('model_name', 'foobar');

$this->foobar->method();



Connecting to your Database


  • You can tell the model loading method to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
    $this->load->model('model_name', '', TRUE);
    
  • You can manually pass database connectivity settings via the third parameter:
    $config['hostname'] = 'localhost';
    $config['username'] = 'myusername';
    $config['password'] = 'mypassword';
    $config['database'] = 'mydatabase';
    $config['dbdriver'] = 'mysqli';
    $config['dbprefix'] = '';
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    
    $this->load->model('model_name', '', $config);

Example:

Controller
<?php
class Blog extends CI_Controller {

public function index()
{
//gather information here from models
$this->load->model('blog_model');
echo $this->blog_model->test_blog();
}
}
?>

Model
<?php
class Blog_model extends CI_Model
{
function test_blog()
{
echo "This is model test funtion";
}
//Database function write here
}
?>


Here is an example of what such a model class might look like:

class Blog_model extends CI_Model {

        public $title;
        public $content;
        public $date;

        public function get_last_ten_entries()
        {
                $query = $this->db->get('entries', 10);
                return $query->result();
        }

        public function insert_entry()
        {
                $this->title    = $_POST['title']; // please read the below note
                $this->content  = $_POST['content'];
                $this->date     = time();

                $this->db->insert('entries', $this);
        }

        public function update_entry()
        {
                $this->title    = $_POST['title'];
                $this->content  = $_POST['content'];
                $this->date     = time();

                $this->db->update('entries', $this, array('id' => $_POST['id']));
        }

}
Note
The methods in the above example use the Query Builder database methods.
More Details


Monday 12 August 2019

Codelgniter - Controllers


Controllers

Controllers are the heart of your application, as they determine how HTTP requests should be handled.


A Controller is simply a class file that is named in a way that can be associated with a URI.
Consider this URI:
example.com/index.php/blog/
Controller name as Blog.php
The file must be called ‘Blog.php’, with a capital ‘B’.
Class names must start with an uppercase letter.

* index function works by default when the controller is loaded.


Let’s try it: Hello World!


Let’s create a simple controller so you can see it in action. Using your text editor, create a file called Blog.php, and put the following code in it:
<?php
class Blog extends CI_Controller {

        public function index()
        {
                echo 'Hello World!';
        }
}
Then save the file to your application/controllers/ directory.
Now visit the your site using a URL similar to this:
example.com/index.php/blog/

Methods

In the above example the method name is index(). The “index” method is always loaded by default if the second segment of the URI is empty. Another way to show your “Hello World” message would be this:
example.com/index.php/blog/index/


Add a new method to your controller:
<?php
class Blog extends CI_Controller {

 public function index()
 {
  echo "Hello World!";  
 }

 public function test()
 {
  echo "Test method";
 }
}
Now load the following URL to see the comment method:
example.com/index.php/blog/test/

Call one method to anther

<?php
class Blog extends CI_Controller {

   public function index()
 {
  echo "Hello World!"; 
  $this->test(); 
 }

 public function test()
 {
  echo "Test method";
 }
}


Passing URI Segments to your methods

If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/blog/index/32/Shaneesh
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php

class Blog extends CI_Controller {

 public function index($id,$name)
 {
  echo $id ." - " ,$name;
 } 

}

?>

Defining a Default Controller

To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'blog';
by default loaded index method in blog controller.

Class Constructors

If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::__construct();
The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.
Example:
<?php
class Blog extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                // Your own constructor code
        }
}

Private methods

private function _utility()
{
        // some code
}

Processing Output

CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically.
public function _output($output)
{
        echo $output;
}

Remapping Method Calls

CodeIgniter permits you to override this behavior through the use of the _remap() method:
public function _remap()
{
        // Some code here...
}

public function _remap($method)
{
        if ($method === 'some_method')
        {
                $this->$method();
        }
        else
        {
                $this->default_method();
        }
}


More details