How can I make and show associative table ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jelonek
Forum Newbie
Posts: 3
Joined: Mon Apr 20, 2009 3:04 pm

How can I make and show associative table ?

Post 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')
            ),
          )
        );
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How can I make and show associative table ?

Post 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'...);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How can I make and show associative table ?

Post by pickle »

McInfo wrote:Expanded, it should look like this:
Very nice syntax!
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jelonek
Forum Newbie
Posts: 3
Joined: Mon Apr 20, 2009 3:04 pm

Re: How can I make and show associative table ?

Post 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
jelonek
Forum Newbie
Posts: 3
Joined: Mon Apr 20, 2009 3:04 pm

Re: How can I make and show associative table ?

Post 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/>";
}
 
Post Reply