Codeigniter从两个不同的模型中获取数据并在一个视图中显示

时间:2016-03-30 20:54:42

标签: php codeigniter

我正在开展一个项目,我有客户和预订。我可以创建新客户端,获取它们并列出它们。但是,在预订时,我需要能够显示来自一个模型和控制器的客户端名称到我的实际视图,即预留模型和控制器。

我正在列出客户,并添加指向另一个页面的链接,在该页面中我会显示客户名称,预订ID以及预订中的所有其他信息。

我是编程的新手,我真的会帮助你。提前谢谢。

我的客户端控制器列出客户端,以便我可以进行预订。

public function listarr(){
    $nome = $this->session->userdata("nome");
    $this->load->view("template/topo");
    $this->load->library('pagination');
    $this->load->model('Clientes');
    $config = array();
    $config['base_url'] = base_url().'cliente/listarr';
    $config['total_rows'] = $this->Clientes->count_tblclientes();
    $config['per_page'] = 10;
    $config['num_links'] = 5;
    //$config['uri_segment'] = 3;

    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] = "</ul>";
    $config['full_tag_open'] = "<li>";
    $config['full_tag_close'] = "</li>";
    $config['full_tag_open'] = "<li class ='disabled'><li class='active'><a href='#'>";
    $config['full_tag_close'] = "<span class='sr-only'></span></a></li>";
    $config['full_tag_open'] = "<li>";
    $config['full_tag_close'] = "</li>";
    $config['full_tag_open'] = "<li>";
    $config['full_tag_close'] = "</li>";
    $config['full_tag_open'] = "<li>";
    $config['full_tag_close'] = "</li>";
    $config['full_tag_open'] = "<li>";
    $config['full_tag_close'] = "</li>";



    $this->pagination->initialize($config);
    $page = $this->uri->segment(3);
    $data['tblclientes'] = $this->Clientes->fetch_tblclientes($config['per_page'], $page );
    $data['links'] = $this->pagination->create_links();
    $data['message'] = '';

    $this->load->view("cliente/listarr",$data, array (
        'nome' => $nome));
    $this->load->view("template/rodape");
}

我的预订控制器,我想显示来自客户的名称数据,并显示预订中的其他数据。

public function ficha_completa($id) {
    $this->load->view("template/topo");
    $this->load->model("Reservas", "rowDao");
     $resp = "";

    if($_POST){

    $nomecliente = $this->input->post("nomecliente");
        $datar = implode("-", array_reverse(explode("/", $this->input->post("datar"))));
        $codcliente = $this->input->post("codcliente");
        $adultos = $this->input->post("adultos");
        $precoadulto = $this->input->post("precoadulto");
        $criancas = $this->input->post("criancas");
        $precocrianca = $this->input->post("precocrianca");
        $datasinal = implode("-", array_reverse(explode("/", $this->input->post("datasinal"))));
        $valor = $this->input->post("valor");
        $obs = $this->input->post("obs");
        $data2 = implode("-", array_reverse(explode("/", $this->input->post("data2"))));
        $valor2 = $this->input->post("valor2");
        $obs2 = $this->input->post("obs2");
        $data3 = implode("-", array_reverse(explode("/", $this->input->post("data3"))));
        $valor3 = $this->input->post("valor3");
        $obs3 = $this->input->post("obs3");
        $locou = $this->input->post("locou");
        $excursao = $this->input->post("excursao");
        $referencia = $this->input->post("referencia");
        $grupo = $this->input->post("grupo");
        $obsfinal = $this->input->post("obsfinal");


        $data = array(
            'nomecliente' => $nomecliente,
            'datar' => $datar,
            'codcliente' => $codcliente,
            'adultos' => $adultos,
            'precoadulto' => $precoadulto,
            'criancas' => $criancas,
            'precocrianca' => $precocrianca,
            'datasinal' => $datasinal,
            'valor' => $valor,
            'obs' => $obs,
            'data2' => $data2,
            'valor2' => $valor2,
            'obs2' => $obs2,
            'data3' => $data3,
            'valor3' => $valor3,
            'obs3' => $obs3,
            'locou' => $locou,
    'excursao' => $excursao,
    'referencia' => $referencia,
    'grupo' => $grupo,
    'obsfinal' => $obsfinal,

        );

        $this->rowDao->atualizar($data);
        $resp = "Pessoa editada com sucesso!";
    }

    $row = $this->rowDao->buscarPorId($id);

    $this->load->view("reserva/ficha_completa", array(
        'row' => $row,
        'resp' => $resp
    ));
    $this->load->view("template/rodape");
}

我的预订模式

 public function buscarPorId($id){
    $this->db->where("codreserva", $id);
    return $this->db->get("reservas")->first_row();
}

我的html在视图中显示数据

                <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="last-name">Codigo do Cliente 
                  </label>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <input readonly="readonly" type="text" id="codcliente" name="nomecliente" value="<?php echo $row->nomecliente; ?>" class="form-control col-md-7 col-xs-12">
                  </div>
                </div>
                <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="last-name">Codigo da Reserva 
                  </label>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <input readonly="readonly" type="text" id="codcliente" name="codreserva" value="<?php echo $row->codreserva; ?>" class="form-control col-md-7 col-xs-12">
                  </div>
                </div>
                <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name" >Data da reserva 
                  </label>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <input readonly="readonly" type="text" id="datar" name="datar" value="<?php echo $row->datar; ?>" class="form-control col-md-7 col-xs-12" data-inputmask="'mask': '99/99/9999'">
                  </div>
                </div>

也是为了显示,这是我保存客户端数据的控制器

    public function index(){
    $this->load->view("template/topo");
    $resp = "";
    //Se os dados chegarem via POST
    if($_POST){

        $NomeCliente = $this->input->post("NomeCliente");
        $Endereco = $this->input->post("Endereco");
        $Bairro = $this->input->post("Bairro");
        $Cidade = $this->input->post("Cidade");
        $UF = $this->input->post("UF");
        $CEP = $this->input->post("CEP");
        $CGC_CPF = $this->input->post("CGC_CPF");
        $DataNasc = implode("-", array_reverse(explode("/", $this->input->post("DataNasc"))));
        $CodSexo = $this->input->post("CodSexo");
        $TelResid = $this->input->post("TelResid");
        $TelComerc = $this->input->post("TelComerc");
        $Ramal = $this->input->post("Ramal");
        $Celular = $this->input->post("Celular");
        $E_mail = $this->input->post("E_mail");
        $Telrecado = $this->input->post("Telrecado");
        $Fax = $this->input->post("Fax");
        $Observacoes = $this->input->post("Observacoes");




        $dados = array(
            'NomeCliente' => $NomeCliente,
            'Endereco' => $Endereco,
            'Bairro' => $Bairro,
            'Cidade' => $Cidade,
            'UF' => $UF,
            'CEP' => $CEP,
            'CGC_CPF' => $CGC_CPF,
            'DataNasc' => $DataNasc,
            'CodSexo' => $CodSexo,
            'TelResid' => $TelResid,
            'TelComerc' => $TelComerc,
            'Ramal' => $Ramal,
            'Celular' => $Celular,
            'E_mail' => $E_mail,
            'Telrecado' => $Telrecado,
            'Fax' => $Fax,
            'Observacoes' => $Observacoes,

        );

        //Carregar o Model Pessoas
        $this->load->model("Clientes", "pDao");
        if($this->pDao->gravar($dados))
            $resp = $NomeCliente . ", enviado com sucesso!";
        else
            $resp = $NomeCliente . ", nao enviado!";

    }

    $this->load->view("cliente/index", 
            array('resp' => $resp));
    $this->load->view("template/rodape");
}

1 个答案:

答案 0 :(得分:0)

在您的控制器中,您可以在单个视图中加载许多模型并放置模型数据

在您的控制器中首先加载您的所有模态

$this->load->model(array('modal1','modal2','modal3'));

<强>控制器

 data['modaldata_1']=$this->modal1->model_1();
 data['modaldata_2']=$this->modal2->model_2();
 data['modaldata_3']=$this->modal3->model_3();
 //putting all data in view 
 $this->load->view('yourview',$data);

查看  你可以打印你的数据。

 print_r($modaldata_1);
 print_r($modaldata_2);
 print_r($modaldata_3);