Solved

Building a dynamic array from a postgresql database in PHP

Posted on 2010-09-22
3
588 Views
Last Modified: 2013-12-12
Hi,

I am new to PHP and need a little help with a project I am working on. Below is some code that I am using to build a 2 dimensional array:

// get all employee data
$db=new Database($_SESSION['dbname']);
 $result= $db->query('SELECT "FirstName","LastName","Initials","EmanID","EmpNumber","eMail" FROM "Employee"');
     
// dump employee details into an array
foreach ($result->result_array(FALSE) as $row)
            {
            
      $user =  array('FirstName'=>$row['FirstName'],
                                         'LastName'=>$row['LastName'],
                                         'Initials'=>$row['Initials'],
                                         'EmanID'=>$row['EmanID'],
                                         'EmpNumber'=>$row['EmpNumber'],
                                         'eMail'=>$row['eMail'],
                                         );            
      
            }
            
            // dump the $user array into the $users array
            $users = array(
                  $user
            );                  


            print_r($users);



What I am trying to achieve is something like:


$users = array(
      array('FirstName'=>'Fred', Lastname=>'Bloggs', Initials=>'P', EmanID=>'2', EmpNumber=>'123', eMail=>'foo@bar.com'),
      array('FirstName'=>'Joe', Lastname=>'Bloggs', Initials=>'D', EmanID=>'3', EmpNumber=>'456', eMail=>'foo@bar.com'),
      array('FirstName'=>'Bill', Lastname=>'Bloggs', Initials=>'Z', EmanID=>'4', EmpNumber=>'789', eMail=>'foo@bar.com'),
);

But what I get is:

Array ( [0] => Array ( [FirstName] => Bill [LastName] => Blogs [Initials] => Z[EmanID] =>4 [EmpNumber] =>789 [eMail] => foo@bar.com ) )

It seems to be overwriting the $user array through each loop so I only end up with one record. Any ideas on what I am doing wrong?


Thanks for any help
0
Comment
Question by:trojan_uk
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Sander Stad earned 500 total points
Comment Utility
Çan you try the following code:
// get all employee data
$db=new Database($_SESSION['dbname']);
$result= $db->query('SELECT "FirstName","LastName","Initials","EmanID","EmpNumber","eMail" FROM "Employee"');

$users = array();
      
// dump employee details into an array
foreach ($result->result_array(FALSE) as $row)
{

  $user =  array('FirstName'=>$row['FirstName'], 
      'LastName'=>$row['LastName'], 
      'Initials'=>$row['Initials'],
      'EmanID'=>$row['EmanID'],
      'EmpNumber'=>$row['EmpNumber'],
      'eMail'=>$row['eMail'],
  );            
  
  array_push($users, $user) 
}
            
             
print_r($users);

Open in new window

0
 

Author Closing Comment

by:trojan_uk
Comment Utility
sstad, thank you very much, you're a star. So use array_push to append each loop to $users.

Many thanks
0
 
LVL 9

Expert Comment

by:Sander Stad
Comment Utility
You're welcome. Good luck
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now