View that prints all the array $data['personas'] in a table html. |
Good morning and sorry for my english.
I have a very simple Controller, a very simple Model and I need the code of a simple View that prints all the array $data['personas'] in a html table. The problem is: I don't know the name of fields, I don't know how many fields has got the table, (or array) And I need put the name of the fields in the first row of the html table... Code of Controller: Code: class Controller_05_tabla_oracle extends CI_Controller { Code of Model: Code: class Model_05_tabla_oracle extends CI_Model{ Code of View, (since now) Code: <div class="page-header"> This you can see: ![]() And this is i need to see: ![]() Thanks a lot for your atention. Àngel Gimeno
You can use the array_keys function to get the keys of the first item (if they all have the same keys?) and loop through them to create the header row.
Then simply loop through the data array and print the data.
(04-20-2017, 08:10 PM)JayAdra Wrote: You can use the array_keys function to get the keys of the first item (if they all have the same keys?) and loop through them to create the header row. Hello Jay. Thanks for your answer. 1) Referent to array_keys I tried this in the View: Code: <div class="page-header"> And it prints the first level of keys: ![]() I don't know how descend levels of sub Arrays... Do you know how? 2) Referent to simply loop through the data array and print the data. I don't know how print a sub Array without the name of the Fields. I only know like here: Code: <Table border="1"> I tried this but not rules: Code: <Table border="1"> ![]() Thanks a lot...
You need to loop through the keys of the first array item, not the personas array itself. So try:
PHP Code: <table> This will create the header row first by looping through keys of first item. It will then loop through the personas array, echoing a new row for each person, and a new cell for each "field" or property of the person object.
[quote pid='342865' dateline='1492771418']
Ooooh... It's perfect: ![]() [/quote] Thank you very much Jay. ![]()
Hello Jay.
Another question more please: What can I do if only need one value of the table? (collumn 5, row 6) Thanks a lot… ![]()
You can also use this, it will pull the column names from the table:
CodeIgniter User Guide - HTML Table Class What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(05-02-2017, 04:01 AM)InsiteFX Wrote: You can also use this, it will pull the column names from the table: Thanks for your answer InsiteFx. But i don't know how de html Table Class can helps me... I need to know how print the content of the array with the index $i, $j, something like this: Code: <tbody>
I'm not quite sure what you're wanting, but could you do this?
PHP Code: <?php echo $personas[5][6]; ?>
(05-02-2017, 06:20 AM)JayAdra Wrote: I'm not quite sure what you're wanting, but could you do this? Thanks for your answer Jay. Yes, I need only the 6 value of the 5 array I tried that you say and it does this error: _______________________________ Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\CuadroMando_CodeIgniter\application\views\view_05_tabla_oracle.php on line 601 A PHP Error was encountered Severity: Error Message: Cannot use object of type stdClass as array Filename: views/view_05_tabla_oracle.php Line Number: 601 Backtrace: ________________________________ What do you think? Thanks a lot. |
Welcome Guest, Not a member yet? Register Sign In |