由于另一列,PHP echo几次都是整个列

时间:2017-04-29 05:59:44

标签: php mysql

我必须建立一个网站,从数据库中显示有关“酒店”的信息。我有2个表:'locations'和'rating'。 问题:如果我的评分超过1,则具有相同locationID的整个循环将使用2个不同的评论打印两次。我应该使用除LEFT JOIN之外的其他东西吗?

当前输出: Current

计划输出: 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>";
        ?>

1 个答案:

答案 0 :(得分:0)

你可以尝试一下:

$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;";

注意:不确定我在这里假设,您只需要总评分