Skip to content

Instantly share code, notes, and snippets.

View abdullahbutt's full-sized avatar

Abdullah Butt abdullahbutt

  • Fulda, Deutschland
View GitHub Profile
@abdullahbutt
abdullahbutt / CI_Email_attachments
Created November 10, 2013 20:19
CI Email attachments
Send Email Attachments without Hassles
Sending emails is a complex business. CI's code for doing it looks easy to follow:
$this->load->library('email');
$this->email->from('your@your-site.com', 'Your Name');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
There are a number of issues involved in sending emails: setting word-wrapping (and escaping it so long URLs don't get wrapped and broken up) for example, or sending attachments. The standard PHP functions can get quite complex here, and the result is that many code writers are tempted to avoid using these functions if they possibly can.
CI's email class makes it simple to send an attachment. You write:
$this->email->attach('/path/to/photo1.jpg');
@abdullahbutt
abdullahbutt / CI_Zip_Files_for_Download
Created November 10, 2013 20:22
CI Zip Files for Download
Save Bandwidth by Zipping Files That Users Need to Download
To save bandwidth, it's a fairly common practice to compress or 'ZIP' files before you download them. That's not something I've ever done, and I wouldn't know how to go about it. On the other hand, CI has a nice facility that allows you to produce zipped files with four lines of code:
$name = 'mydata1.txt';
$data = 'the contents of my file…………';
$this->zip->add_data($name, $data);
$this->zip->archive('c:/my_backup.zip');
Run this, and you find a ZIP archive on your C drive containing one file. Your ZIP filer reader will unzip it and produce the original data for you.
People who use your site won't know that you've produced this impressive result so easily. They'll be impressed! Your site will save bandwidth. You did it in minutes rather than hours.
@abdullahbutt
abdullahbutt / Top Ten PHP Frameworks
Created November 10, 2013 20:26
Top Ten PHP Frameworks
Top Ten PHP Frameworks
http://www.phpit.net/article/ten-different-php-frameworks/
@abdullahbutt
abdullahbutt / CI_principles
Created November 14, 2013 07:59
CI's principles: Loose Coupling & Component Singularity
CI has two useful principles.
These are set out in the Design and Architectural Goals section of CI's User Guide:
Loose Coupling: Coupling is the degree to which the components of a system rely on each other. The less the components depend on each other, the more re-usable and flexible the system becomes. Our goal was a very loosely coupled system.
Component Singularity: Singularity is the degree to which components have a narrowly focused purpose. In CodeIgniter, each class and its functions are highly autonomous in order to allow maximum usefulness.
@abdullahbutt
abdullahbutt / CI_model_library_helper_plug-in
Created November 14, 2013 12:06
CI Types of Files or Classes on a CI Site .. model, library, helper, plug-in
Types of Files or Classes on a CI Site
There are several different sub-folders within the application folder. We have already looked at the controller, config, and views folders.
But what are libraries, models, and scripts? This is one area where CI seems rather confusing. (If you have used versions of CI before version 1.5, you'll realize why. Rick Ellis wasn't happy with the earlier versions and has changed the structure quite a lot. However, for compatibility reasons, some anomalies remain.)
In a technical sense, these folders are treated in much the same way. There's no reason why you shouldn't put your code in any of these folders, though you'll have to make it slightly different in each.
Let's say that you have written a block of code called display, for example, which contains a function called mainpage. There are four ways you might have done this: as a model, a library, a helper, or a plug-in. The following table summarizes the differences between each approach, and shows you how to load and use eac
@abdullahbutt
abdullahbutt / CI_anchor
Last active December 28, 2015 12:48
anchor in CI
//Note: We have to load the 'url' helper first to use anchor function
$this->load->helper('url');
//echo base_url().'<br>';
echo anchor('start/hello', 'Say Hello Mate');
//It is equivalent of:
//<a href="http://localhost/my_project/index.php/start/hello">Say Hello Mate</a>
@abdullahbutt
abdullahbutt / gist:7523942
Last active December 28, 2015 15:48
CI_get_instance() (get_instance() or & get_instance() in CI)
get_instance() or & get_instance() in CI:
In some cases you may want to develop classes that exist apart from your controllers but have the ability to utilize all of Code Igniter's resources. This is easily possible by using get_instance() functions.
Any class that you instantiate within your controller functions can access Code Igniter's native resources simply by using the get_instance() function. This function returns the main Code Igniter object.
Normally, to call any of the available Code Igniter functions requires you to use the
$this</b> construct:
@abdullahbutt
abdullahbutt / CI_Connection_DB
Created November 18, 2013 08:27
CI_Connection_DB (establishing connection to db in ci)
In other words, the CI framework is making your code more robust. Now, let's look at how it works.
Firstly, connecting to the database is very simple. In classic PHP, you might say something like this:
$connection = mysql_connect("localhost","fred","12345");
mysql_select_db("websites", $connection);
$result = mysql_query ("SELECT * FROM sites", $connection);
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
foreach ($row as $attribute)
print "{$attribute[1]} ";
}
@abdullahbutt
abdullahbutt / CI_Active_Record_Auto_Func_&_Read_Queries
Created November 18, 2013 08:44
CI_Active_Record_Auto_Func_&_Read_Queries
Active Record
'Active Record' is a 'design pattern'—another of those highly abstract systems like MVC, which provide templates for solving common coding problems and also generate some of the dullest books on the planet. In itself, it isn't code, just a pattern for code. There are several different interpretations of it. At its core is the creation of a relationship between your database and an object, every time you do a query. Typically, each table is a class, and each single row becomes an object. All the things you might want to do with a table row—create it, read it, update it, or delete it,
for example—become 'methods', which that object inherits from its class. Ruby on Rails is built around the Active Record pattern, and so is CI—although the exact implementation in the two frameworks seems to have subtle differences.
Enough theory—what does it mean? Well, simple and clear code statements, if you don't mind putting arrows in them.
Advantages of Using the Active Record Class
Active record saves you t
@abdullahbutt
abdullahbutt / CI_Active_Record_Displaying_Query_Results
Created November 18, 2013 09:14
CI_Active_Record_Displaying_Query_Results
Displaying Query Results
Showing database query results in CI is quite simple. We define our query as above, ending in:
$query = $this->db->get();
Then, if there are multiple results, they can be returned as a $row object through which you iterate with a foreach loop:
foreach ($query->result() as $row)
{
print $row->url;
print $row->name;
print $row->client;
}