SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional Array Output

    I find myself asking the database for the same information, sometimes in the same script. I'd like to save the data in an array, then output the data in a <select><option> drop down.

    So far I've got the data in an array called $agentList like so:

    Code PHP:
    $agentList = array();
     
    $q = "SELECT 
    		admin_id, first_name, last_name
    	FROM
    		admins
    	ORDER BY 
                    last_name";
     
    $r = @mysqli_query($dbc, $q);
     
    while ($rows = mysqli_fetch_assoc($r)) {
    	$agentList[] = $rows;
    }

    The array data looks like this:

    Code PHP:
    Array (
        [0] => Array
            (
                [admin_id] => 40
                [first_name] => Tom
                [last_name] => Clemente
            )
     
        [1] => Array
            (
                [admin_id] => 47
                [first_name] => Thomas
                [last_name] => Conlin
            )
     
        [2] => Array
            (
                [admin_id] => 45
                [first_name] => Bob
                [last_name] => Deangelis
            )
     
        [3] => Array
            (
                [admin_id] => 62
                [first_name] => Gabby
                [last_name] => Gascon
            )
    )

    I'm lost after that. I'd like the data to output:

    Code HTML4Strict:
    <select name="agent">
      <option value="40">Tom Clemente</option>
      etc...

    Any help is much appreciated and will only make me a better programmer!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Take a look at foreach
    PHP Code:
    echo '<select name="agent">';
    foreach (
    $agentList as $agent) {
      echo 
    '  <option value="' $agent['admin_id'] . '">' $agent['first_name'] . ' ' .  $agent['last_name'] . '</option>';
    }
    echo 
    '</select>'

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You make it look so easy. Thank you very much!


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
  •