Wednesday, November 26, 2014

How To Handle Errors in Codeigniter?

CodeIgniter lets you build error reporting into your applications using the functions such as..
show_error('message' [, int $status_code= 500 ] )
This function will display the error message supplied to it using the following error template: application/errors/error_general.php

show_404('page' [, 'log_error'])
This function will display the 404 error message supplied to it using the following error template: application/errors/error_404.php

log_message('level', 'message')
This function lets you write messages to your log files. You can three level options:  error, debug and info.
By default, on a clean install, CI will display ALL php errors of all severity. So if you make a mistake in your PHP coding, you'll see the errors in your browser..

CI uses PHP's error_reporting() function to define the level of error reporting, and you can find this in the main index.php file in the root of your CI install. It’s the first function you see.

Instead of E_ALL, you can change it to any of the predefined error constant that PHP understands to suit your needs. Obviously, once your site goes live, you should change from E_ALL to E_ERROR to show only messages for fatal run time problems. This will hide all php errors except those that will halt your script execution.

How to hide database error?
You can hide the database error by setting db_debug to FALSE in application/config/database.php.
It will display blank page if any database error will occur instead of displaying any kind of database table name or error on site.
  $db['default']['db_debug'] = FALSE; 
You can easily redirect to some other error page instead of showing blank page as follows.

$query = $this->db->get('your_table');  
   show_error("This is where your error message will appear surrounded by this default template. ", 500 );

When you call show_error(), you will generate page that looks like this. The layout can changed by modifying this file application/errors/error_general.php)

That's it. Feel free to comment.

Tuesday, November 25, 2014

How To Perform Multiple Tags Search Query using PHP, MYSQL & CodeIgniter?

I working on a tag based search. I have three tables tag(id,name), tagXmedia(id,tag_id,media_id), and media(id,...).
tagXmedia is the mapping table between the tag and media tables. This is a one to many relationship.
For instance I need to be able to search for an entry in the media table that is associated with both the "home" and "hawaii" tags.
SELECT media_id
FROM tagXmedia
WHERE tag_id IN (SELECT id FROM tag WHERE name IN ('home','hawaii'))
GROUP BY media_id
HAVING COUNT(tag_id) = 2;
If you wish to have it match more than just two tags, you can easily add them. Just remember to change the 2 in the HAVING clause.

Perform Multiple Tags Search using URL Parameters and Codeigniter
Within the query string, parameters (filters) are separated by ampersands (&), and multiple values per parameter are separated by commas. For example:

Here is the Codeigniter active-record query for performing multiple tags using comma separated values. Table structure are explained above.

  $this->db->select(', media.*');
  $this->db->join('tagXmedia', ' = tagXmedia.media_id', 'left');
  $this->db->join('tags', ' = tagXmedia.tag_id', 'left');

  $tags = explode(',',$tags);
  $a_tag  = array_unique($tags);
  $tag_count = count($a_tag);
  $this->db->where_in('', $a_tag);     
  $count = "COUNT(tagXmedia.tag_id) = $tag_count ";

  $query = $this->db->get('media');


Thursday, November 20, 2014

Which IDE To Use When Coding in CodeIgniter?

People use various IDE to code in CodeIgniter (CI) depending on one's need. It is hard to tell which IDE is best to use to code in CI. I have been using Coda and Sublime text 2. It serve my purpose.
Some of the popular IDE which I have heard are
Netbeans 7.0
zend studio
JetBrains PhpStorm
phpDesigner 8
If you have any suggestion about other IDE's , do share.

Friday, November 14, 2014

How To Join Multiple Tables in Codeigniter?

By using CodeIgniter's Active Record class, you can just use the join method multiple times to join multiple tables.
$this->db->join('table2', 'table2.ID = table1.ID');
$this->db->join('table3', 'table3.ID = table1.ID');
Permits you to write the JOIN portion of your query. If you need a specific type of JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.
$this->db->join('table4', 'table4.ID = table1.ID', 'left');
// Produces: LEFT JOIN table4 ON table4.ID = table1.ID

Read more detail on