Thread: Array

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Question 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 !!!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,336
    Rep Power
    594
    You don't want 'fetch_object'. You want 'fetch_all' or 'fetch_array'.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    solution


    and here the solution :

    PHP Code:
    while($rows mysql_fetch_array($result)){

    echo 
    $rows['VILLE']."\n";


    :-)

    THANKS FOR YOUR HELP ! ----it was too obvious ;-))

    Comments on this post

    • Jacques1 disagrees : Please *do not* close your threads. There's still a lot to say about your current approach, but it's not possible when the thread is already closed.
    • gw1500se disagrees : I will add my 2 cents as well and argee with Jacques1. I gave you the solution to your immediate problem but you have a few additional issues that shojuld be dealt with even though your code seems to work.

IMN logo majestic logo threadwatch logo seochat tools logo