如何在php codenngiter中的视图页面中获取多个值

时间:2018-05-07 08:55:01

标签: php mysql codeigniter

城市显示在视图页面中,如 chennaihyderabadmumbai 。我需要展示 chennai,hyderabad,mumbai 。 请帮助

控制器代码:

$data['jobCityName'] =explode(',',$viewData['jobCity']);
foreach($data['jobCityName'] as $cityid) {        
    $data['jobCityNames']=$this->hrm_model->getCitybyId($cityid);
    $data['jobCity'].=$data['jobCityNames']['cityName'];

型号代码

function getCitybyId($city)
    {
        $this->db->select('cityName');
        $this->db->from('city');
        $this->db->where("city_id", $city);
        $query = $this->db->get();
        return $query->row_array();
    }

4 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

更改您的模型:

function getCitybyId($city)
{
    $this->db->select('cityName');
    $this->db->from('city');
    $this->db->where("city_id", $city);
    $query = $this->db->get();
    if ($query->num_rows() > 0 )
    {
       return $query->row()->cityName;
    }

}

在控制器

$data['jobCityName'] = explode(',',$viewData['jobCity']);
foreach($data['jobCityName'] as $cityid) {        
    /*$data['jobCityNames']=$this->hrm_model->getCitybyId($cityid);*/
    $data['jobCity'][] = $this->hrm_model->getCitybyId($cityid);
}
$data['jobCity'] = implode(',',$data['jobCity']);

对于单个城市使用此:

$data['single_city_name'] = $this->hrm_model->getCitybyId($cityid);

将其传递给视图:

$this->load->view('your_view',$data);

答案 1 :(得分:0)

如果你想展示像chennai,hyderabad,mumbai这样的城市,请试试这个。

Applications/STS/Eclipse/plugins

答案 2 :(得分:0)

除第一项外,请在逗号旁边加上逗号:

$data['jobCityName'] =explode(',',$viewData['jobCity']);
$isFirst = true;
foreach($data['jobCityName'] as $cityid) {        
    $data['jobCityNames']=$this->hrm_model->getCitybyId($cityid);
    $data['jobCity'].=$data['jobCityNames']['cityName'].($isFirst ? "" : ",");
    $isFirst = false;
}

答案 3 :(得分:0)

使用result_array()函数来隐藏多个值

return $query->result_array();