duancong7358 2018-08-26 05:01
浏览 305
已采纳

如何显示数据库中不存在的所有列表记录事件? mysql php

I have a list of rooms

  1. Deluxe
  2. Standard
  3. Regular

I made a display records based on what the user added per day.

If the user add a new event and use one of the room.

Example: for this day August 26,2018. one event was booked, and it will display the ff. Retrieved from database.

Deluxe - 1 booked

What I want to display is this:

Deluxe - 1 booked

Standard - none

Regular - none

I want to add all the list of rooms even some of the rooms has no value in database or doesn't exist in database.

But how can I do it? Please help!

My sample code:

    $connect = mysqli_connect("localhost", "root", "", "bookings");
    $sql = "SELECT * FROM events WHERE (start BETWEEN '$start' AND '$end' OR 
    end BETWEEN '$start' AND '$end');  
    $result = mysqli_query($connect, $sql);
    while($rows = mysqli_fetch_array($result))
    {
    echo $rows['room'];
    }
  • 写回答

1条回答 默认 最新

  • douchaqi3369 2018-08-26 05:41
    关注

    Based on the OP's comments, you should get count of booked rooms per category using a GROUP BY clause. If there is no entry found for a particular room category, then you should let your application code handle this. Here is a sample code:

    $connect = mysqli_connect("localhost", "root", "", "bookings");
    $sql = "SELECT room, COUNT(id) as rooms_booked 
             FROM events 
             WHERE ((start BETWEEN '$start' AND '$end') OR 
                    (end BETWEEN '$start' AND '$end')
                   ) 
             GROUP BY room";  
    $result = mysqli_query($connect, $sql);
    
    // all possible room categories
    $all_room_categories = array('Deluxe', 'Standard', 'Regular');
    // room categories for which there is some booking done
    $booked_room_categories = array();
    
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
        echo $row['room'] . " - " . $row['rooms_booked'] . " booked" . "<\br>";
        $booked_room_categories[] = $row['room'];
    }
    
    // Now find all the room categories for which no booking was found
    $unbooked_room_categories = array_diff($all_room_categories, $booked_room_categories);
    
     // Loop over unbooked rooms to print their status as well
    foreach ($unbooked_room_categories as $cat)
    {
        echo $cat . " - " . " none" . "<\br>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀