Codeigniter: Helper for template library dynamic view partial loading

Hello everyone,

I have recently been in quick-fire blogging mode. After playing with the superb template library developed by Phil Sturgeon.

The following template helper is the outcome of tweaking that makes life easier while setting up view partials and calling them inside the layout dynamically. The helper also has an add-on use which gives user the option to use region specific partial loading.

Controller:

welcome.php

$partial_list[]['region'] = 'left';
$partial_list[]['name'] = 'category';
$partial_list[]['view'] = 'partial_category';
$partial_list[]['region'] = 'right';
$partial_list[]['name'] = 'login_box';
$partial_list[]['view'] = 'partial_login';

/* following functionautomatically sets the partials
* based on the above array and also passes $partial_list
* variable to the view.
*/

/*
* make sure the pareser helper is loaded before
* calling this function
*/

setup_partials($partial_list);

$this->template->set_layout('home_layout');
$this->template->build('home_view');


Layout:

home_view.php

<html>
<head>
	<title><?php echo $template['title'];?></title>
</head>
<body>
	<div class="left">
		/* Will show only partials with region set as left */
		<?php show_partials('list', 'left', $partial_list, $template); ?>
	</div>
	<div class="mid"><?php echo $template['body'];?></div>
	<div class="right">
		/* Will show only partials with region set as right */
		<?php show_partials('list', 'right', $partial_list, $template);?>
	</div>
</body>
</html>

Detail about the usage can be found in the actual helper file, also feel free to comment here and I will try my best to help out.

Download Helper:

DOWNLOAD PARSER HELPER

thanks,

faisal ahmed

web application developer
web: http://www.faisalbd.com/
email: thephpx(at)gmail(dot)com

Share

Google translate helper for codeigniter

Hello everyone,

I am back with a tiny contribution. Today I have created a nifty helper for codeigniter which uses the google translate api (v2).

In order to use the function you will require a google translation api key which you can get for free the google api console (https://code.google.com/apis/console).

After you have your key it has to be passed to the function along-with the text to be translated, the language from which it is being translated from and to the language it is translated to. The function returns the translated text which can be used to do further processing or can be also be echoed.

CODE:

  1. <?php
  2.  
  3. /* Develeoped by: faisal ahmed <thephpx(at)gmail.com–> */
  4.  
  5.         function google_translate($api_key, $text, $from_lang, $to_lang){
  6.                 $link = ‘https://www.googleapis.com/language/translate/v2?key=’.$api_key.‘&amp;source=’.$from_lang.‘&amp;target=’.$to_lang.‘&amp;q=’.$text;
  7.                 $response = file_get_contents($link);
  8.                 $array = json_decode($response);
  9.                 return $array->data->translations[0]->translatedText;
  10.         }
  11.  
  12. ?>

Hope to be back with another snippet soon, till then keep in touch :)

thanks,

thephpx

Share

codeigniter: facebook social plugin – like button helper

Hello everybody,

I have just created a short helper to create facebook like button dynamically for any content, by just giving-in the URL of the page. Hope this one also helps you like my other helpers :)

Helper function:

  1.  
  2. if(!function_exists(‘facebook_like’)){
  3.  function facebook_like($url){
  4.  $formlink=‘<iframe src="http://www.facebook.com/plugins/like.php?href=’;
  5.   $formlink .= urlencode($url);
  6.   $formlink .= ‘;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>’;
  7.                
  8.   return $formlink;
  9.   }
  10.  }
  11.  

Usage:

  1.  
  2. <?php
  3. $url = current_url();
  4. echo facebook_like($url);
  5. ?>
  6.  

Note:
As you can see the function requires you to use the codeigniter’s native URI helper.

Share

codeigniter: helper function to call library or model function in view

Hi I am back again,

This time with two helper function that lets you call any function from any model or library, given you already have the model or library loaded into the instance.

This one calls a library function with a single parameter being passed.

  1. if(!function_exists(‘call_lib_func’)){
  2.  function call_lib_func($library_name, $function_name, $parameters=null){
  3.   $ci = &amp;get_instance();
  4.   $ci->load->library($library_name);
  5.   return $ci->$library_name->$function_name($parameters);
  6.  }
  7. }

The next one calls a model function with a single parameter being passed.

  1. if(!function_exists(‘call_mod_func’)){
  2.  function call_mod_func($model_name, $function_name, $parameters=null){
  3.   $ci = &amp;get_instance();
  4.   $ci->load->model($model_name);
  5.   return $ci->$model_name->$function_name($parameters);
  6.  }
  7. }

As of now multiple parameter are not yet supported but I will soon post an extension of these two functions that will support multiple parameters. If you choose to use any of these functions feel free to use but a link back to my site is appreciated :)

thanks,

thephpx

Share

Codeigniter: Helper – Create breadcrumb from url

Breadcrumb is a navigational tool that developer often integrate in their application to give easy access to different segment of the application. This is used in applications which has multiple sequence of activities.

The following code will go into your helper file and you have to call create_breadcrumb(); function to get the formatted html text;

  1. if(!function_exists(‘create_breadcrumb’)){
  2. function create_breadcrumb(){
  3.   $ci = &amp;get_instance();
  4.   $i=1;
  5.   $uri = $ci->uri->segment($i);
  6.   $link = ‘<ul>’;
  7.  
  8.   while($uri != ”){
  9.     $prep_link = ”;
  10.   for($j=1; $j<=$i;$j++){
  11.     $prep_link .= $ci->uri->segment($j).’/';
  12.   }
  13.  
  14.   if($ci->uri->segment($i+1) == ”){
  15.     $link.=’<li>» <a href="’.site_url($prep_link).’"><b>’.$ci->uri->segment($i).’</b></a></li> ’;
  16.   }else{
  17.     $link.=’<li>» <a href="’.site_url($prep_link).’">’.$ci->uri->segment($i).’</a></li> ’;
  18.   }
  19.  
  20.   $i++;
  21.   $uri = $ci->uri->segment($i);
  22.   }
  23.     $link .= ‘</ul>’;
  24.     return $link;
  25.   }
  26. }
  27.  

If the current url is http://www.domain.com/user/add then the create_breadcrumb() function will return -

  1. » user » ad

Here user is linked to http://www.domain.com/user/ and add is linked to http://www.domain.com/user/add

This is generated by calling in the view file. Try it out and let me know how it went :)

thanks,

thephpx

[ad code=1 align=center]

Share

Codeigniter: Helper – absolute path helper

Recently I decided to give some more time to my blog and write some short but interesting posts. I will be writing some posts about custom helper functions that I have written which helps me in application development.

First on the list is a absolute path helper which creates an absolute path, relative to the base of the application. I call this function base_path(‘relative/url’);

  1. if(!function_exists(‘base_path’)){
  2.     function base_path($relative_path_location){
  3.         return base_url().$relative_path_location;
  4.     }
  5. }

The above is the code which will go into your helper file. The function formulates an absolute url from the given relative path based on base path of the application directory. It uses the existing base_url() helper function in codeigniter. So, you have make sure the URL helper is already loaded.

Example:

Suppose you want to tag an image which is located at “application-root/images/picture.jpg” to give its absolute URL in the img tag you can type as follows in your template/view file.

  1. <img src="<?php echo base_path(‘images/picture.jpg’);?>" alt="picture"/>

If you application is hosted at domain.com then it will give http://www.domain.com/images/picture.jpg as the returned path where http://www.domain.com/ is the base_url();

thanks,

thephpx

Share

PHP Simple HTML DOM Parser, Codeigniter Integration

Hi all,

I came across “Simple HTML DOM Parser” recently that allows jQuery like DOM selection. This function is quite handy to do some kungfoo DOM manipulation on the fly!

I needed to use it on codeIgniter so modified the files and integrated it into a single codeIgniter library. The original files can be accessed from here its sourceforge project page. For the codeigniter version of the library click here.

  1. $this->load->library(‘domparser’);
  2. $html = $this->domparser->file_get_html(‘http://www.google.com/’);
  3. $rank = $html->find(‘b.gb1′);
  4.  
  5. print_r($rank); // will show all content of tag with class gb1

For further utility functions of the DOM parser visit this link.

Feel free to comment or contact me directly for any query.

take care,

thephpx

Share

Staged Code-Igniter Web Application Development

Today I am going to share a little customization that I do while i develop codeigniter based web application. Now a days in most web application development firms a three-tire development process is followed. This is done to ensure smooth roll-out of the application from development to live.

The three-tire development methodology involves three stages of development servers. First comes the local server which is setup in your laptop or workstation. Then comes the internal test server, which is a central server that holds all the SVN repository as well as hosts the site and make it accessable to all from within the network. This server is also sometimes called staggin server. Last but not the least comes the live server, in most cases its your clients server where you upload your codes after they have passed the QA checks of both local-server and staging-server.

One related aspect of staged development is that in most cases they involve using SVN server for version control. Using SVN, though you can automatically update your server files from SVN server through “svn update” command but if the config file is not configured to take the different servers into consideration you will have to update the config file every time you do update on a server or set separate config file on each server and add the file to ignor list of the SVN repository.

Our way of doing it is to use a single config file and make it lookup the host address to determine which configuration settings to use.  If you are familiar with codeigniter then you already know you will find your config file at system/application/config/config.php.

Find the following line in the config.php file:

  1.  $config[‘base_url’] = "http://localhost/codeigniter/";

Replace the line with the following lines -

  1. if($_SERVER[‘HTTP_HOST’] == "localhost"){
  2.   $config[‘base_url’] = "http://localhost/codeigniter/";
  3. }else if($_SERVER[‘HTTP_HOST’] == "192.168.1.1"){
  4.   $config[‘base_url’] = "http://192.168.1.1/codeigniter/";
  5. }else{
  6.   $config[‘base_url’] = "http://www.live-server.com/codeigniter/";
  7. }

Now all your URL’s will be adjusted based on the server the site is accessed from. Next we will modify the database.php file which is also located in system/application/config/database.php

Find the following line(s) in the database.php file:

  1.  $db[‘default’][‘hostname’] = "localhost";
  2.   $db[‘default’][‘username’] = "root";
  3.   $db[‘default’][‘password’] = "";
  4.   $db[‘default’][‘database’] = "restobolivia";
  5.   $db[‘default’][‘dbdriver’] = "mysql";
  6.   $db[‘default’][‘dbprefix’] = "";
  7.   $db[‘default’][‘pconnect’] = TRUE;
  8.   $db[‘default’][‘db_debug’] = TRUE;
  9.   $db[‘default’][‘cache_on’] = FALSE;
  10.   $db[‘default’][‘cachedir’] = "";
  11.   $db[‘default’][‘char_set’] = "utf8";
  12.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";

Replace the above line with the following once:

  1. if($_SERVER[‘HTTP_HOST’] == "localhost"){
  2.  
  3.   $db[‘default’][‘hostname’] = "localhost";
  4.   $db[‘default’][‘username’] = "root";
  5.   $db[‘default’][‘password’] = "password";
  6.   $db[‘default’][‘database’] = "database_name";
  7.   $db[‘default’][‘dbdriver’] = "mysql";
  8.   $db[‘default’][‘dbprefix’] = "";
  9.   $db[‘default’][‘pconnect’] = TRUE;
  10.   $db[‘default’][‘db_debug’] = TRUE;
  11.   $db[‘default’][‘cache_on’] = FALSE;
  12.   $db[‘default’][‘cachedir’] = "";
  13.   $db[‘default’][‘char_set’] = "utf8";
  14.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";
  15.  
  16. }else if($_SERVER[‘HTTP_HOST’] == "192.168.1.1"){
  17.  
  18.   $db[‘default’][‘hostname’] = "localhost";
  19.   $db[‘default’][‘username’] = "root";
  20.   $db[‘default’][‘password’] = "password";
  21.   $db[‘default’][‘database’] = "database_name";
  22.   $db[‘default’][‘dbdriver’] = "mysql";
  23.   $db[‘default’][‘dbprefix’] = "";
  24.   $db[‘default’][‘pconnect’] = TRUE;
  25.   $db[‘default’][‘db_debug’] = FALSE;
  26.   $db[‘default’][‘cache_on’] = FALSE;
  27.   $db[‘default’][‘cachedir’] = "";
  28.   $db[‘default’][‘char_set’] = "utf8";
  29.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";
  30.  
  31. }else{
  32.  
  33.   $db[‘default’][‘hostname’] = "localhost";
  34.   $db[‘default’][‘username’] = "root";
  35.   $db[‘default’][‘password’] = "password";
  36.   $db[‘default’][‘database’] = "database_name";
  37.   $db[‘default’][‘dbdriver’] = "mysql";
  38.   $db[‘default’][‘dbprefix’] = "";
  39.   $db[‘default’][‘pconnect’] = TRUE;
  40.   $db[‘default’][‘db_debug’] = FALSE;
  41.   $db[‘default’][‘cache_on’] = FALSE;
  42.   $db[‘default’][‘cachedir’] = "";
  43.   $db[‘default’][‘char_set’] = "utf8";
  44.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";
  45.  
  46. }

The above code will give the server specific database configuration to the application based on the server it is accessed from.

Now save these two files and test your application you should be able to use the same config.php file in different servers if they are properly configured on config.php and database.php

For any further query feel free to leave a comment I will try my best to help you out.

take care,

thephpx

Share