array of data |
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 ) " PHP Code: $result = $this->db->select('*')
when i pass $rows to the view
$this->load->view('following',$rows); it still shows a undefined variable rows error in the view.
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
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 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 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); View file: application/views/table_view.php PHP Code: <!DOCTYPE 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. |
Welcome Guest, Not a member yet? Register Sign In |