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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作