How to load two tables in a single select (html)?

0

I have two customer tables, one called personalization and another called personalization I need to load both tables within 1 (a select) using php and codeigniter 3.6.

To load the personalization table, as follows the code in the controller and view below, remembering that the Attendance table has the foreign keys of the personal table (fkcodpf) and the personal table (fkcodpj):

CONTROLLER Attendance

class Atendimento extends CI_Controller {

public function cadastro() {


    $dados['pessoafisica'] = $this->db->get('pessoafisica')->result();        
    $this->load->view('includes/html_header');
    $this->load->view('includes/menu');
    $this->load->view('cadastrar/cad_atendimento', $dados);
}
}

Call Signup VIEW (select)

<label>Nome do Cliente</label>
<select id="fkcodpf" name="fkcodpf" class="form-control" >
<option value=""></option>  
<?php foreach ($pessoafisica as $pf) { ?>
<option value="<?= $pf->codpf ?>"> <?= $pf->nomepessoafisica; ?></option>
<?php } ?>
</select> 

But how do you load together in this select, the personajuridica table? If possible by ordering the personal name, followed by the personal name?

    
asked by anonymous 08.02.2018 / 23:53

1 answer

0

If I understand you correctly, you want to merge the personal data table data with the legal entity tables into a single select element. I will not comment on the complication that you will have to differentiate the data in the application (who is PJ and who is PF), because you should already have this in mind at the time of validation. In my opinion you should use another element and JavaScript , but that is not the scope of the question.

But it should be noted that this should be heavy to load. If the client machine is not there these things, it will not be funny to use. I simulated 4290 records, and it was not very cool response time.

That being said, as far as I can see, CodeIgniter does not have the possibility of running UNION in the native library. But you have the option to run a QUERY in text format ( read more here ) .

Knowing this, it follows the model method:

function select_duas_tabelas(){
        $sql = "SELECT cpf as id,nome FROM tb_pessoa_fisica ";
        $sql.= "UNION ALL ";
        $sql.= "SELECT cnpj,nome FROM tb_pessoa_juridica ";
        $sql.= "ORDER BY nome ASC";
        $query = $this->db1->query($sql);
        return $query->result_array();
    }

Basically, cpfs and cnpjs will be called 'id' in this query, and will take the place of the id in 'fkcodpf'

controller loading the data into view :

function select_duas_tabelas(){
    $data['title'] = $this->lang->line('pages_select_duas_tabelas_title');
    $data['dados'] = $this->teste->select_duas_tabelas();
    $this->template->load('template/index', 'pages/select_duas_tabelas', $data);
}

Finally, view showing your select element with all PF and PJ data:

<label>Nome do Cliente</label>
<select id="fkcodpf" name="fkcodpf" class="form-control" >
    <option value=""></option>  
    <?php
    foreach ($dados as $p) { ?>
        <option value="<?=$p['id']?>"><?=$p['nome'];?></option>
    <?php } ?>
</select>

That's it.

    
13.02.2018 / 15:55