Running CodeIgniter from Cron/CLI

Many of us faced the problem of running CodeIgniter controllers from Cron or from Command Line. usually we write the following codes to run a php file from cron/CLI.

php /var/www/path/to/your/script

but if we give CodeIgniter Controller path here it will not work since CodeIgniter will fail to initialize its core classes. A work around of this is running the script using CURL

curl http://yourwebsite/controllername

the problem with the above command is that it takes too much CPU resources .

Today my collegue Hasan showed me a nice way of doing it, just write the following codes in a php file in your root folder. here is the way to go

<?
$_GET[“/external/do_cron”] = null;
require “index.php”;
?>

note: here “external” is the controller name and “do_cron” is the function name that you want to execute.

now save this file to any name you want (eg. mycron.php) and run the script using

php /var/www/path/to/script

[here it is php /var/www/mywebsite/mycron.php]

if the above code does not work, you can try the following one. (thanks to sucio)

$_SERVER[“REQUEST_URI”] =”external/do_cron”;
require “index.php”;

Advertisements

Installing CodeIgniter on IIS

I faced a serious problem during deploying one of my CodeIgniter application to a windows server running on IIS. After installing CodeIgniter the index.php page was not visible at all. I tried many ways to configure it but it does not show anything. A problem with SEF (search engine friendly) URL CodeIgniter follows. I searched the internet but couldn’t find a suitable solution or answer to the problem. After two day I found a very good and step by step solution for this problem.  You can have a look at it in the following URL.

Installing CodeIgniter on IIS

Code Igniter & maani.us

Many of us feels that a report without a graph is totally incomplete. I also agree with it completely. We need to represent the data in reports to be meaningful and also representable. A table with rows of data does not give the reader a clear idea about what is actually happening. So it is better to show a chart or a graph to the user before showing the long tables of data.

In PHP, one of the most popular graph tool is php/swf charts from maani.us. This tool provides some cool looking graphs with easy to integrate with your application. The integration becomes much easier if you are using Oode Igniter. In this article I am going to describe you, how to integrate maani.us php/swf charts with your Code Igniter application.

Detail Steps to Install the application:

1. download php/swf charts from http://www.maani.us

2. Create a directory named charts or any appropriate name in your root folder (not inside the Code Igniter system folder). it is better if you keep the CI system folder and charts folder on same level.

3. put the unzipped content (except the charts.php) file of the downloaded file in this folder.

Writing Code Igniter code:

You can integrate the maani.us code by either creating a library in Code Igniter or by simply creating a helper. I am going to show you the process by creating a helper file. In order to create a helper file in Code Igniter, you can either create a helper file in system\helpers or system\application\helpers file. In the later case you have to create the directory as Code Igniter does not create it by default. It is always better to keep the core files and folders of Code Igniter to be intact. Code Igniter gives you the flexibility to extend any of its helpers, libraries, plugins etc to be extended from the application folder.

here are the detail steps for creating and showing graph in a view:

1. first create a helper named graph_helper.php and copy paste the content of charts.php in this file. save the file.

2. now load the helper file from your controller where you want to show the graph. there are two parts in the helper file. One for inserting the chart and in order to show a chart, we need to define the data and chart properties. the SendChartData function defines the properties of the chart and InsertChart shows the Data. in InsertChart function we have to define few things

  • location of the flash file (.swf) , in our case in the charts folder in base directory.
  • location of the charts library, in our case in the charts folder in base directory.
  • PHP source location for ChartData or SendChartData function, in our case a controller.
  • width, height, background color, transparency and license information

here is the code inside a controller looks like.

$this->load->helper(“graph”);
$url = site_url().”/reports/showsummary/”;
$charturl = base_url().”/charts/charts.swf”;
$chartlib = base_url().”/charts/charts_library”;

$data[‘chart’] = InsertChart( $charturl, $chartlib, $url ,650,300,”cccccc” ); // passing the chart information to view.

$this->load->view(“reports/dashboard”, $data);
now we have to define the controller which will send the data for the graph.

function showsummary()

{

$this->load->helper(“graph”);
$chart[ ‘axis_category’ ] = array ( ‘font’=>”arial”, ‘bold’=>true, ‘size’=>16, ‘color’=>”000000″, ‘alpha’=>60, ‘skip’=>0 ,’orientation’=>”vertical” );
$chart[ ‘axis_ticks’ ] = array ( ‘value_ticks’=>false, ‘category_ticks’=>false, ‘major_thickness’=>2, ‘minor_thickness’=>1, ‘minor_count’=>1, ‘major_color’=>”222222″, ‘minor_color’=>”222222″ ,’position’=>”centered” );
$chart[ ‘axis_value’ ] = array ( ‘font’=>”arial”, ‘bold’=>true, ‘size’=>10, ‘color’=>”000000″, ‘alpha’=>50, ‘steps’=>6, ‘prefix’=>””, ‘suffix’=>””, ‘decimals’=>0, ‘separator’=>””, ‘show_min’=>true );

$chart[ ‘chart_border’ ] = array ( ‘color’=>”000000″, ‘top_thickness’=>0, ‘bottom_thickness’=>0, ‘left_thickness’=>5, ‘right_thickness’=>0 );

$chart[ ‘chart_grid_h’ ] = array ( ‘alpha’=>10, ‘color’=>”000000″, ‘thickness’=>0 );
$chart[ ‘chart_grid_v’ ] = array ( ‘alpha’=>10, ‘color’=>”000000″, ‘thickness’=>20 );
$chart[ ‘chart_rect’ ] = array ( ‘x’=>50, ‘y’=>50, ‘width’=>540, ‘height’=>200, ‘positive_color’=>”ffffff”, ‘positive_alpha’=>30, ‘negative_color’=>”ff0000″, ‘negative_alpha’=>10 );
$chart[ ‘chart_type’ ] = “line”;

$chart[ ‘chart_data’ ] = array ( array ( “”, “2004”, “2005”, “2006”, “2007” ), array ( “region 1”, 48, 55, 80, 100 ), array ( “region 2”, -12, 10, 55, 65 ), array ( “region 3″, 27, -20, 15, 80) );

$chart[ ‘chart_value’ ] = array ( ‘prefix’=>””, ‘suffix’=>””, ‘decimals’=>0, ‘separator’=>””, ‘position’=>”cursor”, ‘hide_zero’=>true, ‘as_percentage’=>false, ‘font’=>”arial”, ‘bold’=>true, ‘size’=>12, ‘color’=>”000000″, ‘alpha’=>100 );
$chart[ ‘draw’ ] = array ( array ( ‘type’=>”text”, ‘color’=>”000033″, ‘alpha’=>25, ‘font’=>”arial”, ‘rotation’=>0, ‘bold’=>true, ‘size’=>30, ‘x’=>0, ‘y’=>5, ‘width’=>650, ‘height’=>295, ‘text’=>”Statistics”, ‘h_align’=>”right”, ‘v_align’=>”bottom” ) );
$chart[ ‘legend_label’ ] = array ( ‘layout’=>”horizontal”, ‘font’=>”arial”, ‘bold’=>true, ‘size’=>13, ‘color’=>”000000″, ‘alpha’=>50 );
$chart[ ‘legend_rect’ ] = array ( ‘x’=>25, ‘y’=>10, ‘width’=>600, ‘height’=>5, ‘margin’=>3, ‘fill_color’=>”ffffff”, ‘fill_alpha’=>10, ‘line_color’=>”000000″, ‘line_alpha’=>0, ‘line_thickness’=>0 );
$chart[ ‘series_color’ ] = array ( “ddaa41”, “88dd11”, “4e62dd”, “ff8811”, “8A2BE2”, “FF1493”, “F0E68C”, “DDA0DD”,”8B4513″,”FFFF00″,”FF6347″,”708090″,”B0E0E6″,”808000″, “20B2AA”, “FF8C00”, “338833” );
$chart[ ‘series_gap’ ] = array ( ‘bar_gap’ => 0, ‘set_gap’ => 35 );

SendChartData ($chart);
}

You can copy paste this part from maani.us graph examples according to your graph choice. Just copy the first line

$this->load->helper(“graph”);

when you run the changes, you will see that the graphs are generated and it is shown in the view as expected. I have shown how to put a chart in a variable and pass it to view. It is a good way to show more than one graph if required.

I hope that shows how easily you can add a cool featured graph tool in your Code Igniter application. Let me know any comments, questions regarding Code Igniter and PHP.

www.phpmytips.com coming soon

for last few days i am working on my next project http://www.phpmytips.com, a complete question and answer site. after getting very good positive response from my php mysql interview question sections in this site, i have decided to build phpmytips.com for everyone where everyone can participate and contribute. here are the rough features i have in my mind to implement

1. anyone can add or answer question

2. ratings on answers and questions to decide popularity.

3. user ranking system

4. ability to tag question

5. watch a particular question or category

6. add tips (you will give the tips) to the site

7. favorite question, top answers, unanswered section.

8. ability to add in social book marking sites

9. rss feeds.

10. group question and answers or tips such as interview tips or questions (can be be hundreds)

11. email a friend functionality

12. pdf export (still thinking about it)

13. comments on pages.

so if you guys have anything else to add then please feel free to append on this post. i will prioritize them and implement them accordingly. i have a plan to release the beta version with minimum functionality within mid November. btw, i am using PHP, MySQL, CodeIgniter, jQuery, XHTML

so the countdown begins..

Code Igniter: Installation and First Run

Code Igniter is an PHP Application Development Framework – a toolkit for PHP developers. The main goal of Code Igniter is to enable developers to develop projects much faster than they could have done ( if they have started from scratch) by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries.

You can download Code Igniter from http://www.codeigniter.com/

How to Install:

1. Download the zip file from the above URL.

2. unzip the files and put them to your webserver where you want to. lets assume that you put it at codeigniter folder of your web server.

3. Open the application/config/config.php file with a text editor and set your base URL. lets asumme that your base URL is set like this:

$config[‘base_url’] = “http://localhost/codeigniter/&#8221;;

4. if you intend to use a database, open the application/config/database.php file with a text editor and set your database settings. Set the following variables according to your setup.

$db[‘default’][‘hostname’] = “localhost”;
$db[‘default’][‘username’] = “admin”;
$db[‘default’][‘password’] = “*****”;
$db[‘default’][‘database’] = “projectone”;
$db[‘default’][‘dbdriver’] = “mysql”;
$db[‘default’][‘dbprefix’] = “”;
$db[‘default’][‘active_r’] = TRUE;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = “”;

5. now you are ready to run our installed piece of software. in order to run Code Igniter you have to visit the following URL in you web server.

http://localhost/codeigniter/index.php/

if your setup is correct then you will see a welcome page with heading

“Welcome to Code Igniter!”

Now you are ready to work with Code Igniter.

Note: if you try to access the Code Igniter installation like the following then you will definitely get an error message.

http://localhost/codeigniter/

http://localhost/codeigniter/index.php

By default Code Igniter follows (Search Engine Friendly (SEF) URL and for this they use segment based URL which will come to next tutorial.