Posts Tagged ‘ci’

Upload image and thumbnail generation in Code Igniter

Posted in post on June 15th, 2010 by Aditya – 3 Comments

Controller

  function index()
  {
    $this->load->model('Gallery_model');

    if($this->input->post('upload')){
      $this->Gallery_model->do_upload();
    }

    $this->load->view('gallery');

  }

view

    <?php 

      echo form_open_multipart('gallery');
      echo form_upload('userfile'); //userfile is default... dont bother changing
      echo form_submit('upload', 'Upload bebs!');
      echo form_close();
    ?>
class Gallery_model extends Model {

  var $gallery_path;
  var $gallery_path_url;

  function Gallery_model()
  {
    parent::Model(); // a must for constructor

    //APPPATH IS AVAILABLE IN THE ENTIRE CODEIG
    $this->gallery_path = realpath(APPPATH . '../images');
    $this->gallery_path_url = base_url().'images/'; //dunno for what
  }

  function do_upload()
  {
    $config = array(
      'allowed_types' => "jpg|jpeg|gif|png",
      'upload_path' => $this->gallery_path,
      'max_size'=>2000
    );
    $this->load->library('upload', $config);
    $this->upload->do_upload(); //do upload

    //want to create thumbnail

    $image_data = $this->upload->data(); //get image data

    $config = array(
      'source_image' => $image_data['full_path'], //get original image
      'new_image' => $this->gallery_path . '/thumbs', //save as new image //need to create thumbs first
      'maintain_ratio' => true,
      'width' => 150,
      'height' => 100

    );

    $this->load->library('image_lib', $config); //load library
    $this->image_lib->resize(); //do whatever specified in config
  }

}

Validation in Code Igniter

Posted in post on June 14th, 2010 by Aditya – 3 Comments

Using version 1.7.2 from one of the video sample

in controller…

  function create_member()
  {
    $this->load->library('form_validation');
    // field name, error message, validation rules

    $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
    $this->form_validation->set_rules('last_name', 'Last name', 'trim|required');
    $this->form_validation->set_rules('email_address', 'Email address', 'trim|required|valid_email');

    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('password2', 'Password confirmation', 'trim|required|matches[password]');

    if($this->form_validation->run() == FALSE )
    {

      $this->signup();

    }
    else
    {
      $this->load->model('membership_model');
      if($query = $this->membership_model->create_member())
      {
        $data['main_content'] = 'signup_successful';
        $this->load->view('includes/template', $data);
      }
      else
      {
        $this->load->view('signup_form');
      }
    }
  }

  function signup()
  {
    $data['main_content'] = 'signup_form';
    $this->load->view('includes/template', $data);
  }

in membership_model

<?php

class Membership_model extends Model {

  function validate()
  {
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));

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

    if($query->num_rows == 1)
    {
      //$this->load->model('membership_model');

      return true;
    }

  }

  function create_member()
  {
    $new_member_insert_data = array(
      'first_name' => $this->input->post('first_name'),
      'last_name' => $this->input->post('last_name'),
      'email_address' => $this->input->post('email_address'),
      'username' => $this->input->post('username'),
      'password' => md5($this->input->post('password'))
    );

    $insert = $this->db->insert('membership', $new_member_insert_data);
    return $insert;

  }
}

Pagination in Code Igniter

Posted in post on June 13th, 2010 by Aditya – 1 Comment

version used 1.7.2

in controller

  function index()
  {
    $this->load->library('pagination');
    $this->load->library('table');

    $this->table->set_heading('id', 'Title', 'The Content');

    $config['base_url']	= 'http://localhost/ci/index.php/site/index';
    $config['total_rows']	= $this->db->get('data')->num_rows();
    $config['per_page']	= 10;
    $config['num_links']	= 20;
    $config['full_tag_open']	= '<div id="pagination">';
    $config['full_tag_close']	= '</div>';

    $this->pagination->initialize($config);
    //db, limit , segment
    $data['records'] = $this->db->get('data', $config['per_page'], $this->uri->segment(3) );

    $this->load->view('site_view', $data);
  }

in view

<body>
  <div id="container">
    <h1>super pagination with code igniter</h1>
    <?php echo $this->table->generate($records); ?>
    <?php echo $this->pagination->create_links(); ?>
	</div>

</body>

from http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-7-pagination/

CodeIgniter Controller for Sending Email via Gmail SMTP

Posted in post on June 12th, 2010 by Aditya – 3 Comments
function Index()
	{
    $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'SuperAwesomeAccount@gmail.com',
      'smtp_pass' => 'SuperAwesomePassword'

    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n"); /* for some reason it is needed */

    $this->email->from('SuperAwesomeAccount@gmail.com', 'Aditya Lesmana Test');
    $this->email->to('SuperAwesomeReceiver@gmail.com');
    $this->email->subject('This is an email test');
    $this->email->message('it is working Darling :D ');

    if($this->email->send())
    {
      echo 'Your email was sent, dammit';
    }
    else
    {
      show_error($this->email->print_debugger());
    }

	}

notice that I load the email library everytime this class is called and not via application/config/autoload.php

$this->load->library('email', $config);

and pay attention to

    $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'SuperAwesomeAccount@gmail.com',
      'smtp_pass' => 'SuperAwesomePassword'

    );

can be moved (not copy, MOVED) more ood-ly into config/email.php (in this case CI will try to look for config for ‘email’ controller at config folder named ‘email’. Inside config/email.php the code wil turn something like

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'SuperAwesome@gmail.com';
$config['smtp_pass'] = 'Alamak';

in which you need to change controller/email.php, removing unnecessary $config

$this->load->library('email', $config);

to

$this->load->library('email');

the first line is to prevent unauthorized access while the rest is pretty self explanatory.

ps: the closing statment for php is not mandatory. Omitting them is considered best practice

via

http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-3

Ways to Retrieve Data from MySQL Database Using CodeIgniter

Posted in post on June 12th, 2010 by Aditya – 2 Comments

I use CI 1.7.2

This code should resides in model class. In short, this is the model you need to consider,

<?php
class Data_model extends Model {
/* we are going to use this getSome for example purpose */
	function getSome() {

/*all the code displayed below need to be inserted here */

/* standard row thing... its better to put this loop inside macros */
		if($q->num_rows() > 0) {
			foreach ($q->result() as $row) {
			    $data[] = $row;
			}
		return $data;
		}
	}
}

assuming q is the query and there is data table available somewher with id, title, author and contents column

First way, write yourself

$q = $this->db->query("SELECT * FROM data");

Second way, same sql query but with prepared statement to prevent sql injection.

 $sql = "SELECT title,author,contents FROM data WHERE id = ? AND author =?";
 $q = $this->db->query($sql, array(4,"Author two") );

Third way, use active record way-ish

$q = $this->db->get('data');

Fourth way, same like third way but with additional parameters specifying which column to retrieve. Should be faster than the second one.

$this->db->select('title, contents');
$q = $this->db->get('data');

Fifth way, same like fourth way but with more parameters specified separately for easier reading.

$this->db->select('title, contents');
$this->db->from('data');
$this->db->where('id', 1);
$q = $this->db->get();