Hi Folks,
I have an array problem that has me clawing the walls,
so I really hope someone can help.
I use the SQL query below to retrieve records from
a PostgreSQL database which are then written into an array.
print_r shows the array for a typical retrieval
as follows:
Array ( [0] => Array ( [firstname] => Stephen [lastname] => Spainhour )
[1] => Array ( [firstname] => Ellen [lastname] => Siever ) [2] => Array
( [firstname] => Nathan [lastname] => Patwardhan ) )
<?php
$bkid=$HTTP_GET_VARS[bookid];
$authors=$db->get_results(
"select a.firstname, a.lastname from authors a, haswritten h
where a.authorid=h.hw_authorid and h.hw_bookid=$bkid", ARRAY_A);
foreach ($authors as $author)
while(list($key, $value) = each($author))
{
$authorstring= " $value ";
echo $authorstring;
}
?>
<BR><BR>
<? print_r($authors); ?>
<BR>
In my example, the value
for $authorstring inside the while loop would be:
Stephen Spainhour Ellen Siever Nathan Patwardhan
Outside the while loop, the string is truncated to the
last element of the array, i.e.:
Patwardhan
I have two problems that I can't solve.
- How can I re-format the string from the above
to:
Stephen,Spainhour;Ellen,Siever;Nathan,Patwardhan
and
- How can I keep the re-formatted value outside the while loop so that I can use it later for other purposes, e.g
to display it in a form field for possible updating?
thanks in advance,
Denis Grannell