Looping through the database results

In any application with database connectivity, you'll probably need to display records from a database; looping through rows of data returned from a query is one of the most common tasks you'll perform in programming. CodeIgniter handles looping through database results using PHP for each statement. In this recipe, we will loop through each record at a time, echoing out the relevant information.

Getting ready

To support this recipe, we are going to create a database table and write some data to it. If you already have the data, you can skip this recipe; if not, copy the following code into your database:

CREATE TABLE IF NOT EXISTS `loop_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(255) NOT ...

Get CodeIgniter 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.