Category Archives: Codeigniter Tutorial
Codeigniter Path Helper
Codeigniter Path Helper -This helper contains function that returns a server path without symbolic links or related directory structures. An optional second argument will cause an error to be triggered if the path cannot be resolved. $this->load->helper(‘path’); is used to load the helper. The path helper file contains functions that authorize you to work with file paths on the server. Here in this tutorial we are going to explain how to use path helper in codeigniter.
Codeigniter path helper example
Let us first see how to load path helper in codeigniter and then use its function-
Load path helper
How to load path helper in codeingniter example:
$this->load->helper('path'); |
Functions:-
The following functions are available in path helper.
-
Absolute path OR real path
- $path (string) : Path
- $check_existance (bool) : Whether to check if the path actually exists
- Returns : An absolute path
- Return type : String
Syntax of absolute path function is
Syntax of absolute path function is:-
set_realpath($path[$check_existance = FALSE]) |
Parameters:
EXAMPLE
Here is simple example of absolute path OR real path.
Absolute path OR real path in codeigniter example:-
//Controllers part public function checkPath() { $this->load->helper('path'); $this->load->view('checkPath_view'); } // Views parts |
The output of the above example will be something like this –
Codeigniter Language Helper
Codeigniter Language Helper– This function returns a line of text from a loaded language file with simplified syntax that may be more desirable for view files than calling. $this->load->helper(‘language’); is used to load the helper. The optional second parameter will also output a form label for you. The language helper file contains functions that assist in working with language files. Here in this tutorial, we are going to explain how to use this helper with the example.
Codeigniter language helper example
Now we will see how to load language helper in codeigniter and then use its function-
Load language helper
How to load language helper in codeingniter example:
$this->load->helper('language'); |
Available functions:-
Following functions are available in language helper.
Language line
Syntax of language line function
Syntax of language line function is :-
lang($line[$for = ''[$attributes = array()]]) |
- $line (string) : Language line key
- $for (string) : HTML “for” attribute (ID of the element we’re creating a label for)
- $attributes (array) : Any additional HTML attributes
- Returns : The language line; in an HTML label tag, if the $for parameter is not empty
- Return type : string
Parameters:
EXAMPLE
Here is simple example of language line tags.
Language line tags in codeigniter example:-
|
The output of the above example will be something like this –
Codeigniter Number Helper
Codeigniter Number Helper – Codeigniter number helper provide various functions that are used to display the formats number in a bytes. $this->load->helper(‘number’);is used to load the helper. This contains formats numbers as bytes, based on size, and adds the appropriate suffix. Here in this tutorial, we are going to explain how to use this helper with the example.
Codeigniter number helper example
Now we will see how to load number helper in codeigniter and then use its function-
Load number helper
$this->load->helper('number'); |
Available functions:-
Following functions are available in number helper.
Byte format
Syntax of byte format function
Syntax of byte format function is :-
byte_format($num[$precision = 1]) |
- $num (mixed) : Number of bytes
- $precision (int) : Floating point precision
- Returns : Formatted data size string
- Return type : string
Parameters:
EXAMPLE
Here is simple example of number helper tags.
Number helper tags in codeigniter example:-
|
The output of the above example will be something like this –
Codeigniter Inflector Helper
Codeigniter Inflector Helper – Codeigniter inflector helper provide various functions that are used to convert english word to inflector case. $this->load->helper(‘inflector’);is used to load the helper. This contains functions that permit you to change english words to plural, singular, camel case etc. Here in this tutorial, we are going to explain how to this helper with the example.
Codeigniter inflector helper example
Now we will see how to load inflector helper in codeigniter and then use its function-
Load email helper
$this->load->helper('Inflector'); |
Functions:-
Following functions are available in inflector helper.
- 1. Singular Function
- 2. Plural Function
- 3. Camelize Function
- 4. Underscore Function
- 5. Humanize Function
- 6. is_countable Function
1. Singular function
Singular function is used to change a plural word to singular
Syntax of singular function is :-
singular($str) |
- $str (string) : Input string
- Returns : A singular word
- Return type : string
Parameters:
EXAMPLE
Here is simple example of inflector tags.
Inflector function tags in codeigniter example:-
//Controllers part public function inflect() { $this->load->helper('Inflector '); $this->load->view('view_page'); } // Views parts |
The output of the above example will be something like this –
2. Plural function
Plural function is used to change a singular word to plural
Syntax of plural function is :-
plural($str) |
- $str (string) : Input string
- Returns : A plural word
- Return type : string
Parameters:
EXAMPLE
Here is simple example of plural inflector tags.
Plural inflector function tags in codeigniter example:-
// Views parts |
The output of the above example will be something like this –
3. Camelized function
Camelized function is used to change a words separated by spaces or underscores to camel case
Syntax of camelized function is :-
camelize($str) |
- $str (string) : Input string
- Returns : Camelized string
- Return type : string
Parameters:
EXAMPLE
Here is simple example of camelized inflector tags.
Camelized inflector function tags in codeigniter example:-
// Views parts |
The output of the above example will be something like this –
4. UnderScore function
Underscore function is used to take multiple words separated by spaces and underscores them
Syntax of underscore function is :-
underscore($str) |
- $str (string) : Input string
- Returns : String containing underscores instead of spaces
- Return type : string
Parameters:
EXAMPLE
Here is simple example of underscore inflector Tags.
Underscore inflector function tags in codeigniter example:-
// Views parts |
The output of the above example will be something like this –
5. Humanized function
Humanized function is used to take multiple words separated by underscores and adds spaces between them. Each word is capitalized
Syntax of humanized function is :-
humanize($str[$separator = '_']) |
- $str (string) : Input string
- $separator (string) : Input separator
- Returns : Humanized string
- Return type : string
Parameters:
EXAMPLE
Here is simple example of humanized inflector tags.
Humanized inflector function tags in codeigniter example:-
// Views parts |
The output of the above example will be something like this –
6. Countable function
Countable function is used to print output in boolean data type
Syntax of countable function is :-
is_countable($word) |
- $word (string) : Input string
- Returns : TRUE if the word is countable or FALSE if not
- Return type : bool
Parameters:
EXAMPLE
Here is simple example of countable inflector tags.
Countable inflector function tags in codeigniter example:-
// Views parts |
The output of the above example will be something like this –
Codeigniter Html Helper
Codeigniter Html Helper – Codeigniter html helper provides various functions which is used to create Heading , image, Link and List Tags. $this->load->helper(‘html’); is used to load the helper. The html helper file contains functions that assist in working with html. Here in this tutorial we are going to explain how to use html helper in codeigniter.
Codeigniter html helper example
Let us first see how to load html helper in codeigniter and then use its function-
Load html helper
How to load html helper in codeingniter example:
$this->load->helper('html'); |
Functions:-
There are many functions are available in html helper. Now we will explain one by one with example.
- 1. HTML heading tag
- 2. HTML image tag
- 3. HTML link tag
- 4. HTML list tag
- 5. HTML line break tag
- 6. HTML Non Breaking space tag
1. HTML heading tag
Syntax of heading tags function is
Syntax of heading tags function is:-
heading([$data = ''[$h = '1'[$attributes = '']]]) |
- $data (string) : Content
- $heading (string) : Heading level
- $attributes (mixed) : HTML attributes
- Returns : HTML heading tag
- Return type : String
Parameters:
EXAMPLE
Here is simple example of html heading tags.
Html heading tags in codeigniter example:-
//Controllers part public function heading() { $this->load->helper('html'); $this->load->view('heading_view'); } // Views parts 'mycustomclass','style' => 'color: red;');?> 'question', 'class' => 'green', 'style' => 'color:green'));?> |
The output of the above example will be something like this –
2. Html image tag
Syntax of image Tags function is
Syntax of image tags function is:-
img([$src = ''[$index_page = FALSE[$attributes = '']]]) |
- $src (string) : Image source data
- $index_page (bool) : Whether to treat $src as a routed URI string
- $attributes (array) : HTML attributes
- Returns : HTML image tag
- Return type : String
Parameters:
EXAMPLE
Here is simple example of html image tags.
Html image Tags in codeigniter example:-
// Views parts // // |
The output of the above example will be something like this –
3. Html link tag
Syntax of link tags function is
Syntax of link tags function is:-
link_tag([$href = ''[$rel = 'stylesheet'[$type = 'text/css'[$title = ''[$media = ''[$index_page = FALSE]]]]]]) |
- $href (string) : What are we linking to
- $rel (string) : Relation type
- $type (string) : Type of the related document
- $title (string) : Link title
- $media (string) : Media type
- $index_page (bool) : Whether to treat $src as a routed URI string
- Return : HTML link tag
- Return type : String
Parameters:
EXAMPLE
Here is simple example of html link tags. Which is used to connect external css and javascript file.
Html link tags in codeigniter example:-
// Views parts 'css file path', 'rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'text' );?> |
The output of the above example will be something like this –
3. Html list tag
There are two types Of list tags
1. Unordered list.
2. Ordered list.
Syntax of link tags function is:-
ul($list[$attributes = '']) ol($list, $attributes = '') |
- $list (array) : List entries
- $attributes (array) : HTML attributes
- Return : HTML list tag
- Return type : String
Parameters:
EXAMPLE
Here is simple example of html list tags.
Html list tags in codeigniter example:-
// Views parts // Unordered List |
The output of the above example will be something like this –
3. Html line break tag
Syntax of line break tags is:-
Syntax of line break tags function is:-
br([$count = 1]) |
- $count (int) : Number of times to repeat the tag
- Return : HTML line Break tag
- Return type : String
Parameters:
EXAMPLE
Here is simple example of html line break tags.
Html line break tags in codeigniter example:-
// Views parts echo br(3); |
This Tag will produce three times break in code section like this-
3. Html non breaking space tag
Syntax of non breaking space tag is:-
Syntax of non breaking space tag function is:-
nbs([$num = 1]) |
- $num (int) : Number of space entities to produce
- Return : A sequence of non-breaking space HTML entities
- Return type : String
Parameters:
EXAMPLE
Here is simple example of html non breaking space tag.
Html non breaking space tag in codeigniter example:-
// Views parts echo nbs(3); |
This Tag will produce three times non-breaking spaces ( )in Single Line Code like this-
Codeigniter Form Helper
Codeigniter Form Helper – Codeigniter Form helper provides various functions that are used to create the form $this->load->helper(‘form’); is used to load the helper. The Form Helper file contains functions that assist in working with forms. Here in this tutorial, we are going to explain how to create a form in CodeIgniter.
Codeigniter Form Helper Example
Now we will see how to load form helper in codeigniter and then use its function-
Load Form Helper
$this->load->helper('form'); |
Functions:-
There are many functions are available in Form helper. Now we will explain one by one with example.
1. Form Open and Adding Attributes
Syntax of form_open and Adding Attributes function is
Syntax of form_open and Adding Attributes function is:-
form_open([$action = ''[$attributes = ''[ $hidden = array()]]]) $attributes = array('class' => 'email', 'id' => 'myform'); echo form_open('email/send', $attributes); |
Parameters:
- $action (string) : Form action/target URI string
- $attributes (array) : HTML attributes
- $hidden (array) : An array of hidden fields’ definitions
- Returns: : An HTML form opening tag
- Return type : string
2. Form Hidden
Syntax of form_hidden function is
Syntax of form_hidden function is:-
form_hidden($name[$value = '']) |
Parameters:
- $name (string) : Field name
- $value (string) : Field value
- Return type : string
3. Form Input
Syntax of form_input function is
Syntax of form_input function is:-
form_input([$data = ''[$value = ''[ $extra = '']]]) form_password([$data = ''[$value = ''[ $extra = '']]]) form_upload([$data = ''[$value = ''[$extra = '']]]) form_textarea([$data = ''[, $value = ''[, $extra = '']]]) |
Parameters:
- $data (array) : Field attributes data
- $value (string) : Field value
- $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
- Return type : string
4. Form DropDown
Syntax of form_dropdown function is
Syntax of form_dropdown function is:-
form_dropdown([$name = ''[$options = array()[$selected = array()[$extra = '']]]]) |
Parameters:
- $name (string) : Field name
- $options (array) : An associative array of options to be listed
- $selected (array) : List of fields to mark with the selected attribute
- $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
- Returns : An HTML dropdown select field tag
- Return type : string
5. Form Fieldset
Syntax of form_fieldset function is
Syntax of form_fieldset function is:-
form_fieldset([$legend_text = ''[$attributes = array()]]) form_fieldset_close([$extra = '']) |
Parameters:
- $legend_text (string) : Text to put in the legend tag
- $attributes (array) : Attributes to be set on the fieldset tag
- Returns : An HTML fieldset opening tag
- Return type : string
6. Form Checkbox and Radio Button
Syntax of form_checkbox and radiobutton function is
Syntax of form_checkbox and radiobutton function is:-
form_checkbox([$data = ''[$value = ''[$checked = FALSE[$extra = '']]]]) form_radio([$data = ''[$value = ''[$checked = FALSE[$extra = '']]]]) |
Parameters:
- $data (array) : Field attributes data
- $value (string) : Field value
- $checked (bool) : Whether to mark the checkbox as being checked
- $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
- Returns : An HTML checkbox and Radio Button input tag
- Return type : string
7. Form Label
Syntax of form_label function is
Syntax of form_label function is:-
form_label([$label_text = ''[, $id = ''[, $attributes = array()]]]) |
Parameters:
- $label_text (string) : Text to put in the
- $id (string) : ID of the form element that we’re making a label for
- $attributes (string) : HTML attributes
- Return type : string
8. Form Submit , Reset and Button
Syntax of form_submit , Reset and button function is
Syntax of form_submit , Reset and button function is:-
form_submit([$data = ''[$value = ''[$extra = '']]]) form_reset([$data = ''[$value = ''[$extra = '']]]) form_button([$data = ''[$content = ''[$extra = '']]]) |
Parameters:
- $data (string) : Button name
- $value (string) : Button value
- $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
- Return : An HTML input submit , Reset and button tag
- Return type : string
9. Form Close
Syntax of form_close function is
Syntax of form_close function is:-
form_close([$extra = '']) |
Parameters:
- $extra (string) : Anything to append after the closing tag, as is
- Return : An HTML form closing tag
- Return type : string
10. Form Error
Syntax of form_error function is
Syntax of form_error function is:-
form_error([$field = ''[, $prefix = ''[, $suffix = '']]]) |
Parameters:
- $field (string) : Field name
- $prefix (string) : Error opening tag
- $suffix (string) : Error closing tag
- Return : HTML-formatted form validation error message(s)
- Return type : string
11. Form Validate Error
Syntax of validation_errors function is
Syntax of validation_errors function is:-
validation_errors([$prefix = ''[, $suffix = '']]) |
Parameters:
- $prefix (string) : Error opening tag
- $suffix (string) : Error closing tag
- Return : HTML-formatted form validation error message(s)
- Return type : string
EXAMPLE 1
Here is simple example to Form Open and Adding Attributes.
| Form Open and Adding Attributes in Codeigniter Example:-
controllers public function form() { $this->load->helper('form'); $this->load->view('form_view'); } views part:- $attributes = array('class' => 'email', 'id' => 'myform'); echo form_open('email/send', $attributes); |
EXAMPLE 2
Here is simple example to Form Hidden.
| Form Hidden in Codeigniter Example:-
views part $data = array( 'name' => 'John Doe', 'email' => 'john@example.com', 'url' => 'http://example.com' ); echo form_hidden($data); |
EXAMPLE 3
Here is simple example to Form Input.
| Form Input in Codeigniter Example:-
views part echo form_label('User Name :'); $input = array('name'=>'username','id'=>'username','value'=>'johndoe', 'maxlength'=>'100','size'=>'50','style'=>'width:50%'); echo form_input($input); echo form_label('Password :'); $password = array('name'=>'passowrd','id'=>'password','type'=>'password', 'maxlength'=>'100','size'=>'50','style'=>'width:50%'); echo form_password($password); echo form_label('File Upload :'); $upload = array('name'=>'upload','id'=>'upload','type'=>'file'); echo form_open_multipart($upload); echo form_label('TextArea :'); $text_area= array('name'=>'txt_area','id'=>'txt_area','value'=>'johndoe', 'rows'=>'5','cols'=>'10','style'=>'width:50%'); echo form_textarea($text_area); |
EXAMPLE 4
Here is simple example to Form DropDown.
| Form DropDownin Codeigniter Example:-
views part $options = array( 'Cse' => 'Computer Science Engineering', 'Ece' => 'Electronics engg', 'Mech' => 'Mechanical Engg', 'EEE' => 'Electrical Engg', ); echo form_dropdown('Course', $options); |
EXAMPLE 5
Here is simple example to Form Fieldset.
| Form Fieldset in Codeigniter Example:-
views part $field_set = array( 'id' => 'address_info', 'class' => 'address_info' ); echo form_fieldset('Address Information', $field_set ); echo " |
EXAMPLE 6
Here is simple example to Form Checkbox and Radiobutton.
| Form Checkbox and Radiobutton in Codeigniter Example:-
views part Check box echo form_label('course :'); echo form_label('php :'); $data = array('name'=>'course','id'=>'php','value'=>'accept', 'checked'=>false,'style'=>'margin:10px'); echo form_checkbox($data); echo form_label('java :')?> $data = array('name'=>'course','id'=>'java','value'=>'accept', 'checked'=> false,'style'=>'margin:10px'); echo form_checkbox($data); Radio Button $male=array('id'=>'2','name'=>'gen'); $female=array('id'=>'3','name'=>'gen'); echo " |
EXAMPLE 7
Here is simple example to Form Label.
| Form label in Codeigniter Example:-
views part $attributes = array( 'class' => 'mycustomclass', 'style' => 'color: red;' ); echo form_label('What is your Name', 'username', $attributes); |
EXAMPLE 8
Here is simple example to Form Submit and Reset Button.
| Form Submit and Reset Button in Codeigniter Example:-
views part $data = array('name'=>'submit','id'=>'submit','value'=>'submit', 'type'=>'submit','content'=>'submit post'); echo form_submit($data); $data = array('name'=>'button','id'=>'button','value'=>'true', 'type'=>'reset','content'=>'Reset'); echo form_reset($data); |
EXAMPLE 9
Here is simple example to Form close.
| Form Close in Codeigniter Example:-
views part $string = ''; echo form_close($string); |
EXAMPLE 10
Here is simple example to Form Error.
| Form Error in Codeigniter Example:-
views part echo form_error('myfield', ' |
EXAMPLE 11
Here is simple example to Form validate Error.
| Form Validate Error in Codeigniter Example:-
views part
echo validation_errors('', '');
|
Complete Example To Create Contact Form Using CodeIgniter
| Create Contact Form Using Codeigniter Example:-
Controllers part public function form() { $this->load->helper('form'); $this->load->view('form_view'); } Views part 'name', 'name' => 'name')); ?> |
The output of the above example will be something like this –
Codeigniter File Helper
Codeigniter File Helper – Codeigniter file helper provides various functions which is used to access the file. $this->load->helper(‘file’); is used to load the helper. This contains the functions – read_file , write_file and delete_files. Here in this tutorial we are going to explain how to read , write and delete files in codeigniter.
Codeigniter File Helper Example
Now we will see how to load file helper in codeigniter and then use its function-
Load file Helper
$this->load->helper('file'); |
Functions:-
Following functions are available in file helper.
- 1. Read File
- 2. Write File
- 3. Delete File
1. Read_File
Syntax of Read_File function is
Syntax of Read_File function is:-
read_file($fileName) |
Parameters:
- $fileName (string) : File path
- Returns : File contents or FALSE on failure
- Return type : string
2. write_file
Syntax of Write_File function is
Syntax of Write_File function is:-
write_file($path, $data[, $mode = 'wb']) |
Parameters:
- $path (string) : File Path
- $data (string) : Data to write to file
- $mode (string) : fopen() mode
2. Delete_files
Syntax of Delete_Files function is
Syntax of Delete_Files function is:-
delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]]) |
Parameters:
- $path (string) : File Path
- $del_dir (bool) (string) : Whether to also delete directories
- $htdocs (bool) : Whether to skip deleting .htaccess and index page files
- Returns : TRUE OR FALSE
- Return Type : Bool
EXAMPLE 1
Here is simple example of Read File.
| Read File in Codeigniter Example:-
public function readfiles() { $this->load->helper('file'); $path = 'http://localhost/CodeIgniter/media/write.txt'; $string = read_file($path); echo $string; } |
EXAMPLE 2
Here is simple example of Write File.
| Write File in Codeigniter Example:-
public function writefiles() { $this->load->helper('file'); $path = 'http://localhost/CodeIgniter/media/new.txt'; $data = 'Good Morning Friends'; if ( ! write_file($path, $data)) { echo 'Unable to write'; } else { echo 'File written!'; } } |
EXAMPLE 3
Here is simple example of Delete Files.
| Delete Files in Codeigniter Example:-
public function Deletefiles() { $this->load->helper('file'); $path = 'http://localhost/CodeIgniter/media/file/abc.txt'; delete_files($path, TRUE); } |
Codeigniter Email Helper
Codeigniter Email Helper – Codeigniter email helper provides various functions that are used to send the emails. $this->load->helper(’email’); is used to load the helper. This contains the functions – valid_email and send_email. Here in this tutorial we are going to explain how to send an email in codeigniter.
Codeigniter Email Helper Example
Now we will see how to load email helper in codeigniter and then use its function-
Load Email Helper
$this->load->helper('email'); |
Functions:-
Following functions are available in email helper.
- 1. Validate Email
- 2. Send Email
1. Validate_Email
Syntax of Validate_email function is
Syntax of validate_email function is:-
(valid_email($email)) |
- $email (string) : E-mail address
- Returns : TRUE if a valid email is supplied, FALSE otherwise
- Return type : bool
Parameters:
2. Send_Email
Syntax of Send_email function is
Syntax of Send_email function is:-
send_email($email,$subject,$message) |
- $email (string) : E-mail address
- $subject (string) : Mail subject
- $message (string) :Message body
Parameters:
EXAMPLE 1
Here is simple example to validate email.
| Validate Email in Codeigniter Example:-
public function ValidateEmail() { $this->load->helper('email'); //$this->load->view('email_view'); $email = 'sonukr321@gmail.com'; if (valid_email($email)) { echo 'email is valid'; } else { echo 'email is not valid'; } } |
EXAMPLE 2
Here is simple example to send email.
Send Email in Codeigniter Example:-
public function sendemail() { $this->load->helper('email'); //$this->load->view('email_view'); $email = 'sonukr321@gmail.com'; $subject = 'hello'; $message = 'hi'; if (send_email($email,$subject,$message)) { echo 'sent succesfully'; } else { echo 'unable to send'; } } |
Codeigniter Download Helper
Codeigniter Download Helper Codeigniter download helper enables you to download data from server to your system. It is very easy to use the download helper. Here in this tutorial we are going to explain how you can load this helper and use it’s functions.
Codeigniter Download Helper | Load | Syntax Example
Let us first see how to load download helper in codeigniter and then use its function-
Load Download Helper
To load Download Helper in Codeingniter Example:
$this->load->helper('download'); |
Functions
Following functions are available in download helper.
force_download
Syntax of force_download function is-
force_download Function Syntax:
force_download($filename , $data, $set_mime) |
Parameters-
Following parameters are accepted by force_download function-
- $filename(string) – Name of file.
- $data(mixed) Content of file.-
- $set_mime(bool) – Whether to try to send the actual MIME type
Return Type-
Return Type of this function is void.
Example 1
Here is simple example to download the image.
Download File in Codeigniter Example:
$this->load->helper('download'); force_download('/img/myimage.jpg', NULL); |
Example 2
Download File in Codeigniter Example:
$this->load->helper('download'); $data = 'This is dummy content'; $name = 'myFile.txt'; force_download($name, $data); |