dongqiang2069 2015-01-09 01:30
浏览 158
已采纳

无法对下拉列表进行数据绑定

I'm having issues databinding a dropdown list. I'm following a MVC structure and this is how I did it.

Here is a function from my Model layer:

function GetTillverkare()
{
    $data = array();
    mysql_set_charset('utf8');
    $query = "Select Namn from Tillverkare";
    if(!$sql = mysql_query($query)) {
        throw new exception("Error: Can not execute the query.");
    } else {
        $num = mysql_num_rows($sql);
        if($num>0)
        {
            for($i=0; $i<$num; $i++)
            {
                $data[$i] = mysql_fetch_array($sql);
            }
        }
    }
    return $data;

}

Here is the code from my Controller layer:

$displayResults = new Sok() //Sok is my model class.

$GetTillverkare = $displayResults->getTillverkare(); 
//I am able to print the $GetTillverkare so there is no problem with getting the data.

Here is my View layer

Fabrikat:<br /> <select name="Tillverkare_search" id="Tillverkare_search">
        <option value="" selected="selected">Pick</option>
        <option value="<?php echo $GetTillverkare ?>"</option>

    </select><br/>

I don't get any error, but it doesn't display data o the dropdown list. It's empty

  • 写回答

1条回答 默认 最新

  • drudfe0446838 2015-01-09 01:39
    关注

    Your model function returns array so you can't print array by echo , if each index of array is element of this list try to print this way

    for($i=0;$<sizeof($GetTillverkere;$i++){
     echo "<option value='".$GetTillverkere[$i]."'>".$GetTillverkere[$i]."</option>";
     }
    

    and you also have unclosed html tag in second option tag , and you try to print name to value of option but no in between tags which is the visible part of this tag

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题
  • ¥15 一道计算机组成原理问题