CodeIgniter Class Reference
Jamshid Hashimi
Trainer, Cresco Solution
http://www.jamshidhashimi.com
jamshid@netlinks.af
@ja...
Agenda
• Database Class
• Calendar Class
• Config Class
• Email Class
• File Uploading Class
• Form Validation Class
Database Class
• CodeIgniter comes with a full-featured and
very fast abstracted database class that
supports both traditi...
Database Class
• Configuration
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';...
Database Class
• Standard Query
• Result
$query = $this->db->query('SELECT
name, title, email FROM my_table');
$query->res...
Database Class
• Testing Result
• Single Result
if ($query->num_rows() > 0)
$query = $this->db->query('SELECT name FROM
my...
Database Class
• Affected Rows
$sql = "INSERT INTO mytable (title, name)
VALUES (".$this->db->escape($title).", ".$this-
>...
Database Class
• Query Helper Functions
$this->db->insert_id();
$this->db->affected_rows();
$this->db->count_all();
$this-...
Database Class
• Active Record – Selecting Data
• Selecting Data – LIMIT
$query = $this->db->get('mytable');
// Produces: ...
Database Class
• Selecting Data – Result
• get_where()
$query = $this->db->get('mytable');
foreach ($query->result() as $r...
Database Class
• $this->db->select();
• $this->db->select_max();
• $this->db->select_min();
• $this->db->select_avg();
• $...
Database Class
• $this->db->or_where();
• $this->db->where_in();
• $this->db->or_where_in();
• $this->db->where_not_in();
...
Database Class
• $this->db->group_by();
• $this->db->distinct();
• $this->db->having();
• $this->db->or_having();
• $this-...
Database Class
• Inserting Data
– $this->db->insert();
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date'...
Database Class
• Inserting Data
– $this->db->insert_batch();
$data = array(
array(
'title' => 'My title' ,
'name' => 'My N...
Database Class
• Updating Data
– $this->db->update();
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
)...
Database Class
• Updating Data
– $this->db->update_batch();
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Na...
Database Class
• Deleting Data
– $this->db->delete();
– $this->db->truncate();
• Method Chaining
$this->db->where('id', $i...
DEMO
Calendar Class
• The Calendar class enables you to dynamically
create calendars. Your calendars can be
formatted through t...
DEMO
Config Class
• The Config class provides a means to retrieve
configuration preferences.
$this->config->item('item name');
...
DEMO
Email Class
• Multiple Protocols: Mail, Sendmail, and SMTP
• Multiple recipients
• CC and BCCs
• HTML or Plaintext email
•...
Email Class
Email Class
$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@...
Email Class
• Send email with Gmail
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => '...
Email Class
• Initialize Configuration
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$conf...
Email Class
• Attachments
$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this-...
File Upload Class
• CodeIgniter's File Uploading Class permits files
to be uploaded. You can set various
preferences, rest...
File Upload Class
• Initializing Configurations
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg...
DEMO
Form Validation Class
• CodeIgniter provides a comprehensive form
validation and data prepping class that helps
minimize t...
Form Validation Class
DEMO
Assignments
QUESTIONS?
Upcoming SlideShare
Loading in …5
×

CodeIgniter Class Reference

3,035 views

Published on

Useful class references in CodeIgniter

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,035
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
68
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • $this->db->like(‘title’,’match’,’after’);$this->db->like(‘title’,’match’,’before’);$this->db->like(‘title’,’match’,’both’);$this->db->like(‘title’,’match’,’none’);
  • $this->db->order_by('title desc, name asc');
  • Max sizes in KB
  • CodeIgniter Class Reference

    1. 1. CodeIgniter Class Reference Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
    2. 2. Agenda • Database Class • Calendar Class • Config Class • Email Class • File Uploading Class • Form Validation Class
    3. 3. Database Class • CodeIgniter comes with a full-featured and very fast abstracted database class that supports both traditional structures and Active Record patterns. The database functions offer clear, simple syntax. • Usage Examples $this->load->database(); $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);
    4. 4. Database Class • Configuration $active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'sakila'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE;
    5. 5. Database Class • Standard Query • Result $query = $this->db->query('SELECT name, title, email FROM my_table'); $query->result() //Object $query->result_array() //Array
    6. 6. Database Class • Testing Result • Single Result if ($query->num_rows() > 0) $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); $row1 = $query->row(); $row2 = $query->row_array(); echo $row1->name; echo $row2[‘name’];
    7. 7. Database Class • Affected Rows $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this- >db->escape($name).")"; $this->db->query($sql); echo $this->db->affected_rows();
    8. 8. Database Class • Query Helper Functions $this->db->insert_id(); $this->db->affected_rows(); $this->db->count_all(); $this->db->platform(); $this->db->version(); $this->db->last_query();
    9. 9. Database Class • Active Record – Selecting Data • Selecting Data – LIMIT $query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable $query = $this->db->get('mytable', 10, 20); // Produces: SELECT * FROM mytable LIMIT 20, 10
    10. 10. Database Class • Selecting Data – Result • get_where() $query = $this->db->get('mytable'); foreach ($query->result() as $row) { echo $row->title; } $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
    11. 11. Database Class • $this->db->select(); • $this->db->select_max(); • $this->db->select_min(); • $this->db->select_avg(); • $this->db->select_sum(); • $this->db->from(); • $this->db->where(); • $this->db->join();
    12. 12. Database Class • $this->db->or_where(); • $this->db->where_in(); • $this->db->or_where_in(); • $this->db->where_not_in(); • $this->db->or_where_not_in(); • $this->db->like(); • $this->db->or_like(); • $this->db->not_like(); • $this->db->or_not_like();
    13. 13. Database Class • $this->db->group_by(); • $this->db->distinct(); • $this->db->having(); • $this->db->or_having(); • $this->db->order_by();
    14. 14. Database Class • Inserting Data – $this->db->insert(); $data = array( 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date' ); $this->db->insert('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
    15. 15. Database Class • Inserting Data – $this->db->insert_batch(); $data = array( array( 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date' ), array( 'title' => 'Another title' , 'name' => 'Another Name' , 'date' => 'Another date' ) ); $this->db->insert_batch('mytable', $data);
    16. 16. Database Class • Updating Data – $this->db->update(); $data = array( 'title' => $title, 'name' => $name, 'date' => $date ); $this->db->where('id', $id); $this->db->update('mytable', $data);
    17. 17. Database Class • Updating Data – $this->db->update_batch(); $data = array( array( 'title' => 'My title' , 'name' => 'My Name 2' , 'date' => 'My date 2' ), array( 'title' => 'Another title' , 'name' => 'Another Name 2' , 'date' => 'Another date 2' ) ); $this->db->update_batch('mytable', $data, 'title');
    18. 18. Database Class • Deleting Data – $this->db->delete(); – $this->db->truncate(); • Method Chaining $this->db->where('id', $id); $this->db->delete('mytable');
    19. 19. DEMO
    20. 20. Calendar Class • The Calendar class enables you to dynamically create calendars. Your calendars can be formatted through the use of a calendar template, allowing 100% control over every aspect of its design. In addition, you can pass data to your calendar cells. echo $this->calendar->generate(); echo $this->calendar->generate(2006, 6);
    21. 21. DEMO
    22. 22. Config Class • The Config class provides a means to retrieve configuration preferences. $this->config->item('item name'); $this->config->set_item('item_name', 'item_value');
    23. 23. DEMO
    24. 24. Email Class • Multiple Protocols: Mail, Sendmail, and SMTP • Multiple recipients • CC and BCCs • HTML or Plaintext email • Attachments • Word wrapping • Priorities • BCC Batch Mode, enabling large email lists to be broken into small BCC batches. • Email Debugging tools
    25. 25. Email Class
    26. 26. Email Class $this->load->library('email'); $this->email->from('your@example.com', 'Your Name'); $this->email->to('someone@example.com'); $this->email->cc('another@another-example.com'); $this->email->bcc('them@their-example.com'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); $this->email->send(); echo $this->email->print_debugger();
    27. 27. Email Class • Send email with Gmail $this->load->library('email'); $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'youremail@gmail.com', 'smtp_pass' => 'yourpassword', );
    28. 28. Email Class • Initialize Configuration $config['protocol'] = 'sendmail'; $config['mailpath'] = '/usr/sbin/sendmail'; $config['charset'] = 'iso-8859-1'; $config['wordwrap'] = TRUE; $this->email->initialize($config);
    29. 29. Email Class • Attachments $this->email->attach('/path/to/photo1.jpg'); $this->email->attach('/path/to/photo2.jpg'); $this->email->attach('/path/to/photo3.jpg'); $this->email->send();
    30. 30. File Upload Class • CodeIgniter's File Uploading Class permits files to be uploaded. You can set various preferences, restricting the type and size of the files. $this->upload->do_upload(); $this->upload->display_errors(); $this->upload->data();
    31. 31. File Upload Class • Initializing Configurations $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config);
    32. 32. DEMO
    33. 33. Form Validation Class • CodeIgniter provides a comprehensive form validation and data prepping class that helps minimize the amount of code you'll write. <?php echo validation_errors(); ?> $this->form_validation->run()
    34. 34. Form Validation Class
    35. 35. DEMO
    36. 36. Assignments
    37. 37. QUESTIONS?

    ×