dqeq885710 2017-04-25 10:51
浏览 52

使用fetchAll循环的MySQL输出不一致

I am having problems with my query and wondered if anyone has any advice to where I'm going wrong. I have a time slot booking system for testing machines in my company

I have two tables, one called event_machine_time that handles the machine names and timings and the other event_bookings that handles the booking

event_machine_time // Table Name
id
machine_name // Laptop Name e.g. Laptop 1
start_time // Start Time
end_time // End Time

Each 'machine_name' is added three times, one for each of the time slots:

**********************************************
* id * machine_name * start_time *  end_time *
**********************************************
* 1  * Laptop 1     * 09:30:00   * 11:00:00  *
* 2  * Laptop 1     * 11:30:00   * 13:00:00  *
* 3  * Laptop 1     * 13:30:00   * 15:00:00  *
* 4  * Laptop 2     * 09:30:00   * 11:00:00  *
* 5  * Laptop 2     * 11:30:00   * 13:00:00  *
* 6  * Laptop 2     * 13:30:00   * 15:00:00  *
**********************************************

This then is then linked to the 'event_booking' via a row called machine_id table which holds all the booking data. The key part to where I am going wrong is in the information_id as I must be calling it wrong!

I want to call all the machine_name rows but check if there is a match on event_booking on both machine_id and information_id so I can disable to booking link.

Here is my current code:

$Event_Times = $dbconn->query('SELECT 
                                   event_machine_time.machine_name,
                                   event_machine_time.start_time,
                                   event_machine_time.id AS machine_id,
                                   event_booking.information_id
                                   FROM event_machine_time
                                   LEFT JOIN event_booking
                                   ON event_machine_time.id=event_booking.machine_time_id
                                   WHERE event_booking.information_id = '.$_GET["id"].' OR event_booking.information_id IS NULL
                                   ORDER BY start_time
                                   ');
    $Event_Times_Loop = $Event_Times->fetchAll(PDO::FETCH_ASSOC);

I don't normally use $_GET["id"] directly in a SQL but I used it as a quick example as this is how the page knows what the event date is.

One of the 7 loops:

    <td>
    <?php foreach ($Event_Times_Loop as $Event_Times_Row) { ?>
    <?php if($Event_Times_Row["machine_name"] == "Laptop 1") { ?>
                        <a data-title="Book <?php echo date("G:i", strtotime($Event_Times_Row["start_time"])); ?> slot?" class="button<?php if($Event_Times_Row["information_id"] == $_GET["id"]) { echo " button-disabled"; } ?> msg_testing_booking" href="make_booking.php?<?php echo "i_id=".$Event_Data_ID."&m_id=".$Event_Times_Row["machine_id"].""; ?>"><?php echo date("G:i", strtotime($Event_Times_Row["start_time"])); ?></a>
 <?php } } ?>
 </td>

So from the code above if a record is found to match in event_booking it disables the button but it appears to output differently on each testing day

This is one testing day: enter image description here

This is another: enter image description here

I can see the relationship where its only showing free slots from the other page, but now its hiding the other slots as its behaving as if there booked, but for some reason hiding them.

Its just a bit beyond my knowledge and any help would be amazing!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看