×

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc.

Loading the HTML Helper

Before using the html helper function, you must load the helper in the controller's files.

Syntax

$this->load->helper (‘html’);

Function of HTML Helper:

1.heading(): A heading() function is used to create a heading in the HTML form.

Syntax

heading ( [ $data = ‘ ‘ [, $h = ‘1’ [, $attributes
= ‘ ‘] ] ]);

It has three parameters:

  • $data: In this parameter, you have to defined the content.
  • $h: It defines the heading level in html such as 1 to 6.
  • $attributes: In this fields, you can define the html attributes such as class and id.

Example

echo heading('Welcome to the tutorials and example', 3);
 echo heading('Here, we have described about the html helper', 4, array('id' => 'helper', 'class' => 'codeigniter')); 

2. img(): This function is used to display images in html pages.

Syntax

img ( [ $src = ’ ‘ [, $index_page = FALSE
[, $attributes = ‘ ‘] ] ]);
  • $src : It represents the image source path in html page.
  • $index_page : It define whether to pass $src as a routed URL string.
  • $attributes: If you want to show additional functionalities in the HTML page, you can define in this attribute.

Example

$show_image1 = array( 'src'  
=> 'images/cloud.jpg' , 'width' => '300', 'height' => '200');
 echo img($show_image1); 

3. link_tag(): A link_tag () function is used to create links and transfer controls to some other HTML / CSS pages.

Syntax

link_tag ( [ $href  = ‘’ [, $ref = ‘stylesheet’ [, $type =
‘text/css’ [, $title = ‘‘ [, $media = ‘ ‘ [, $index_page = FALSE ] ] ] ] ] ] );

Parameters lists:

  • $href = In this field, you can define a stylesheet link as well as other link to transfer control.
  • $rel = It defines the relation type such as stylesheet.
  • $type = It defines related file types such as text, css and javascript, which you have defined in link_tag.
  • $title = It defines the link title.
  • $media = It represents the media type in the link_tag.
  • $index_page = It defines whether you want to treat $src as a routed url string to pass control to other files.

Example

$link = array(
         'href'  => 'css/hello.css',
         'rel'   => 'stylesheet',
         'type'  => 'text/css',
         'media' => 'file' 
 );
 echo link_tag($link); 

4. ul(): It defines the list of items in unordered format.

Syntax

ul ( $list , $attributes = ‘ ‘] );

It has two parameters:

  • $list = It contains an array of un-order list.
  • $attributes = It contains HTML attributes such as class and id that you can define in <div> tag.

Example

$list = array(
             'STAR',
             'PLANET',
             'GALAXY',
             'MOON'
         );
         echo nbs(3);
         $attributes = array( 
             'class' => 'atmosphere',
             'id'    => 'mylist'
         );
         echo ul($list, $attributes); 

5. ol(): This function defines all items in the ordered structure.

Syntax

ol ( $list, $attributes = ‘ ‘ );
  • $list:  It contains an array of order list.
  • $attributes = It contains HTML attributes such as class and id that you can define in <div> tag.

Example

$list = array(
             'apple',
             'mango', 
             'banana',
             'orange'
         );
         $attributes = array(
             'class' => 'show_list',
             'id'    => 'list'
         );
         echo ol($list, $attributes); 

6. meta(): It is used to provide all information about an html document. The content of the meta tag doesn’t display on the web page. You can also define web page related keywords in meta tag to access in search engines.  

Syntax

meta ( [ $name = ‘ ‘ [, $content = ‘ ‘ [,
$type = ‘ name’ [, $newline = ‘n’ ] ] ] ]);

Parameters list:

  • $name: It contains meta name such as keywords, content-type, robots, description in html file.
  • $content: As its name suggests, you can define the content in this parameter.
  • $type: It defines the type of meta tags such as http-equiv or name.
  • $newline: It represents the new line characters.

Example

echo meta( array( ‘name’ = ‘view’, 
‘content’ = ’view’, ‘charset’ = ‘utf-8’)); 

7. br(): This generates a break tag in two continuous lines or paragraphs in an html web page. It is simple in CodeIgniter because you have to define numbers only in the br() function, and it will generate the space according to the number passed in the function.

Syntax

br([ $count = 1 ]);
 $count = It contains a numeric number which is used to display number of times to repeat the <br> tag. 

Example

echo br(4); // it will create 4 times <br><br><br><br> 

8. nbs(): It is used to create space between two consecutive words.

Syntax

nbs( [ $num = 1 ]);

Parameter List:

$num = It contains a numerical value that is used to produce space between two or more words.

Example

 echo nbs(4); // it shows 4 times (&nbsp) (&nbsp) (&nbsp) (&nbsp)

Example of HTML helper

Create a controller file Online.php and save it in application/controller/. After that, write the following program in your controller file.

Online.php

<?php
 class Online extends CI_Controller
 {
     public function html_helper() { 
      $this->load->helper('html'); // load the html helper
       echo "<title> Tutorial and Example</title>";
         $link = array(
             'href'  => '#.css',
             'rel'   => 'stylesheet',
             'type'  => 'text/css', 
             'media' => 'audio'
         );
         echo link_tag($link);
         echo heading("Hello Learner", 2);
         echo heading('Welcome to the tutorials and example', 3);
         echo heading('Here, we have described about the html helper', 4, array('id' => 'question', 'class' => 'green')); 
         echo br(2);
         $image_properties = array(
             'src'   => 'images/welcome.png', 
             'alt'   => 'sorry for the issue we are working on it',
             'class' => 'my_images',
             'width' => '300',
             'height'=> '200', 
             'title' => ' image gallery',
             'rel'   => 'roundbox'
         );
         $show_image1 = array( 'src'   => 'images/cloud.jpg' , 'width' => '300', 'height' => '200');
         $show_image2 = array( 'src'   => 'images/download.jpg', 'width' => '300', 'height' => '200' );
         $show_image3 = array( 'src'   => 'images/valley', 'width' => '300', 'height' => '200'  );
         echo img($image_properties);
         echo img($show_image1); 
         echo img($show_image2);
         echo img($show_image3;
        $list = array(
         'STAR',
         'PLANET',
         'GALAXY',
         'MOON'
     );  
     $attributes = array( 
         'class' => 'atmosphere',
         'id'    => 'mylist'
     );
     echo ul($list, $attributes);   
         $list = array(
             'apple',
             'mango', 
             'banana',
             'orange'
         );
         $attributes = array(
             'class' => 'show_list',
             'id'    => 'list'
         );
         echo ol($list, $attributes);   
     }
 }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/, it shows the output, as shown below.

HTML HELPER

Related Topics

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.