trojan_uk
asked on
Building a dynamic array from a postgresql database in PHP
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","In itials","E manID","Em pNumber"," eMail" FROM "Employee"');
// dump employee details into an array
foreach ($result->result_array(FAL SE) as $row)
{
$user = array('FirstName'=>$row['F irstName'] ,
'LastName'=>$row['LastName '],
'Initials'=>$row['Initials '],
'EmanID'=>$row['EmanID'],
'EmpNumber'=>$row['EmpNumb er'],
'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
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","In
// dump employee details into an array
foreach ($result->result_array(FAL
{
$user = array('FirstName'=>$row['F
'LastName'=>$row['LastName
'Initials'=>$row['Initials
'EmanID'=>$row['EmanID'],
'EmpNumber'=>$row['EmpNumb
'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',
array('FirstName'=>'Joe', Lastname=>'Bloggs', Initials=>'D', EmanID=>'3', EmpNumber=>'456', eMail=>'foo@bar.com'),
array('FirstName'=>'Bill',
);
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome. Good luck
ASKER
Many thanks