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.