CodeIgniter Forums
array of data - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: array of data (/thread-67822.html)

array of data - anmol - 04-14-2017

i am very new to codeigniter and struggling with the syntax.
i have retrieved the data from the database and the array have more thean one rows in it now i want to foreach loop each entry one by one,i am not getiing how would i do it.i want to make a table and dispaly all the values in the respective columns
my array $followings have following data in it
"Array ( [0] => stdClass Object ( [username] => anmol [country] => india [designation] => student ) [1] => stdClass Object ( [username] => rahulchut [country] => japan [designation] => developer ) [2] => stdClass Object ( [username] => harsha [country] => india [designation] => founder ) [3] => stdClass Object ( [username] => rahuls [country] => usa [designation] => manager ) "

RE: array of data - neuron - 04-14-2017

PHP Code:
$result $this->db->select('*')

$rows $result->result_array(); // returns result as an array, array(row0, row1 ...);

foreach($rows as $key => $row){
'row index: ' $key' '$row['column_name_1'],  ' '$row['column_name_2']

RE: array of data - anmol - 04-14-2017

when i pass $rows to the view

it still shows a undefined variable rows error in the view.

RE: array of data - natanfelles - 04-14-2017

CodeIgniter assign the first keys of an array as variables. Example:


$data['followings'] = $this->db->get('followings')->result_array();
$this->load->view('followings', $data);



Learn the User Guide:

RE: array of data - dave friend - 04-14-2017

You can write all the code to do this yourself but you are learning a framework designed to make your life easier. So let's make your life easier.

Codeigniter (CI) has a nice library for creating html tables. (Documentation Here) The Table library can take the result of a database query and turn it into a nice HTML table with very few lines of code.

Here is how it might be done, and done the CI MVC way.

Controller file: application/controllers/Tabletest.php

PHP Code:
('BASEPATH') OR exit('No direct script access allowed');

Tabletest extends CI_Controller
   public function __construct()
       $this->table->auto_heading FALSE //Because we will set our own headings in the next line

   public function index()
       $db_result_object $this->user_model->get_users();

       $vars['table'] = $this->table->generate($db_result_object);

The constructor loads the Table library, the model, and sets a couple Table related items.

The index() method uses the model get a db result object that is used as an argument to table->generate(). That method then creates the table html. The html is then passed to the view file "table_view.php".

Model File: /application/models/User_model.php

PHP Code:
('BASEPATH') OR exit('No direct script access allowed');

User_model extends CI_Model
   public function __construct()

   public function get_users()
       return $this->db->query('SELECT username, country, designation FROM tableName');

Note that rather than making Query Builder (QB) calls  (e.g. select(), from(), where(), get()) the method query() is used instead.

Why not use QB? Query Builder is a great tool but is not always appropriate. QB makes calls to multiple methods to create SQL statement strings. In this case, QB would need to run a large amount of code just to construct the simple SQL statement "SELECT username, country, designation FROM tableName".  After creating that string QB literally calls the method (query()) shown above. We can type less code and execute less code by using query().

So, when you have a very simple db request you should consider bypassing QB and directly create the statement yourself.

We return the return of query() without calling any of the methods that generate query results (e.g. result(), result_array() ). All Table needs is an object that can call those result generating methods.

We pass the return from the model to the table with this line.
PHP Code:
The return of that call is sent to the view.

View file: application/views/table_view.php

PHP Code:
<!DOCTYPE html>
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
title>Table Test</title>
php echo $table?>

So, not much code and a nice html table on the screen. Any you didn't have to write a single foreach statement. Examining the source code for Table will show you how that is done and much more.

You can use CSS and the templating options of the Table class to make it look just the way you want.

Thank you for putting up with me in teacher mode.