Showing posts with label Codeignter. Show all posts
Showing posts with label Codeignter. Show all posts

Thursday, September 8, 2016

Codeigniter tutorial 7

   No comments     
categories: ,
Codeigniter and MySQL

Codeigniter by default assumes that we don't need databases.
So we need to configure the database connection.

In config/autoload.php we add database library

 $autoload['libraries'] = array('database');

Then in config/database.php we do the configurations with the details of database and hosts

$db['default'] = array(
    'dsn'    => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'ci',
    'dbdriver' => 'mysqli',

     ....................
here my database name is 'ci'. And I have a table 'users' with these
attributes

# Name Type





1 int(11)




2 varchar(50)




3 varchar(50)




Some values for the tables,


id firstname email
1 Ravishan ravishan3@gmail.com
2 Yuzishan yuzishan.edu@gmail.com

The model file is responsible for the database queries. 
Here I create a model file named user_model.php

<?php
class User_model extends CI_Model{
    function getFirstName(){
        $query = $this->db->query('SELECT firstname FROM users');
        return $query->result();
    }
    function getUsers(){
        $query = $this->db->query('SELECT * FROM users');
        return $query->result();
    }
}


Above first function getFirstName() is to retrieve only the first names from the users table and the second function getUsers() is to retrieve all the data from the table.

The controller is to load the mode file and have variables to store the data which retrieved from the model file.

my controller file is hello.php

<?php
class Hello2 extends CI_Controller{
    public function index(){
        $this->home();
    }
    public function home(){
        $this->load->model('user_model');
        $data['names'] = $this->user_model->getFirstName();
        $data['users'] = $this->user_model->getUsers();
        $this->load->view('message',$data);
    }
}
?>


In the above code I have used the index function, which is loaded automatically with the controller name.
Here I save all the first names in an array named 'names'
and the full data in an array named users, which I use these array names in the view file.

Then the viewing part , message.php.
All the data loaded as objects, so that I can view them accordingly,

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<?php
    foreach($names as $object){
        echo $object->firstname . '<br/>';
    }
    foreach ($users as $object){
        echo $object->firstname ."'s email addrerss is: ".   $object->email.'<br/>';
    }
?>
</body>
</html>

Thursday, July 14, 2016

Remove index.php using .htaccess : Codeigniter tutorial 6

   No comments     
categories: 

To remove 'index.php' from CodeIgniter URL, we need to make some changes in the configuration file.

Step 1 
open application/config/config.php
search for the line 
$config['index_page'] = "index.php";
and remove 'index.php' from that line.
($config['index_page'] = "";)

Step 2

Create a new file inside Codeigniter folder and save it as '.htaccess'

paste the following code inside '.htaccess' file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Thursday, July 7, 2016

Codeigniter tutorial 5

   No comments     
categories: 
I have posted tutorials on, working with Controller and View files in the MVC Pattern Using Codeigniter.

Now I go through with Model.


In the application folder, we have a sub-folder model
Create a new file new_model.php in model folder.

<?php
Class New_model extends CI_Model{
public function getProfile($name){
return array ("fullname"=>"Nimishan","email"=>"nimishan.edu","age"=>25);
}
}
?>

Here the Base class is CI_Model. I created a function getProfile() to assign some values to an array and the variable 'name' is the function parameter for retrieving the output of the function.

Now we see how to get the output from the model to the Controller.

in the code\application\controllers edit the hello.php.

That is to create a variable to assign the values of the getProfile function which is in the model file , and also print the variable which is assigned as array elements.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first($name){
      $this->load->model('new_model');
$profile = $this->new_model->getProfile("shan");
print_r($profile);
}
}
?>

here I load the model file and assign the values, then print using print_r, which is used to print the output other types, not just strings.


the URL for retrieving the data is;
localhost/code/index.php/hello/first/shan

here the value shan is used as the parameter for the getProfile method.

Friday, June 24, 2016

Codeigniter tutorial 4

   No comments     
categories: 
Data passing from controller to view

I posted CodeIgniter URL Routing in the controller, Special methods, and Controller and View files earlier.

Now we see, how to pass some data to view files from controller

My view file first_view.php
<html>
<head>
<title>View</title>
</head>
<body>
<h1>This is a view file</h1>
<h2>The UI part</h2>
</body>
</html>

edit this file, give a variable in PHP.

<html>
<head>
<title>View</title>
</head>
<body>
<h1>This is a view file</h1>
<h2>Hello, <?php echo $name ?></h2>
</body>
</html>

in the code\application\controllers edit the hello.php.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first(){
$data=array("name"=>"Nimishan");
$this->load->view('first_view',$data);
}

}
the input string is assigned into the variable data and the passed to view file.
then the output would be (for URL see image)



Also, the a value can be passed using a parameter.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first($value){
$data=array("name"=>$value);
$this->load->view('first_view',$data);
}

}

here I add a variable inside the function parenthesis, and it should be assigned to the variable data which is the loaded into the view file. 

Thursday, June 23, 2016

Codeigniter tutorial 3

   No comments     
categories: 
I introduced CodeIgniter routing and parameter passing on the controller in previous posts.

Now I show creating functions to load 'view' files where we have our UI.
Create a file in 'code\application\views'. Here the 'code' is the renamed folder of extracted CodeIgniter zipped file. 

first_view.php

<html>
<head>
<title>View</title>
</head>
<body>
<h1>This is a view file</h1>
<h2>The UI part</h2>
</body>
</html>

in the code\application\controllers edit the hello.php.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first(){
$this->load->view('first_view');
}

}
?>

This function will load the view file.

Then the URL to get the view file from the controller method is;
localhost/code/index.php/hello



Here, since I used special function my URL does not contain any controller function name.

Now I create another file in view say `header_view.php`

<html>
<head>
<title>View</title>
</head>
<body>
<h2>Header</h2>
 <hr/>
</body>
</html>


And I load both the functions by the following file `hello.php`

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first(){
$this->load->view('header_view');
$this->load->view('first_view');
}
}
?>


Here both the view files loaded as one output.

Codeigniter tutorial 2

   No comments     
categories: 
CodeIgniter special method

For routing first, the browser looks the controller name then the method name and parameters if available.

In Codeigniter tutorial, we created a function and a function with the parameters.
What if we missed any parameter value in our URL

We get warning and notice about missing argument.

But we can give default values for the parameters;
for example,

hello.php

<?php
class Hello extends CI_Controller{
public function first($p1,$p2="Nimishan"){
echo "This is the first method<br>";
echo "My parameters are : $p1 and $p2";
}
}
?>

here I gave a value 'Nimishan' for the second parameter. and I call the function in URL as below,
localhost/code/index.php/hello/first/blog

So, the output is,



here I call the function only with one parameter, the second parameter is the given value in the php code.

This parameter passing is not a functionality of codeIgnter, It is a functionality of PHP

Create a new function and call that function only.

<?php
class Hello extends CI_Controller{
public function first($p1="Hello", $p2="Nimishan"){
echo "This is the first method<br>";
echo "My parameters are : $p1 , $p2";
}
public function second(){
echo "This is the second method<br>";
}
}
?>

The output with the URL shown below:



What if we have two functions to be shown and we call those functions only with the controller name?
if we type this: localhost/code/index.php/hello
We get 404 error.

We can call the URL only with the controller name by creating a special function called  'index' .

<?php
class Hello extends CI_Controller{
public function index(){
echo "This is a special function";
}
public function first($p1="Hello", $p2="Nimishan"){
echo "This is the first method<br>";
echo "My parameters are : $p1 , $p2";
}
public function second(){
echo "This is the second method<br>";
}
}
?>

Thus the above URL gives no error but this,


therefore we can use this special function to get all the functions without any struggle to call them.

the code:

<?php
class Hello extends CI_Controller{
public function index(){
echo "This is a special function<br><br>";
$this->first();
$this->second();
}
public function first($p1="Hello", $p2="Nimishan"){
echo "This is the first method<br>";
echo "$p1 , $p2<br><br>";
}
public function second(){
echo "This is the second method<br>";
}
}
?>

Here I have used some break tags to show and separate the outputs of each function.



In the output, we have all three methods.

Here the browser found no methods in the URL it looks for the index method.



Codeigniter tutorial

   No comments     
categories: 

CodeIgniter  

                        Is light Weight
                        No installation required
                        Flexible

CodeIgniter has a very efficient URL structure, it generates a well optimised Search engine friendly URL.

URL Routing in CodeIgniter

General URL structure for CodeIgniter:
<baseURL>/index.php/<controller_name>/<controller_function>

Download CodeIgniter here.

I renamed the extracted CodeIgniter folder into 'code' for my convenience. My PHP development environment is XAMPP. So, 'code' folder location will be 
C:\xampp\htdocs

type 'localhost/code' on a browser, it shows the CodeIgniter welcome message. if not check your XAMPP Control Panel.

First, I create a controller function. 


hello.php

<?php
class Hello extends CI_Controller{
public function first(){
echo "This is the first method";
}
}
?>

Here the file name is 'hello.php', where the class name is capitalised file name, that is 'Hello' and the base class is CI_Controller.

for call the method in the browser, type localhost/code/index.php/hello/first

hello is the controller name and the first is the controller function name.
The output is:


Function with Parameters

We can input parameters inside the parenthesis. the same program,

<?php
class Hello extends CI_Controller{
public function first($p1,$p2){
echo "This is the first method <br>";
echo "My parameters are : $p1 and $p2";
}
}
?>

Here I have created two parameters as $p1 and $p2
So the URL for calling the function with parameters is 
localhost/code/index.php/hello/first/one/two

and the output was:



  thus, you can change the value for the parameter and get the output as your wish.