使用PHP/MySQL/Ajax的队列表

使用PHP/MySQL/Ajax的队列表,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有数据库表schedule,其中包含id,日期,时间,文件柜列。 此外,我还有带有id,cabinetNumber,title列的表文件柜。 我应该生成表,但它应该看起来像队列表 cabinet 8,9,10,11,12,13,14,15,16 102 - - - + - - + - - 103 - + - - - - + - - 如果时间是繁忙的,它将是+,如果是空闲的- 我需要修改我的mysql查询。并且应该可以更改日期(使用datepicke

我有数据库表
schedule
,其中包含
id
日期
时间
文件柜
列。 此外,我还有带有
id
cabinetNumber
title
列的表
文件柜
。 我应该生成表,但它应该看起来像队列表

cabinet 8,9,10,11,12,13,14,15,16
  102   - - -  +  -   -  +  - - 
  103   - + -  -  -   -  +  - - 
如果时间是繁忙的,它将是
+
,如果是空闲的
-

我需要修改我的mysql查询。并且应该可以更改日期(使用datepicker和Ajax)并根据日期更改表行值。 但若日期不存在,查询将为空,表将不包含行。我怎样才能修好它

MySQL查询:

$q = $_GET['date'];
$query = mysql_query("SELECT date,cabinet,
   SUM(IF(ROUND(`time`)=8,1,0)) as h8,
   SUM(IF(ROUND(`time`)=9,1,0)) as h9,
   SUM(IF(ROUND(`time`)=10,1,0)) as h10,
   SUM(IF(ROUND(`time`)=11,1,0)) as h11,
   SUM(IF(ROUND(`time`)=12,1,0)) as h12,
   SUM(IF(ROUND(`time`)=13,1,0)) as h13,
   SUM(IF(ROUND(`time`)=14,1,0)) as h14,
   SUM(IF(ROUND(`time`)=15,1,0)) as h15
FROM `schedule` WHERE date = '".$q."'
GROUP BY cabinet") or die(mysql_error());
?>
表:

<table class="table table-hover">
    <tr class=".info"><td>Cab</td><td >8:00</td><td >9:00</td><td >10:00</td><td >11:00</td><td >12:00</td><td >13:00</td><td >14:00</td><td >15:00</td></tr>
    <!--  -->
    <?php while($data=mysql_fetch_array($query)) :?>
        <tr>
            <?php  $cabinet = $data['cabinet']; ?>
            <td><?=$cabinet ?></td>
            <?php  for($j=8; $j<=15; $j++)  : ?>
                <?php
                $busy = $data['h'.$j];
                ?>
                <?php if($busy>0 && $data['date']===$q ): ?>
                    <td class="busy"></td>
                <?php else: ?>
                    <td class="free">
                        <form action="1.php" method="post">
                            <input type="hidden" name="time"  value="<?= $j;?>" />
                            <input type="hidden" name="cabinet"  value="<?= $cabinet;?>" />
                            <input type="submit" style="free" value="" name="sub"/>
                        </form>
                    </td>
                <?php endif?>

            <?php  endfor ?>
        </tr>
    <?php endwhile ?>
</table>

关键是保持查询简单,使用PHP处理和显示数据。我将不讨论使用datapicker和ajax更改数据,因为您首先需要获得正确的显示。请看一下这段代码,了解它的作用,以及它为什么能解决没有日期数据的问题。您似乎没有在表中添加文件柜名称,因此我将其作为id保留。您可以在查询中进行简单的联接以获取文件柜名称

PHP代码

<?php

//Using the object orientated style from this page
//http://php.net/manual/en/mysqli-stmt.bind-param.php

//Connect to database
$link = new mysqli("localhost", "root", "", "scheduler");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//Use todays date unless specified
$date = date("Y-m-d");
if (isset($_GET['date'])) {
    $date = $_GET['date'];
}

$cabinet_activity = array();

//First do a query to get all distinct cabinets, whether they have activity for the date or not
if ($cabinets = $link->prepare("SELECT DISTINCT cabinet FROM schedule")) {
    $cabinets->execute();
    $result = $cabinets->get_result();
    while ($this_row = $result->fetch_assoc()) {
        $cabinet_activity[$this_row['cabinet']] = array();  //Initialise the busy array for the next query
    }
}

if ($stmt = $link->prepare("SELECT id, HOUR(time) as hour, cabinet FROM schedule WHERE date = ?")) {

    $stmt->bind_param('s', $date);

    $stmt->execute();

    $result = $stmt->get_result();

    while ($this_row = $result->fetch_assoc()) {
        $id = $this_row['id'];
        $hour = $this_row['hour'];
        $cabinet_id = $this_row['cabinet'];

        //Add to cabinet to cabinet activity list (should not be necessary now)
        /*
        if (!array_key_exists($cabinet_id, $cabinet_activity)) {
            $cabinet_activity[$cabinet_id] = array();
        }
        */

        //Add the activity time to the busy hours
        if (!in_array($hour, $cabinet_activity[$cabinet_id])) {
            $cabinet_activity[$cabinet_id][] = $hour;
        }

    }
}

$min_hours = 8;
$max_hours = 16;

//Display information:
?>
<table border="1">
    <thead>
        <tr>
            <td>Cabinet</td>
            <?php
            for($hour = $min_hours; $hour <= $max_hours; $hour++) {
                ?><td><?php print $hour; ?></td><?php
            }
            ?>
        </tr>
    </thead>
    <tbody>
    <?php
    //For each cabinet
    foreach($cabinet_activity as $cabinet_id => $busy_hours) {
        ?><tr>
            <td><?php print $cabinet_id; ?></td>
            <?php
            for($hour = $min_hours; $hour <= $max_hours; $hour++) {
                if (in_array($hour, $busy_hours)) {
                    ?><td>+</td><?php
                } else {
                    ?><td>-</td><?php
                }
            }
            ?>
        </tr><?php
    } ?>
    </tbody>
</table>

内阁
+-

请。它们不再得到维护,而是在使用。学习替代,并考虑使用PDO,您正在使用PHP。您应该在那里处理所有数据显示问题(如果您愿意,还可以使用javascript)。因此,此查询不必要地复杂。谢谢,但是可以使用函数吗?我不想改变一切.我们在讨论什么,50行代码?改变一切。从你的描述我甚至不知道你的问题是什么。
<?php $query2 = mysql_query("select cabinetNum from cabinets") ?>
<table class="table table-hover">
    <tr class=".info"><td>Cab</td><td >8:00</td><td >9:00</td><td >10:00</td><td >11:00</td><td >12:00</td><td >13:00</td><td >14:00</td><td >15:00</td></tr>
    <!--  -->
    <?php while($data2=mysql_fetch_array($query2)): ?>
    <?php $data=mysql_fetch_array($query) ?>
        <tr>
            <?php  $cabinet = $data2['cabinetNum']; ?>
            <td><?=$cabinet ?></td>
            <?php  for($j=8; $j<=15; $j++)  : ?>
                <?php
                $busy = $data['h'.$j];
                ?>
                <?php if($busy>0  ): ?>
                    <td class="busy"></td>
                <?php else: ?>
                    <td class="free">
                        <form action="1.php" method="post">
                            <input type="hidden" name="time"  value="<?=$j?>" />
                            <input type="hidden" name="cabinet" value="<?=$cabinet?>" />
                            <input type="hidden" name="date"  value="<?=$q?>" />
                            <input type="submit" class="free" value="" name="sub"/>
                        </form>
                    </td>
                <?php endif?>
            <?php  endfor ?>
        </tr>
    <?php endwhile ?>
</table>
<?php

//Using the object orientated style from this page
//http://php.net/manual/en/mysqli-stmt.bind-param.php

//Connect to database
$link = new mysqli("localhost", "root", "", "scheduler");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//Use todays date unless specified
$date = date("Y-m-d");
if (isset($_GET['date'])) {
    $date = $_GET['date'];
}

$cabinet_activity = array();

//First do a query to get all distinct cabinets, whether they have activity for the date or not
if ($cabinets = $link->prepare("SELECT DISTINCT cabinet FROM schedule")) {
    $cabinets->execute();
    $result = $cabinets->get_result();
    while ($this_row = $result->fetch_assoc()) {
        $cabinet_activity[$this_row['cabinet']] = array();  //Initialise the busy array for the next query
    }
}

if ($stmt = $link->prepare("SELECT id, HOUR(time) as hour, cabinet FROM schedule WHERE date = ?")) {

    $stmt->bind_param('s', $date);

    $stmt->execute();

    $result = $stmt->get_result();

    while ($this_row = $result->fetch_assoc()) {
        $id = $this_row['id'];
        $hour = $this_row['hour'];
        $cabinet_id = $this_row['cabinet'];

        //Add to cabinet to cabinet activity list (should not be necessary now)
        /*
        if (!array_key_exists($cabinet_id, $cabinet_activity)) {
            $cabinet_activity[$cabinet_id] = array();
        }
        */

        //Add the activity time to the busy hours
        if (!in_array($hour, $cabinet_activity[$cabinet_id])) {
            $cabinet_activity[$cabinet_id][] = $hour;
        }

    }
}

$min_hours = 8;
$max_hours = 16;

//Display information:
?>
<table border="1">
    <thead>
        <tr>
            <td>Cabinet</td>
            <?php
            for($hour = $min_hours; $hour <= $max_hours; $hour++) {
                ?><td><?php print $hour; ?></td><?php
            }
            ?>
        </tr>
    </thead>
    <tbody>
    <?php
    //For each cabinet
    foreach($cabinet_activity as $cabinet_id => $busy_hours) {
        ?><tr>
            <td><?php print $cabinet_id; ?></td>
            <?php
            for($hour = $min_hours; $hour <= $max_hours; $hour++) {
                if (in_array($hour, $busy_hours)) {
                    ?><td>+</td><?php
                } else {
                    ?><td>-</td><?php
                }
            }
            ?>
        </tr><?php
    } ?>
    </tbody>
</table>