消息:在CodeIgniter中为foreach提供的参数无效

时间:2012-11-19 05:25:42

标签: php jquery codeigniter

我在Codeigniter的应用过程中遇到了一些问题 - 创建了一个像

这样的函数
function searchUnivtab() {
        $country = $this->input->post('countryKey');
        $state = $this->input->post('stateKey');
        $level = $this->input->post('level');
        $degType = $this->input->post('degType');       
        $country = str_replace('%20', ' ', $country);
        $state = str_replace('%20', ' ', $state);
        $degType = explode('~', $degType);
        $data = @$this->get->getSearchedUniversityTab($country, $state, $level, $degType[1]);
        $html = '';
        $i = 0;
        foreach($data as $d)
        {
            $html .= '<option value="'.$d['name'].'">'.$d['name'].'</option>';
        }
        echo $html; die;
    }

错误为:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()Line Number: 270位于foreach

与上述代码相关的任何帮助?

3 个答案:

答案 0 :(得分:1)

删除以下行中的@以查看它是否产生任何错误 所以替换

$data = @$this->get->getSearchedUniversityTab($country, $state, $level, $degType[1]);

$data = $this->get->getSearchedUniversityTab($country, $state, $level, $degType[1]);

答案 1 :(得分:0)

if(is_array($data)) {
    foreach($data as $d){
        $html .= '<option value="'.$d['name'].'">'.$d['name'].'</option>';
    }
} else {
    echo 'data is not an array.';
}

答案 2 :(得分:-1)

function getSearchUniversity() {
  country = jQuery('[name=countryKey]').val();
  state = jQuery('[name=stateKey]').val();
  level= jQuery('[name=level]').val();
  degType= jQuery('[name=degType]').val();
  jQuery.ajax({
    type: "POST",
    url:baseurl+ 'welcome/searchUnivtab', 
    cache: false,
    data: {countryKey: country, stateKey: state, level: level, degType: degType},
    error: function()
    {
      //notify('Error: Your request could be processed.. try again..!!');
    },
    success: function(html)
    {
      jQuery('[name=universityList]').html(html);
      return false;
    }
  }); 
}