array of data |
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. |
Messages In This Thread |
array of data - by anmol - 04-14-2017, 02:51 AM
RE: array of data - by neuron - 04-14-2017, 03:08 AM
RE: array of data - by anmol - 04-14-2017, 06:05 AM
RE: array of data - by natanfelles - 04-14-2017, 06:50 AM
RE: array of data - by dave friend - 04-14-2017, 08:00 AM
|