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.
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 NULL, `last_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; INSERT INTO `loop_table` (`id`, `first_name`, `last_name`) VALUES (1, 'Lucy', 'Welsh'), (2, 'Rob', 'Foster');