Archive for the ‘Snippets Ideas’ Category
Import CSV file into database using CodeIgniter
CSVReader is a library for CodeIgniter that can help you importing CSV files into database using CodeIgniter framework. The library uses fgetcsv function from PHP to get the content from CSV file and then iterates over the records returning an array as result.
Then, you can iterate the parsed results from the CSV file to insert them into the database easily using a foreach statement. For example, you can refer to Clooner’s proposal here.
This CSVReader library can be really helpful in case you need to import large CSV files into the database using a CodeIgniter application, but also can be useful to import Excel files into the database or any other database file that can be exported to CSV. For Excel, you can export the results using comma separated values and then use this approach.
Using MD Image to resize and crop an Image
MD_Image is a CodeIgniter Library that allows you to resize and crop images in CodeIgniter keeping a desired ratio. You can see the original thread here.
Gravatars using CodeIgniter
If you want to display avatar images in your CodeIgniter application, you can use this Gravatar helper that contains a simple function to retrieve the Gravatar image source for a given email.
The Gravatar helper was created by Dave_C as mentioned in Codeigniter Forums.
By using this helper you can easily integrate your application with Gravatars like this:
![]()
How to use the helper?
Follow the installation steps described here, and then just load the helper as usual and put this code in your view page passing the desired email as first parameter.
<?php echo gravatar( "you@domain.com" ); ?>
Encrypt and decrypt with CodeIgniter
Encryption and decryption functions are available in CodeIgniter by using the Encryption class. Of course you can always use PHP functions, but what I say is that CodeIgniter has a specific class for handling some encryption functions.
Everything is explained in the Encryption class, but the first thing you should do is configure your encryption key in config.php.
Then, you can start using for example $this->encrypt->encode() and $this->encrypt->decode().
If you need to handle passwords instead, I’d recommend to visit Handling Passwords in CodeIgniter, by Elliot.
Using url_title() with Spanish or other strange characters
If you are running your site in Spanish and using url_title to create Permalinks in CodeIgniter then likely you had run in character issues while creating your links.
Martin posted a solution in CodeIgniter’s forum with a workaround for this issue. He proposed
$trans = array(
$search => $replace,
"á" => 'a',
"é" => 'e',
"í" => 'i',
"ó" => 'o',
"ú" => 'u',
"à" => 'a',
"è" => 'e',
"ì" => 'i',
"ò" => 'o',
"ù" => 'u',
"ñ" => 'n',
"ä" => 'a',
"ë" => 'e',
"ï" => 'i',
"ö" => 'o',
"ü" => 'u',
"\s+" => $replace,
"[^a-z0-9".$replace."]" => '',
$replace."+" => $replace,
$replace."$" => '',
"^".$replace => ''
);
My two cents here are the following. You can try to override the URL Helper by extending it in MY_url_helper.php under application/helpers directory.




