douluo2930 2013-06-12 08:47
浏览 27
已采纳

从多维数组创建下拉列表

I have a multi dimensional array created from a mysql query. with each index holding an array containing customer information. I want to create a drop down list from this with the value being customer ID and the text being customer name but I don't know how to access the arrays inside the main array.

I have the following function which I used to create other drop down lists from single arrays but when I try to use it with a multi dimensional array all it returns is the index numbers. (i get a list of 0, 1, 2, 3)

function createDropDown($name = '', $options = array()) {
$dropDown = '<select name="'.$name.'">';
foreach ($options as $option => $value) {
    $dropDown .= '<option value='.$value.'>'.$option.'</option>';
}
$dropDown .= '</select>';
return $dropDown;
}

EDIT

its 2 dimensional, an array holding arrays of customer details. my query is ran on a different page so I save the results into a session variable with this.

$searchtext = $_POST['searchDB']; 
    $query = "SELECT * FROM customer WHERE First_Name LIKE '%$searchtext%'";
    $data = mysql_query($query) or die(mysql_error());

    $Customers = array();
    while($row = mysql_fetch_assoc($data)){

    $Customers[] = $row;
        }

     $anymatches = mysql_num_rows($data); 

        if ($anymatches != 0) {
                $_SESSION['names']=$Customers;
    }

print_r($array) gives me the following:

Array ( [0] => Array ( [ID] => 25 [First_Name] => Maggy [Surname] => barrows [Company_Name] => squiggle [Telephone] => 12121212 [Alt_Telephone] => 4343434 [notes] => ) )

展开全部

  • 写回答

4条回答 默认 最新

  • douqi3913 2013-06-12 09:21
    关注

    Like that:

    function createDropDown($name = '', $options = array()) {
    $dropDown = '<select name="'.$name.'">';
    foreach ($options as $option => $value) {
        $dropDown .= '<option value='.$value['ID'].'>'.$value['First_Name'].' '.$value['Surname'].'</option>';
    }
    $dropDown .= '</select>';
    return $dropDown;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部