Codeigniter
Contents
- 1 Pagination Helper
- 2 Pagination per page drop down initialization
- 3 Multiple File Upload
- 4 Disable database errors
- 5 Disable warnings
- 6 Codeigniter and UTF-8
- 7 addslashes, stripslashes in Codeigniter
- 8 Codeigniter Cron
- 9 Multiple image resize issue
- 10 Captcha Helper
- 11 Allowing all base64 encoding characters in URI of codeigniter
- 12 URI SEGMENT Replacing DOT ( . ) to underscore ( _ )
- 13 Image Resize function
- 14 Image resize proportional
- 15 Create Unique URL Slug
- 16 Create unique URL slug - Updated version
Pagination Helper
function paginate($t,$num,$table) { $pcon['base_url'] = base_url().'/admin/property/page/'; $pcon['total_rows'] = $t->db->count_all($table); $pcon['per_page'] = 10; $pcon['num_links'] = 50; $pcon['uri_segment'] = 4; $pcon['full_tag_open'] = '<p>'; $pcon['full_tag_close'] = '</p>'; $pcon['cur_tag_open'] = ' <b>'; $pcon['cur_tag_close'] = '</b>'; $pcon['next_link'] = 'Next'; $pcon['prev_link'] = 'Prev'; $t->pagination->initialize($pcon); $ret = $t->db->get($table, $pcon['per_page'], $num); return $ret; }
function index() { $ret = paginate($this,0,'property'); } function page($num=0) { $ret = paginate($this,$num,'property'); }
when the page loads for example http://site/listings/ then the index function will be called and by default 0 is sent as the initial page number.
when something like this is called http://site/listings/page/20 then the number 20 is sent as parameter to the page function
The $ret var will have the result row object which you can assign to load->view('template',$ret)...
Pagination per page drop down initialization
For my reference... For functions referred here see javascript section.
function init_pagination_perpage() { var perpages = Array('10','20','30','40','50') var selo = document.getElementById('perpagesel') for(i in perpages)selo.options.add(new Option(perpages[i],perpages[i])) selo.options.add(new Option('All','10000')); selo.onchange = function() { setCookie('paginationPerPage',this.value,10); window.location.reload(); } var nowperpage = parseInt(getCookie('paginationPerPage')) if(isNaN(nowperpage))nowperpage = 10; setjsSelectedText(nowperpage,'perpagesel') setCookie('PaginationPerPage',10,10); }
Multiple File Upload
The following is missing some thing and i am correcting it... [23/03/2010]
function savefoto() { $err=NULL; $config['upload_path'] = docroot.addpath.'static/cimages/'; $config['allowed_types'] = 'jpg|gif|png'; $this->load->library('upload'); $errors = ''; $fa = $_FILES; $_FILES = NULL; foreach($fa as $osa) { $_FILES['name'] = $osa['name'][0]; $_FILES['type'] = $osa['type'][0]; $_FILES['tmp_name'] = $osa['tmp_name'][0]; $_FILES['size'] = $osa['size'][0]; $_FILES['error'] = $osa['error'][0]; $osa = $_FILES; $_FILES = NULL; $_FILES['fotoimage'] = $osa; $config['file_name'] = 'prefix_'.uniqid('8').$_FILES['fotoimage']['name']; $this->upload->initialize($config); if(!$this->upload->do_upload('fotoimage')) $err = "Some images were not able to be processed sucessfully"; } $this->session->set_flashdata('err',$err); redirect('admin/company/gallery/'); }
Disable database errors
Go to the root/system/config folder and open the database.php file. Find the following and make it false.
$db['default']['db_debug'] = FALSE;
Disable warnings
This is actually what I ended up doing and I hate to edit core system files but sometimes I have to which means then I have to document those changes and it makes upgrading more tense bc I have to reproduce these changes.
Anyways in Common.php I made this change in the _exception_handler() function:
#if ($severity == E_STRICT) if ($severity == E_STRICT OR $severity == E_NOTICE)#jay added to remove E_NOTICE errors from log
And this kills the notice warnings. Now you php 4 users aren’t affected by a ton of warnings. That’s bc php 5 now sends a warning if you don’t declare variables before you use them and I basically ignore that so it would flood my log files without this change. Now I can turn on logging.
Codeigniter and UTF-8
I had some problems with codeigniter and utf-8. These can be followed to rectify.
- http://www.davidbehler.de/index.php/post/view/25 - Must see
- http://hash-bang.net/2009/02/utf8-with-codeigniter/ - Must see
- http://codeigniter.com/forums/viewthread/129389/#639831
- http://codeigniter.com/forums/viewthread/104068/
addslashes, stripslashes in Codeigniter
How to automatically addslashes and stripslashes in codeigniter and its issues. This is a forum thread which discusses on the above.
Codeigniter Cron
Goto control panel cron option. Create a new cron and in the path input field give the following.
Example
php /home/user/public_html/subfolder/static/cron.php --run=/admincontrol/newsletter_cron --show-output
You have to give the above in the control panel -> cron jobs - Path to execute option. and then set the interval.
If you want to test it in localhost then go to the php directory or give the full php path and then the path of the cron.php with arguments. So goto start -> run -> type cmd and then type like in the following
d:\xampp\php\php.exe d:\xampp\htdocs\folder\static\cron.php --run=/admincontrol/newsletter_cron --show-output
Note:
- If you are using $_SERVER['DOCUMENT_ROOT'] or $_SERVER['SERVER_NAME'] in your config.php or your constants.php then you have to update these values when you run the cron.php because php is directly executed by you and not by apache so these variables will be missing.
For example in your cron.php at the start provide the following else you will get error like undefined index DOCUMENT_ROOT or SERVER_NAME...
$_SERVER['DOCUMENT_ROOT'] = '/home/user/public_html'; $_SERVER['SERVER_NAME'] = 'sitename.com'; //for your localhost you can give like the following. This is with respect to your localhost. $_SERVER['DOCUMENT_ROOT'] = 'd:/xampp/htdocs'; $_SERVER['SERVER_NAME'] = 'localhost';
Consider these in your cron.php You have to update these in cron.php according to your server
$file = __FILE__; if($_SERVER['SERVER_NAME']=='localhost') $root = str_replace('static\cron.php','',$file); else $root = str_replace('static/cron.php','',$file); //$_SERVER['DOCUMENT_ROOT'] = '/home/boondogg/public_html'; $_SERVER['DOCUMENT_ROOT'] = $root; $_SERVER['SERVER_NAME'] = 'boondogglespub.com'; if($_SERVER['SERVER_NAME']=='localhost') $indexpath = 'D:/www/xampp/htdocs/boon/static/cron.php'; // Your CodeIgniter main index.php file else $indexpath = $root.'index.php'; // Your CodeIgniter main index.php file
More References
- http://www.google.com/search?q=cron+jobs+in+codeigniter&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
- http://codeigniter.com/wiki/Cron_job_bootstrapper/
- http://codeigniter.com/forums/viewthread/88635/
- http://stackoverflow.com/questions/1941554/how-to-setup-cron-in-windows-xp-with-codeigniter
- http://codeigniter.com/wiki/Category:Advanced::CronScript/
Multiple image resize issue
Captcha Helper
This is my own captcha helper for codeigniter
Allowing all base64 encoding characters in URI of codeigniter
The base64 alphabet is shown here: http://www.garykessler.net/library/base64.html You need to add +/=.
$config['permitted_uri_chars'] = '+=\a-z 0-9~%.:_-'; //better to prepend +=/
URI SEGMENT Replacing DOT ( . ) to underscore ( _ )
Image Resize function
For my reference
function resize($img) { $config['image_library'] = 'gd2'; $config['source_image'] = docroot.addpath.'static/folder/'.$img; $config['create_thumb'] = false; $config['maintain_ratio'] = false; $config['dynamic_output'] = false; $config['quality'] = '100%'; $config['width'] = 300; $config['height'] = 320; $this->image_lib->clear(); $this->image_lib->initialize($config); $this->image_lib->resize(); }
Image resize proportional
This will resize the images proportionately by maintaining the width and height when resize. Again this is for my reference have not yet prepared the description for the code.
function resizetn($img,$wi,$hi) { $imiz = getimagesize(docroot.addpath.$this->imgFolder.$img); if($imiz[0]<133)return; $config['image_library'] = 'gd2'; $config['source_image'] = docroot.addpath.$this->imgFolder.'tn/'.$img; // $config['new_image'] = docroot.addpath.'static/cimagestn/'.$img; $config['create_thumb'] = false; $config['maintain_ratio'] = true; $config['dynamic_output'] = false; if($imiz[0]>$imiz[1]) { $ratio = $hi / $imiz[1]; $width = $imiz[0] * $ratio; $config['width'] = $width; $config['height'] = $hi; } else { $ratio = $wi / $imiz[0]; $height = $imiz[1] * $ratio; $config['width'] = $wi; $config['height'] = $height; } $this->image_lib->clear(); $this->image_lib->initialize($config); $this->image_lib->resize(); }
If the width or height of the resized image is greater than the preferred dimension then crop it.
if($imiz[0]>170 || $imiz[1]>156); $this->crop($img);
and here is the cropping function
function crop($img) { $src = docroot.addpath.$this->imgFolder.'tn/'.$img; $nw = 170; $nh = 156; $wiwi = getimagesize($src); $imgdest = imagecreatetruecolor(170,156); switch($wiwi['mime']) { case 'image/jpeg': $imgsrc = imagecreatefromjpeg($src); imagecopy($imgdest,$imgsrc,0,0,0,0,170,156); imagejpeg($imgdest,$src,100); break; case 'image/png': $imgsrc = imagecreatefrompng($src); imagecopy($imgdest,$imgsrc,0,0,0,0,170,156); imagejpeg($imgdest,$src,100); break; case 'image/gif': $imgsrc = imagecreatefromgif($src); imagecopy($imgdest,$imgsrc,0,0,0,0,170,156); imagejpeg($imgdest,$src,100); break; } }
Create Unique URL Slug
// $t is the controller instance reference variable // $string is the value you are going to compare against field name 'slug' in table name $table function create_unique_slug($t,$string, $table) { $slug = url_title($string); $slug = strtolower($slug); $i = 0; $params = array (); $params['slug'] = $slug; if ($t->input->post('id')) { $params['id !='] = $this->input->post('id'); } while ($t->db->where($params)->get($table)->num_rows()) { if (!preg_match ('/-{1}[0-9]+$/', $slug )) { $slug .= '-' . ++$i; } else { $slug = preg_replace ('/[0-9]+$/', ++$i, $slug ); } $params ['slug'] = $slug; } return $slug; }
Reference: http://codeigniter.com/forums/viewthread/125827/
Create unique URL slug - Updated version
function create_unique_slug($string, $table,$field='slug',$key=NULL,$value=NULL) { $t =& get_instance(); $slug = url_title($string); $slug = strtolower($slug); $i = 0; $params = array (); $params[$field] = $slug; if($key)$params["$key !="] = $value; while ($t->db->where($params)->get($table)->num_rows()) { if (!preg_match ('/-{1}[0-9]+$/', $slug )) { $slug .= '-' . ++$i; } else { $slug = preg_replace ('/[0-9]+$/', ++$i, $slug ); } $params [$field] = $slug; } return $slug; }