SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Populate an array with DB results

  1. #1
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populate an array with DB results

    I'm using the 'Auto-Complete' feature from the Yahoo! UI Library, which is working fine.

    The array used to return data to the drop-down of 'auto-completed' results is like so:
    PHP Code:
    return array(
        
    'a' => array(
            
    "apple"
        
    ),
        
        
    'b' =>(
            
    "banana"
        
    ),
            
        
    'o' =>(
            
    "orange"
        
    )
    ); 
    However, I need this to be populated with results pulled in from a database...

    Any idea how I can do this??

    Cheeeeeers

  2. #2
    SitePoint Enthusiast FSan's Avatar
    Join Date
    Dec 2005
    Location
    San Martin de los Andes, Patagonia Argentina
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't work..cheers, though.

    Any other suggestions?

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by backtobasics View Post
    I'm using the 'Auto-Complete' feature from the Yahoo! UI Library, which is working fine.
    However, I need this to be populated with results pulled in from a database...
    Any idea how I can do this??

    Cheeeeeers
    When you say populated with results pulled in from a database.... Are you wanting to combine 2 arrays? Could you provide an example of the returned database data and the final format of combined data you are wanting?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by byron3@earthlink View Post
    Could you provide an example of the returned database data and the final format of combined data you are wanting?
    Well, the array I posted orginally, is what I need...

    But I need, for example, 'a', 'b' and 'o' to be filled with data pulled from a database instead of hard-coded.

    So, if in the database there was a field which contained 'pear', then it would be added into the array like so:
    PHP Code:
    'p' => array(
            
    "pear",
            ) 
    Do you follow?

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    function KeyExist$MyArray$compare ) { // Does this key exist in this array?
        
    foreach ( $MyArray as $k => $data ) {
            if ( 
    $k == $compare ) { return TRUE; }
        }
        Return 
    FALSE;
    }

    /* Open database and run mysql query to generate result set */
    $list = array(); //Create master array

    while ( $row mysql_fetch_assoc$results ); {
        
    $value $row['Data_For_Array'// Value to add to sub array in master array
        
    $key substr$value01); // Get first letter to use as key

        
    if ( KeyExist$list$key ) { // Does this key already exist in master array?
            
    $list[$key] = $value// Yes so add value to sub array
        
    } else {
            
    $list[$key] = array(); // No so create sub array
            
    $list[$key] = value// Add value to sub array 
        
    }

    Does this fit the bill?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  7. #7
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, doesn't do the job I'm afraid

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does the output look like?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  9. #9
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works fine, it's just not what I need.

    Thanks for your help, though..

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function KeyExist$MyArray$compare ) { // Does this key exist in this array?
        
    foreach ( $MyArray as $k => $data ) {
            if ( 
    $k == $compare ) { return TRUE; }
        }
        Return 
    FALSE;
    }

    /* Open database and run mysql query to generate result set */
    $list = array(); //Create master array

    while ( $row mysql_fetch_assoc$results )) {
        
    $value $row['Data_For_Array']; // Value to add to sub array in master array
        
    $key substr$value01); // Get first letter to use as key

        
    if ( KeyExist$list$key )) { // Does this key already exist in master array?
            
    $idx count$list[$key] );
            
    $list[$key][$idx] = $value// Yes so add value to sub array
        
    } else {
            
    $list[$key] = array(); // No so create sub array
            
    $list[$key][0] = value// Add value to sub array 
        
    }

    Try it one more time, The above snippet takes this array:

    array( "apple",
    "bannana",
    "pear",
    "orange",
    "opal",
    "olive");

    and produces the following output:

    Array ( [a] => Array ( [0] => apple ) [b] => Array ( [0] => bannana ) [p] => Array ( [0] => pear ) [o] => Array ( [0] => orange [1] => opal [2] => olive ) )
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  11. #11
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works fine, but I still can't get it to pass through the data that I need!

    When I 'print_r' that array and the original array, they're both looking the same...not sure what's happening/I'm missing!

  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post your code where you are passing the data array to your interface.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •