Tag Archives: codeigniter

CodeIgniter vs Laravel

I’ve been using laravel for years already. Now I need to move to Laravel and I need strong reasons why I should.

Here I compiled some

  1. Laravel is truly OOP
  2. The ORM (eloquent) in Laravel is really powerful
  3. Can tune the speed with cache and queue
  4. Artisan command that really help doing job faster – e.g. creating controllers, models, run migration (database) etc.
  5. Migration that acts like database version control for the development team
  6. RESTful resource controllers
  7. Many available packages developed by other programmers that can be used
  8. More support, community, references. e.g. laracasts.com, laravel.io
  9. Powerful routes
  10. Class autoloading?

And below are the articles you may wan to read that are also my reference to the above points.

  1. laravel vs CI. A difficult choice

CodeIgniter Quick Reference

Some of useful functions but not all the time use. Will be a quick reference everytime want to use them.

URI segment

http://example.com/index.php/news/local/metro/crime_is_up
$this->uri->segment(3); //will produce: metro

DB SQL

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
        foreach ($query->result() as $row)
        {
                echo $row->title;
                echo $row->name;
                echo $row->body;
        }
}
$this->db->select('title, content, date');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$this->db->where('name !=', $name);
$this->db->where('title', $title);
$query = $this->db->get('mytable');

end

Sample use of CI query builder for CRUD

Sample insert

$data = array(
        'title' => $title,
        'name' => $name,
        'date' => $date
);

$this->db->insert('mytable', $data);

Sample update

$this->db->update('mytable', $data, "id = 4"); OR
$this->db->update('mytable', $data, array('id' => $id));

Sample delete

$this->db->delete('mytable', array('id' => $id));

Sample to get data

$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

More on getting data with CI

CI Query builder reference: CI2, CI3

Getting data from CI db query

1. All result set

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->title;
      echo $row->name;
      echo $row->body;
   }
}

2. Just one row

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   $row = $query->row(); 

   echo $row->title;
   echo $row->name;
   echo $row->body;
}

Reference: CI2, CI3

CodeIgniter template library

Why use template library for CI

  • You feel like using views can be clunky, especially when “embedding” views.
  • You don’t like calling header, footer, and other global views from every Controller method.
  • You prefer having one “master template” that can be changed for any controller in order to meet unique application design needs.
  • You don’t want to drastically alter the way you interface controllers and views.
  • You like clear, thorough documentation on par with CodeIgniter’s User Guide.

source: http://jeromejaglale.com/doc/php/codeigniter_template

or alternative download