如何在php codengiter的views页面中获取多个值

mzmfm0qo  于 2021-06-25  发布在  Mysql
关注(0)|答案(4)|浏览(262)

城市显示在视图页面中,如下面的钦奈海得拉巴。我需要展示金奈,海得拉巴,孟买。请帮帮我
控制器代码:

$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();
    }
2nbm6dog

2nbm6dog1#

希望这能帮助您:
更改您的模型如下:

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);
aij0ehis

aij0ehis2#

使用result_array()函数返回多个值

return $query->result_array();
bqjvbblv

bqjvbblv3#

如果你想展示金奈、海得拉巴、孟买等城市,试试这个。

<?php echo $jobCity.","; ?>
wnavrhmk

wnavrhmk4#

除第一项外,在此处加逗号:

$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;
}

相关问题