Welcome Guest, Not a member yet? Register   Sign In
View that prints all the array $data['personas'] in a table html.
#1

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 {

  public function index()
  {
$this->load->helper('url');
$this->load->model('model_05_tabla_oracle');
$this->load->database();
$data['personas'] = $this->model_05_tabla_oracle->getPersonas_Ovh();
$this->load->view('view_05_tabla_oracle',$data);
  }
}

Code of Model:
Code:
class Model_05_tabla_oracle extends CI_Model{

function __construct()//model construct function
{
   parent::__construct();
   $this->ovh_db=$this->load->database('ovh',true);
}

public function getPersonas_Ovh()
{
$query = $this->ovh_db->query('select * from OVPRO.OVT_TRACKING_SESSION where rownum<=10');
return $query->result();
}
}


Code of View, (since now)

Code:
     <div class="page-header">
 
<?php foreach($personas as $p){ ?>
<Table border="1">
<tr><td><?php echo $p->USER_ID; ?></td></tr>
</Table>
<?php }//end foreach ?>
</br>
</br>
</br>
</br>
</br>
<pre><?php print_r($personas) ?></pre>

     </div>

This you can see:
[Image: 20170420035908.jpg]
And this is i need to see:
[Image: 20170420040000.jpg]
Thanks a lot for your atention.

Àngel Gimeno
Reply
#2

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

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

Then simply loop through the data array and print the data.

Hello Jay.

Thanks for your answer.



1) Referent to array_keys

I tried this in the View:

Code:
         <div class="page-header">
 
        <Table border="1">
            <?php foreach($personas as $p){ ?>
                <tr><td><?php echo $p->USER_ID; ?></td></tr>
            <?php }//end foreach ?>
        </Table>
        </br>
        </br>
         <pre><?php print_r(array_keys($personas)) ?> </pre>

And it prints the first level of keys:

[Image: 20170421044653.jpg]
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">
            <?php foreach($personas as $p){ ?>
                <tr><td><?php echo $p->USER_ID; ?></td></tr>
            <?php }//end foreach ?>
        </Table>

I tried this but not rules:

Code:
        <Table border="1">
            <?php foreach($personas as $p){ ?>
                <tr><td><?php echo $p->[0]; ?></td></tr>
            <?php }//end foreach ?>
        </Table>

Confused


Thanks a lot...
Reply
#4

You need to loop through the keys of the first array item, not the personas array itself. So try:

PHP Code:
<table>
    <
thead>
        <?
php foreach($personas[0] as $key => $value) { ?>
            <tr><th><?php echo $key//these are the headers, pulled from keys for first item ?></th></tr>
        <?php //end foreach ?>
    </thead>
    <tbody>
        <?php foreach($personas as $p){ //loop through array of persons ?>
            <tr>
                <?php foreach($p as $key => $value) { //loop through each property of the object ?>
                    <td><?php echo $value?></td>
                <?php //end foreach ?>
            </tr>
        <?php //end foreach ?>
    </tbody>
</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.
Reply
#5

[quote pid='342865' dateline='1492771418']
Ooooh...

It's perfect:

[Image: 20170421055437.jpg]
[/quote]

Thank you very much Jay.

Big Grin
Reply
#6

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…






[Image: 20170502044439.jpg]
Reply
#7

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 )
Reply
#8

(05-02-2017, 04:01 AM)InsiteFX Wrote: You can also use this, it will pull the column names from the table:

CodeIgniter User Guide - HTML Table Class

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>
                <?php foreach($personas[5] as $p){ //loop through array of persons ?>
                    <tr>
                        <?php foreach($p[6] as $key => $value) { //loop through each property of the object ?>
                            <td><?php echo $value; ?></td>
                        <?php } //end foreach ?>
                    </tr>
                <?php } //end foreach ?>
            </tbody>
Reply
#9

I'm not quite sure what you're wanting, but could you do this?

PHP Code:
<?php echo $personas[5][6]; ?>
Reply
#10

(05-02-2017, 06:20 AM)JayAdra Wrote: I'm not quite sure what you're wanting, but could you do this?

PHP Code:
<?php echo $personas[5][6]; ?>

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




Theme © iAndrew 2016 - Forum software by © MyBB