douluo2930 2013-06-12 16: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 17: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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度