How to show data of 2 tables in a select

0

I have 2 tables account1 and account2

account1 with the columns [account_code1] and [account_name1]

account2 with the columns [account_code2] and [account_name2]

I need your help to show the data of account1 and account2 in the select

here the code:

<?php

if(!isset($conexion)){
  include ("conexion.php");
}

$query = "SELECT codigo_cuenta1, nombre_cuenta1 FROM cuenta1 ";
$resultado = $conexion->query($query);

?>

                  <div class="row">
                    <div class="col-lg-6">
                      <label for="cbx_cuenta1" class="control-label">Seleccionar Cuenta1</label>
                      <select name="cbx_cuenta1" id="cbx_cuenta1" class="form-control"  required title="Debe seleccionar una cuenta">
                      <option value="">Seleccionar</option>
                      <?php while($row = $resultado->fetch_assoc()) { ?>
                      <option value="<?php echo $row['codigo_cuenta1']; ?>"><?php echo $row["codigo_cuenta1"] .". ". utf8_encode($row["nombre_cuenta1"]); ?></option>
        <?php } ?>
    
asked by outsider 11.11.2018 в 08:10
source

2 answers

1

You can use UNION in your query and problem solved. You should only change your code leaving something like this:

<?php

if(!isset($conexion)){
  include ("conexion.php");
}

$query = "(SELECT codigo_cuenta1 AS codigo, nombre_cuenta1 as nombre FROM cuenta1) UNION (SELECT codigo_cuenta2 AS codigo, nombre_cuenta2 AS nombre FROM cuenta2)";
$resultado = $conexion->query($query);

?>

                  <div class="row">
                    <div class="col-lg-6">
                      <label for="cbx_cuenta1" class="control-label">Seleccionar Cuenta1</label>
                      <select name="cbx_cuenta1" id="cbx_cuenta1" class="form-control"  required title="Debe seleccionar una cuenta">
                      <option value="">Seleccionar</option>
                      <?php while($row = $resultado->fetch_assoc()) { ?>
                      <option value="<?php echo $row['codigo_cuenta1']; ?>"><?php echo $row["codigo_cuenta1"] .". ". utf8_encode($row["nombre_cuenta1"]); ?></option>
        <?php } ?>

Important The fields you are going to join from both tables must have the same name. Otherwise it will not work. In this case, I used alias. Greetings!

    
answered by 11.11.2018 / 19:08
source
0

You could pre-prepare a single output array with the data from both queries:

    $sale = [];
$sale['cod_cuenta'] = [];
$sale['nombre_cuenta'] = [];


$a = $db->query("SELECT codigo_cuenta1, nombre_cuenta1 FROM cuenta1");
$b = $db->query("SELECT codigo_cuenta2, nombre_cuenta2 FROM cuenta2");

while($row = $a->fetch_array()){
    array_push($sale['cod_cuenta'], $row['codigo_cuenta1']);
    array_push($sale['nombre_cuenta'], $row['nombre_cuenta1']);
}
while($row = $b->fetch_array()){
    array_push($sale['cod_cuenta'], $row['codigo_cuenta2']);
    array_push($sale['nombre_cuenta'], $row['nombre_cuenta2']);
}

and then you go through that array to generate the options of your selector, there may be something more elegant, but that's what I think of now. Greetings

    
answered by 11.11.2018 в 17:46