Date post: | 20-Aug-2015 |
Category: |
Technology |
Upload: | jamshid-hashimi |
View: | 2,591 times |
Download: | 0 times |
CodeIgniter Class Reference
Jamshid HashimiTrainer, Cresco Solution
http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi
Afghanistan Workforce Development Program
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 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);
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;
Database Class
• Standard Query
• Result
$query = $this->db->query('SELECT name, title, email FROM my_table');
$query->result() //Object$query->result_array() //Array
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’];
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();
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();
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
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);
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();
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();
Database Class
• $this->db->group_by();• $this->db->distinct();• $this->db->having();• $this->db->or_having();• $this->db->order_by();
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')
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);
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);
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');
Database Class
• Deleting Data– $this->db->delete();
– $this->db->truncate();• Method Chaining
$this->db->where('id', $id);$this->db->delete('mytable');
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);
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');
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
Email Class
$this->load->library('email');$this->email->from('[email protected]', 'Your Name');$this->email->to('[email protected]'); $this->email->cc('[email protected]'); $this->email->bcc('[email protected]'); $this->email->subject('Email Test');$this->email->message('Testing the email class.');$this->email->send();echo $this->email->print_debugger();
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' => '[email protected]','smtp_pass' => 'yourpassword',
);
Email Class
• Initialize Configuration
$config['protocol'] = 'sendmail';$config['mailpath'] = '/usr/sbin/sendmail';$config['charset'] = 'iso-8859-1';$config['wordwrap'] = TRUE;
$this->email->initialize($config);
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();
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();
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);
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()