Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!