
March 4th, 2013, 01:08 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 17 m 25 sec
Reputation Power: 0
|
|
PHP5 - Array
hi,
i have a php file with this code :
PHP Code:
$items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus",
"Black-crowned Night Heron"=>"Nycticorax nycticorax",
"Purple Heron"=>"Ardea purpurea",
"White Stork"=>"Ciconia ciconia",
);
foreach ($item as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
it sends " echo "$key|$value\n"; " to my javascript.
so till here perfect
BUT now, i want to replace my $item array by a database query result
so i have for now :
PHP Code:
$db = new mysqli('xx.x.xxx.xx',myusername,mypassowrd,mydb);
$query = $db->query("SELECT * from mytable");
---> i want to sort out ID and TOWN and to put them in an array just like "$item" array above
PHP Code:
while ($result = $query ->fetch_object()) {
$id = $result->ID;
$town=$result->TOWN;
}
only now, this code here :
PHP Code:
foreach ($result as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
gives me every database result in one row without any <br> or \n .Just a mess, and it should come out as options of an select field. :-(
so the question is:
how can i get this :
PHP Code:
while ($result = $query ->fetch_object()) {
$id = $result->ID;
$town=$result->TOWN;
}
like
PHP Code:
$items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus",
"Black-crowned Night Heron"=>"Nycticorax nycticorax",
"Purple Heron"=>"Ardea purpurea",
"White Stork"=>"Ciconia ciconia",
);
in order to use :
PHP Code:
foreach ($result as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
????HELP !!!!!
|