Starting Out

So, like everyone else, why write code when you can steal it.. or Borrow it..

The website I’m rewriting is a maintenance website – used for maintaining a public facing website. It has a login, is menu driven and most of the menu options are Create, Read, Update, Delete (CRUD) programs. So I went looking for examples of CRUD driven code on CI..

I found a few – and like with most everything I found so far, the examples are all running on a WAMP package on Windows.. OK.. I used to do that – on a Windows 2000 laptop. Then I set up an old PC with Ubuntu and a LAMP stack for development. Now I just carve out some space on one of the sites I have. But OK, you want to use Windoze, go for it.

What I have now is mostly based on ajaxCRUD ( and basically the PHP script is a configuration specifying the table, columns, options, etc. I would like to be able to port all that over to CI, but of course I would like to maybe find an example or two.

All the examples I’ve found so far use Bootstrap and yeah, I could go that route. But, I digress. This is supposed to be a bit of a review on what I’ve found so far in installing CI.

The CI website is pretty good and has good documentation on the basics. When it doubt, RTFM. But I didn’t see much (so far) in the documentation on how to incorporate JS, CSS and images in your website. I’m not going to into the basics of installing CI. You can Google that. But, when you have it installed you will have an “applications” folder and a “system” folder. You may want to create an “assets” folder and inside that have sub-folders for css, js and images.

You may also want to organize your MVC structure under folders in the /application/models, /application/views and /application/controllers folders, especially if your application has a lot different functionality. You don’t have to, but it helps with organization.

I’ve setup CI on a couple of different hosts and had problems on one that I did not have on the other so I’m going to list a few hints.

(A) Don’t change anything in the “system” folder. All your code changes should be in the “applications” folder. If you need to make changes to CI, extend the classes and put your code in the appropriate sub-fulder under “applications”.

(B) If you get an error about the default timezone, add the following in your application/config/config.php file after the “defined(‘BASEPATH’) OR exit()” line:
date_default_timezone_set(‘America/New_York’); (or whatever time zone you are using)
There may be a better way but this is a simple fix.

(C) During development, turn on full logging. In application/config/config.php in the logging section, set the log threshold value to “4”. Yeah, it generates a lot but it helps a lot too.

(D) Log entries should be one per line in the log files. I ran into an issues where they did not have the EOL character and the log was basically unreadable. The Log class appends the PHP_EOL constant to each log entry. On the system where I had problems, the PHP_EOL was a “newline” ( ASCII code 10 or chr(10) or \n ) but what I really needed was newline followed by return (\n\r). Solution was to add a new value (log_eol) in application/config/config.php
$config[‘log_eol’] = “\r\n”;
and then extent the Log class by adding MY_Log.php to application/core with the following code:

(Comments excluded for brevity!)

defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Log extends CI_Log {
	protected function _format_line($level, $date, $message){
		$config =& get_config();
		return $level.' - '.$date.' --> '.$message.$config["log_eol"];

Basically, this is just overriding the _format_line method to use whatever you define in the $config array for the EOL for the log entries. Worked a treat.

(E) Set up the “base_url” value in application/config/config.php to whatever the URL of your site is. You don’t really have to – but if you use the URL “helper” you will really need to define the base url – otherwise CI will use the IP address of your server.

(F) To go along with the previous item, the “base_url()” function returns the base url with whatever URI you pass in as an argument. However, the “site_url()” function returns the base url/index.php with the URI you pass in. Keep that in mind. For example, in the view for the head section of your webpage you could use:
<?php echo base_url(‘assets/css/style.css’); ?> to generate the URL for your css file but for references to pages in your website (like for a button or link) you should use:
<?php echo site_url(‘folder/class/method’); ?> as it will insert index.php in the URL as needed.

(G) AUTOLOAD – If your application does a lot of database access and has buttons and form validation you will probably want to autoload libraries and helpers, rather than load them in each class.
See application/config/autoload.php

Leave a Reply

Your email address will not be published. Required fields are marked *