phpmysqlajax

Display multiple appointment details


I am creating a appointment booking system but at the moment I am having troubles to display multiple appointments. I am echoing only one appointment at the moment, my goal is for the user to view all of there appointments in a while or for loop. I am unsure of the correct style which is needed to display the values of the appointment for the second appointment etc. In my database there a many appointments needed to be viewed for the logged in user.

<?php 
  $user_id = $_SESSION['user_id'];
  $sql = "SELECT
    points.appoint_date, 
    beauticians.b_firstname,
    users.username,
    service.service_name
  FROM points 
  INNER JOIN service ON service.service_id = service.service_id
  INNER JOIN beauticians ON beauticians.beautician_id = beauticians.beautician_id
  INNER JOIN users ON users.id = users.id";

  $query = $connect->query($sql);
  $result = $query->fetch_array();

  // close database connection
  $connect->close();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>

<ul>
  <li>Hello, <?php echo $result['username'] ?> </li>
  <li>Hello, <?php echo $result['b_firstname'] ?> </li>
  <li>Hello, <?php echo $result['service_name'] ?> </li>
  <li>Hello, <?php echo $result['appoint_date'] ?> </li>
</ul>
</body>
</html

Solution

  • You have to fetch each row of the result, and display it.

    <?php 
    $user_id = $_SESSION['user_id'];
    
    $sql = "SELECT 
        points.appoint_date, 
        beauticians.b_firstname, 
        users.username, 
        service.service_name
    FROM points 
    INNER JOIN service ON service.service_id = service.service_id
    INNER JOIN beauticians ON beauticians.beautician_id = beauticians.beautician_id
    INNER JOIN users ON users.id = users.id";
    
    $query = $connect->query($sql);
    
    // close database connection
    $connect->close();
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
    <?php
        while($result = $query->fetch_array()) {
    ?>
    <ul>
        <li>Hello, <?php echo $result['username'] ?> </li>
        <li>Hello, <?php echo $result['b_firstname'] ?> </li>
        <li>Hello, <?php echo $result['service_name'] ?> </li>
        <li>Hello, <?php echo $result['appoint_date'] ?> </li>
    </ul>
    <?php
        }
    ?>