Often times I find myself doing the same queries over and over again for each table used by an application. This is why I needed to create an “universal” model in order to avoid repetition. Codeigniter lets you do that by allowing you to create a “MY_Model” inside the core folder of your application. Continue reading
The CodeIgniter community is still thriving, despite the recent events at EllisLab (http://ellislab.com/blog/entry/ellislab-seeking-new-owner-for-codeigniter). A 3.0 version is in the works down at GitHub and a new book was just published this September by Packt Publishing.
“Programming with CodeIgniter MVC” is written by Eli Orr – a programmer with 30+ years of experience in the high tech industry under his belt and who is presently a high level PHP developer, focusing on projects involving the CodeIgniter framework – and Yehuda Zadik – with 20+ years experience in the IT industry and 8 years worth of Object-Oriented Programming in PHP.
It takes some courage to write a book on CodeIgniter if we consider the last serious one that came out was about the 1.7version, but a statement needed to be done… This framework still has some juice in it.
This is the kind of book I needed when I first got into CodeIgniter. Sadly, at the time 2.1 rolled in, the web development community was still trying to get used to 1.7, so tutorials for newcomers to the new version of this framework were scarce.
Right now, Laravel gets all the buzz. Despite that, we still need frameworks that are fast, easy to grasp, and that lead us to expected results.
Here is where CodeIgniter comes in. Book in hand, having an easy to learn framework with decent requirements and great performance, you can soon say that you know without a doubt what a framework is and how it works.
Even if you supposedly need “some OOP knowledge” to read it, I can tell you that a PHP programmer that’s only on the procedural programming level can easily pick it up.
Starting from the famous “Hello World”, the book delves into the Model-View-Controller mindset and guides you through framework-specific code with a lot of examples.
The authors could have stopped at covering the basics, which can also be found on the internet, for the most part. Instead, they chose to give tangible examples. So, beyond sending e-mail with attachments and designing a login, the book treats popular APIs like Google Maps, Flickr, LinkedIn and Facebook, helping you to understand how to integrate the social aspect in your professional Apps.
I would have wanted the book to talk about HMVC (Hierarchical Model View Controller), which enables modular development in CodeIgniter, but I am pleased that it touches ORM. Hopefully these are the kind of subjects that can be approched in a next book written by Eli Orr and Yehuda Zadik, their style being practical and accessible.
Taking all this into account, this book is useful not only to the CodeIgniter community (a very helpful bunch), but also to those that are diving for the first time in the pool of PHP frameworks, or to those that are looking to migrate to a reliable and straightforward framework.
In a job application interview, the employer asked me if I made some “database scalability” in the past. To my shame, I didn’t even understand what that is, but now I can confidently say that I know the basics. Continue reading
What is a dynamic site without databases, right? Yesterday someone asked me to make a tutorial about retrieving data from a database using CodeIgniter. As a self-respecting framework, CodeIgniter is adhering to the Model-View-Controller philosophy, i.e. is using the Models to interact with the database. For starters I will assume that you already have installed CodeIgniter, and you have a MySQL database installed on your webserver. Continue reading
I will assume that you know how to use CodeIgniter, know what controllers, models and views are and what they do. If you don’t know anything about these terms, maybe you really don’t need to find out about how you can export data to an Excel spreadsheet. Continue reading
There are more ways to retrieve data from a database using PDO. The shortest way, but in my opinion, the too long to remember every single thing in it is as follows:
The longer way, but a better one is to keep the query separated:
We could aswell insert the “WHERE” condition inside the queries we wrote earlier, but the best way to query the database when we use “WHERE” conditions is by using placeholders, whether we are talking about positional placeholders or named placeholders:
About the fetch() and fetchAll() methods
As you’ve seen, the fetch() method returns each row in the result set, one after another, or FALSE if there are now rows returned.
On the other hand fetchAll() method returns an array containing all the rows in the result set.
Either way, the methods return an array representing each column if we use fetch(), or an array of rows with each element of the array (representing each row) containing an array of column values.
As you saw in almost all my examples, I am using the FETCH_ASSOC constant when i retrieve results from a database. That is because is easier for me to use the values from the database by naming the their respectiv columns.
So, when you retrieve the result, you usually call the value of the result row as follows: $row['column'].
Is a constant that returns an array indexed by column number as returned in your result set, starting with element (column) 0.
That means that you will have to call the first column of the row as follows: $row.
Being a memory consumer, the constant FETCH_BOTH will return an array indexed by both column name and number-indexed column number. That means that you can call the columns either by $row['firstcolumn'] or $row.
This constant will return you an object with property names that correspond to the column names returned in the result set. You will therefore have to get the column by calling it like so: $row->firstcolumn.
For this lesson, I think we’ve learned enough. There are a few more things to say, but I think that for the basics is enough. If you think I missed something, please write me a comment. Thank you
We’ve been there. To click by mistake a link or a button which deletes an information and not to be asked if I am sure I want to do it. And there goes my work… one second of not paying attention…
So, why not make in our pages, on those links that are important, an intermediary step (a confirm box) in which people get to be asked if they are sure on the actions they are about to make?
You could, ofcourse, make it a bit more complex like this…
What is directory listing?
There are those embarassing moments in life when you feel naked when someone sees at you something they aren’t suppose to see. The same happens with a site, when someone gets to a directory where there is no “index.php” or “index.html”. When those files are missing, the site shows the visitor the directory listing with all the files that are inside it.
For example, if we type “www.somesite.com/images” in the browser, if the settings are not right, the browser will show the listing of the files that can be found in that directory. For security reasons, it’s better not to show the files inside the directory of the web server.
Most of the web servers are configured in such a way that they don’t show the listing of the directory. And yet, there are web servers that are not configured that way.
How can I prevent directory listing?
There is a simple way to prevent directory listing. Just put the any one of following code (after which make sure you press an Enter, the file must end with an empty line) into the .htaccess file.
If you place the .htaccess file in the root folder you will prevent the directory listing aswell the directory listings of the sub-folders.
What if I want to prevent the listing of certain extensions?
Let’s suppose that you don’t want to list “.jpg , .gif and .zip” and you don’t care about listing other files of that directory then you can out the following code inside .htaccess.
You will have to write:
If your server is setup with preventing directory listing then you can add the following code in the .htaccess file to allow the directory listing.
Once you agreed on the advantages of using PDO, and verified that you can use PDO on your server, is time to connect to the database. For your sake i hope that you are already having a database set up. If you don’t, I would like you to set it up.
How to connect to a database using PDO
I must remind you that the PDO extension is object oriented (OOP), which makes all the things go easier. Simply put, you can connect to the database like this:
Connecting the fast way to the database
After is all set up, you can make the replacements in the following code snippet:
Connecting the right way to the database
Yeah, there is a right way of connecting. I don’t know if this is the one, but this is the way I prefer to connect to the database:
PHP Database Access: Are You Doing It Correctly? - http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
PHP PDO MySQL:Example How to Connect to MySQL Using PDO Class, PDO Error Attributes - http://php.tutorialref.com/pdo-mysql-connect-example.html