如何在codeigniter示例中使用foreach($ data as $ buff)进行双循环第一个选择目标和第二个表单选择是离开

时间:2018-05-19 07:55:27

标签: jquery arrays ajax codeigniter

public function get_trayek() {
        $transportation=$this->input->post('id_transportation');
        $data=$this->main_model->get_trayek($transportation);
        $data_trayek='<option value="">----- Pilih Destination -----</option>';
        foreach($data as $buff){
            $data_trayek.= '<option value="'.$buff->Id_trayek.'">'.$buff->destination.'</option>';
        }


        $data_trayek1='<option value="">----- Pilih Departure -----</option>';


     1. 
     foreach($data as $buff){
                $data_trayek1.= '<option value="'.$buff->Id_trayek.'">'.$buff->departure.'</option>';

        }
        echo json_encode(array('data'=>$data_trayek));
    }

2 个答案:

答案 0 :(得分:0)

试试这段代码。

public function get_trayek() {
    $transportation=$this->input->post('id_transportation');
    $data=$this->main_model->get_trayek($transportation);
    $data_trayek='<option value="">----- Pilih Destination -----</option>';
    foreach($data as $buff){
        $data_trayek.= '<option value="'.$buff->Id_trayek.'">'.$buff->destination.'</option>';
    }

    $data_trayek1='<option value="">----- Pilih Departure -----</option>';

    foreach($data as $buff){
            $data_trayek1.= '<option value="'.$buff->Id_trayek.'">'.$buff->departure.'</option>';

    }
    echo json_encode(array('data'=>array("destination"=>$data_trayek,"departure"=>$data_trayek1));
}

您可以按data.destinationdata.departure

进行回复

答案 1 :(得分:0)

您可以在单个循环中执行此操作,例如

public function get_trayek() {
        $transportation=$this->input->post('id_transportation');
        $data=$this->main_model->get_trayek($transportation);
        $data_trayek='<option value="">----- Pilih Destination -----</option>';
        $data_trayek1='<option value="">----- Pilih Departure -----</option>';

        foreach($data as $buff){
            $data_trayek.= '<option value="'.$buff->Id_trayek.'">'.$buff->destination.'</option>';
            $data_trayek1.= '<option value="'.$buff->Id_trayek.'">'.$buff->departure.'</option>';
        }
        echo json_encode(array('data'=>array("destination"=>$data_trayek,"departure"=>$data_trayek1));
    }