• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
array of data

#1
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 ) "
Reply

#2
PHP Code:
$result $this->db->select('*')
->
from('tableName')
->
get();

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

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

Reply

#3
when i pass $rows to the view

$this->load->view('following',$rows);
it still shows a undefined variable rows error in the view.
Reply

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

Controller:

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

View:

print_r($followings);

Learn the User Guide: https://codeigniter.com/user_guide/tutor...pages.html
Reply

#5
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:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Tabletest extends CI_Controller
{
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load
          
->model('user_model')
 
         ->library('table');
 
       $this->table->auto_heading FALSE //Because we will set our own headings in the next line
 
       $this->table->set_heading('Name''Country''Designation');
 
   }

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

 
       $vars['table'] = $this->table->generate($db_result_object);
 
       $this->load->view('table_view'$vars);
 
   }



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:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
User_model extends CI_Model
{
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->database();
 
   }

 
   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:
$this->table->generate($db_result_object); 
The return of that call is sent to the view.

View file: application/views/table_view.php

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

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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.