Home > Software design >  (PHP) How to fetch one index on the fetch assoc and then continue to the another in a while loop?
(PHP) How to fetch one index on the fetch assoc and then continue to the another in a while loop?

Time:01-30

The problem is that when I fetch assoc of the "Descripcion" in english "Description" field of the mysql database it fetchs two index per email, and I don't want that. I want it to fetch one index per loop and then go to another one and so on. I didn't know if I explained myself so bad xD

Welp, let's see the code.

while ($linea = $resultado->fetch_assoc()) {
                    $nombreQuery = "SELECT * FROM Usuarios WHERE Email = " . "'" . $linea['Email'] . "'";
                    $apellidosQuery = "SELECT Apellidos FROM Usuarios Where Email = " . "'" . $linea['Email'] . "'";
                    $telefonoQuery = "SELECT Telefono FROM Usuarios WHERE Email = " . "'" . $linea['Email'] . "'";
                    $conceptoQuery = "SELECT Descripcion FROM Citas WHERE Email = " . "'" . $linea['Email'] . "' LIMIT $i";
                    $fecha = date('d-m-Y', strtotime($linea['FechaCita']));
                    $hora = date('H:i:s', strtotime($linea['HoraCita']));
                    $email = $linea['Email'];
                    borrarCita($bd, $fecha, $email);
                    $resultadoN = $bd->query($nombreQuery);
                    if ($lineas = $resultado->num_rows != 0) {
                        while ($linea = $resultadoN->fetch_assoc()) {
                            $nombre = $linea['Nombre'];
                        }
                        $resultadoA = $bd->query($apellidosQuery);
                        $lineasA = $resultadoA->num_rows;
                        if ($lineasA != 0) {
                            while ($lineaA = $resultadoA->fetch_assoc()) {
                                $apellidos = $lineaA['Apellidos'];
                            }
                        }
                        $resultadoT = $bd->query($telefonoQuery);
                        $lineasT = $resultadoT->num_rows;
                        if ($lineasT != 0) {
                            while ($lineaT = $resultadoT->fetch_assoc()) {
                                $telefono = $lineaT['Telefono'];
                            }
                        }
                        $resultadoC = $bd->query($conceptoQuery);
                        if($lineas = $resultadoC->num_rows != 0) {
                            while($lineaC = $resultadoC->fetch_assoc()) {
                                $concepto = $lineaC['Descripcion'];
                            }
                        }
                    }

Let's explain the code. I'm trying to print every field on the database and make a materializecss card-panel with every one like: Name, Surname, Email, Phone Number, Description stuff like that. I've already done this, but for some reason, it did stop working and I don't know why. First of all, I think it is obvious that the algorythm I'm using isn't the best, and I'm not using it because it probably saturates the database, it's only for this example, I think it's cleaner to see. Here I leave an image of how was the website working and then I'll leave an image of how is the website now working. I hope you can help me. Notice that I'm new on PHP and I try my best to learn this awesome language :D. Regards to everyone and thank you all for your attetion. All my bests for you <3

How it was working: (The underlined fields are the description fields)

And then how it is working: As you can see here the description fields are overlapping

And then this is a screenshot of the mysqli database.

CodePudding user response:

In your first inner while loop, you re-define the $linea variable which the outer while loop uses, this may be causing your problem

CodePudding user response:

I found the answer my way.

<?php
                $i = 0;
                $queryCitas = "SELECT * FROM Citas";
                $resultadoCitas = $bd->query($queryCitas);  
                $lineasR = $resultadoCitas->num_rows;
                if($lineasR != 0) {
                    while($lineaR = $resultadoCitas->fetch_assoc()) {
                        $email = $lineaR['Email'];
                        $queryU = "SELECT * FROM Usuarios WHERE Email = '$email'";
                        $resultadoU = $bd->query($queryU);
                        $resultadoC = $bd->query($queryC);
                        while ($linea = $resultadoU->fetch_assoc()) {
                            $i  ;
                            $nombre = $linea['Nombre'];
                            $apellidos = $linea['Apellidos'];
                            $email = $linea['Email'];
                            $telefono = $linea['Telefono'];
                            $fecha = $lineaR['FechaCita'];
                            $hora = $lineaR['HoraCita'];
                            $concepto = $lineaR['Descripcion'];
                            ?>

By the way, sorry Ryan Vincent, I couldn't make it without putting the assignment into the while:( Thank you all for your answers, you're awesome <3

  •  Tags:  
  • Related