×

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

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 Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 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.

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 Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 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 Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 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 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.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

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...

7 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 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 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.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.