Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
PHP echo's the entire column several times due to another columns

I have to make a website that will display info about "hotels" from a database. I have 2 tables: 'locations' and 'ratings'. The problem: If I have more than 1 rating, the entire loop with same locationID get's printed twice with the 2 different comments. Should I use something else than LEFT JOIN for this?

CURRENT OUTPUT: Current

PLANNED OUTPUT: Planned

$sql = "SELECT * FROM locations LEFT JOIN ratings ON ratings.locationID=locations.locationID WHERE locationKind = 'hotel'";
    $result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
        $image = $row["image"];
        $locationName = $row["locationName"];
        $description = $row["description"];
        $link = $row["link"];
        $comment = $row["comment"];

        echo "<tr>";
          echo "
          <td><img style='width:100%;height:100%;border-radius:8px;' src='images/$image'/></td>";
          echo "<td style='padding: 0 30px;'><a class='locationName'>$locationName</a><br/><br/>$description</br></br><div class='bookNowButton'><a href='$link' class='bookNowButtonText'>BOOK NOW!</a><div></td>";
          echo "<td>
          <br>/<br>
                    <a>Booking.no</a></td>";
        echo "</tr>";
      }
      echo "</table>";
        ?>
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can try it:

$sql = "SELECT locations.*, COUNT(ratings.locationID) as rating FROM locations LEFT JOIN ratings ON ratings.locationID=locations.locationID WHERE locationKind = 'hotel' GROUP BY ratings.locationID;";

Note: unsure I've assume here, You need only total rating

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda