Page 1 of 1

How can I make and show associative table ?

Posted: Mon Apr 20, 2009 3:21 pm
by jelonek
I will create loop to add data to associative table but first I would like to make it manually because it is complicate for me. :(
Did I make this associative table correct ? How can I show data from this table using for example loop foreach ? It should look:
employees:
- Jack Jones......
- Jane Munson....

students:
- Jack Jones2......
- Jane Munson2....

Code: Select all

 
//add first data to table:
$data[][][][] = array(
          array(
            'employees'=>array(
                
                array('name' => 'Jack Jones', 'home' => '777-555-5555',
                'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
                
                array('name' => 'Jane Munson', 'home' => '000-555-5555',
                'cell' => '123456', 'email' => 'jane@myexample.com'),
            ),          
          )
        );
 
//add second data to table: 
$data[][][][] = array(
          array(            
            'students'=>array(
                
                array('name' => 'Jack Jones2', 'home' => '777-555-5555',
                'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
                
                array('name' => 'Jane Munson2', 'home' => '000-555-5555',
                'cell' => '123456', 'email' => 'jane@myexample.com')
            ),
          )
        );
 

Re: How can I make and show associative table ?

Posted: Mon Apr 20, 2009 3:41 pm
by pickle
$data[][][][] means your elements are 4 levels deep - there's no need for that. This is how I'd do it:

Code: Select all

$data['employees'][] = array('name'=>'Jack Jones'...);
$data['employees'][] = array('name'=>'Jane Munson'...);
 
$data['students'][] = array('name'=>'Jack Jones2'...);
$data['employees'][] = array('name'=>'Jane Munson2'...);

Re: How can I make and show associative table ?

Posted: Mon Apr 20, 2009 6:10 pm
by pickle
McInfo wrote:Expanded, it should look like this:
Very nice syntax!

Re: How can I make and show associative table ?

Posted: Mon Apr 20, 2009 10:14 pm
by jelonek
Thx You very much pickle and McInfo but I can't show that :(

Code: Select all

 
$data['employees'][] = array('name'=>'Jane Munson');
$data['animals'][] = array('name'=>'Michael');  
$data['students'][] = array('name'=>'Jack Jones2');
$data['computers'][] = array('name'=>'Eniac');
..............;
 
 
//count() - return number elements in table
for($x=0, $cnt = count($data); $x < $cnt; $x++)
{
echo "<b>Category: ";
echo $data[$x];
echo "</b><br/>";
 
    for($y=0, $cnt2 = count($data[$x]); $y < $cnt2; $y++)
    {
        echo $data[$x][$y]['name'];
        echo "<br/>";
    }
    echo "<br/><br/>";
}
 
PS. Posted: Mon Apr 20, 2009 11:14 pm :D In my country it is Tuesday Apr 21, 2009 05:21 am :D

Re: How can I make and show associative table ?

Posted: Tue Apr 21, 2009 12:17 am
by jelonek
I think it is something like that - it is complicate :P

Code: Select all

 
$data[] = array('category'=>'employees', 'dane'=>array());
$data[] = array('category'=>'students', 'dane'=>array());
 
$data[0]['dane'][] = array('name'=>'Jack Jones', 'home'  => '777-555-555');
$data[0]['dane'][] = array('name'=>'Jack Jones2', 'home'  => '777-555-556');
$data[1]['dane'][] = array('name'=>'Jane Munson', 'home'  => '777-555-557');
 
 
 
for($x=0, $cnt = count($data); $x < $cnt; $x++)
{
echo "<b>";
echo $data[$x]['category'];
echo "</b><br/>";
 
    for($y=0, $cnt2 = count($data[$x]); $y < $cnt2; $y++)
    {
        echo $data[$x]['dane'][$y]['name'];
        echo " ";
        echo $data[$x]['dane'][$y]['home'];
        echo "<br/>";
    }
    echo "<br/><br/>";
}