Show results from a query to three tables with INNER JOIN

0

I have a problem may be easier than I think but it really takes me several nights and I could not solve, the thing is that I have three related tables and I want to make a query that brings me data from each of them, I have the following tables, MEMBERS, CHILDREN, GROUPS that has the following rows:

The table children in the fields cedulaP and cedulaM is because it may be the cedula of the father and / or the mother, and the group table that is the group that belong, the question is because when making the query I shows the data of the father or mother, the information of the group that belong but to show the children in case of having more than one (1) child only shows the first and the others do not.

The fact is that it seems to me that it is in the php programming because the sql seems to be fine (I think), because if I delete some php codes if all the children appear to me, but it would not show me the father's data anymore. of the group, I do not know if I explain myself but I will try to show some images and codes.

if (isset($_GET['id_padre'])) { $id_padre = $_GET['id_padre']; $verPadre = mysqli_query($conectar, "SELECT * FROM miembros M INNER JOIN grupos G ON G.id_grupo = M.id_grupo LEFT JOIN hijos H ON H.cedulaP = M.cedula OR H.cedulaM = M.cedula WHERE M.id_miembro = '$id_padre'"); }

And this is the php code to show the results.

<table class="table table-hover">

                  <tbody>
                    <tr class='table-primary'>
                      <th colspan=4><span class="icon-description"></span> Datos Personales </th>
                    </tr>
                    <tr class='table-dark'>
                      <th>Nombre </th>
                      <th>C&eacute;dula </th>
                      <th>Fecha de N.</th>
                      <th>Edad</th>
                    </tr>
                      <?php
                        include '../extens/consultasSql.php';

                        $row = mysqli_fetch_array($verPadre);
                          $nombreGrupo = $row['nombre']; 
                          $nombre      = $row['nombre1'].' '.$row['nombre2'].' '.$row['apellido'].' '.$row['apellido2'];
                          $cedula      = $row['cedula'];
                          $id          = number_format($cedula,0,",",".");
                          $fecha       = $row['fecha'];
                          $idGrupo     = $row['id_grupo'];
                          $telefono    = $row['telefono'];
                          $correo      = $row['correo'];
                          $direccion   = $row['direccion'];
                          $talento     = $row['talento'];
                          $estado      = $row['estado'];
                          $pareja      = $row['pareja'];

                          echo "<tr class='table-dark'>
                                <td>$nombre</th>
                                <td>$id</th>
                                <td>$fecha1</td>
                                <td>$edad1</td>
                                </tr>
                                ";
                      ?>

                      <tr class='table-dark'>
                        <th >Direcci&oacute;n </th>
                        <th >Tel&eacute;fono </th>
                        <th >Correo</th>
                        <th >Estado C.</th>
                      </tr>
                      <?php 
                        echo
                          "<tr class='table-dark'>
                            <td>$direccion</th>
                            <td>$telefono</th>
                            <td>$correo</td>
                            <td>$estado</td>
                          </tr>
                            ";

                            ?>

                      <tr class='table-dark'>
                        <th colspan="4">Grupo Familiar</th>
                      </tr>
                      <?php 
                        echo
                          "<tr class='table-dark'>
                            <td colspan=4>$nombreGrupo</th>
                          </tr>
                            ";

                            ?>

                            <?php  

                            if ($estado == 'Casado (a)') {
                              echo "<tr class='table-primary'>
                                      <th colspan=4>Datos del Conyugue </th>
                                    </tr>";
                            }
                         ?>

                        <tr class='table-primary'>
                          <th colspan=4>Hijo (s) </th>
                        </tr>
                        <tr class='table-dark'>
                          <th >Nombre </th>
                          <th >C&eacute;dula </th>
                          <th >Fecha N.</th>
                          <th >Edad</th>
                        </tr>
                      <?php 

                      while ($rowHijo = mysqli_fetch_array($verPadre)) {
                          $nHijo     = $rowHijo['nombresHijo'].' '.$rowHijo['apellidosHijo'];
                          $cedulaHijo = number_format($rowHijo['cedulaHijo'],0,",",".");
                          $fechaHijo = $rowHijo['fechaHijo'];

                           echo
                          "<tr class='table-dark'>
                            <td>$nHijo</th>
                            <td><a href=''>$cedulaHijo</a></th>
                            <td>$fechaHijo</td>
                            <td>$edad1</td>
                          </tr>
                            ";  
                      }


                         ?>

                  </tbody>
                </table>

And it shows me something like this:

Clearly it shows data of a single son and in the table there are 2 children for that father or member.

Now, if you delete php code, if you show them to me, but, it would stop showing me the rest of the results.

<table class="table table-hover">

                  <tbody>
                    <tr class='table-primary'>
                      <th colspan=4><span class="icon-description"></span> Datos Personales </th>
                    </tr>
                    <tr class='table-dark'>
                      <th>Nombre </th>
                      <th>C&eacute;dula </th>
                      <th>Fecha de N.</th>
                      <th>Edad</th>
                    </tr>
                      <?php
                        include '../extens/consultasSql.php';

                         ?>

                        <tr class='table-primary'>
                          <th colspan=4>Hijo (s) </th>
                        </tr>
                        <tr class='table-dark'>
                          <th >Nombre </th>
                          <th >C&eacute;dula </th>
                          <th >Fecha N.</th>
                          <th >Edad</th>
                        </tr>
                      <?php 

                      while ($rowHijo = mysqli_fetch_array($verPadre)) {
                          $nHijo     = $rowHijo['nombresHijo'].' '.$rowHijo['apellidosHijo'];
                          $cedulaHijo = number_format($rowHijo['cedulaHijo'],0,",",".");
                          $fechaHijo = $rowHijo['fechaHijo'];


                           echo
                          "<tr class='table-dark'>
                            <td>$nHijo</th>
                            <td><a href=''>$cedulaHijo</a></th>
                            <td>$fechaHijo</td>
                            <td></td>
                          </tr>
                            ";  
                      }


                         ?>

                  </tbody>
                </table>

And it shows me something like this:

I would like you to help me correct my code so that you can show me all the required information, I thank you from now on.

    
asked by Perothebest24 29.04.2018 в 21:21
source

1 answer

0

After asking the question, I have an idea that has worked so far but if there is another one please let me know, what I have done is instead of being a single table that shows the results I have placed another table to show only the children.

    
answered by 29.04.2018 в 21:50