使用PHP / MySQL / Ajax的队列表

时间:2015-05-07 15:41:12

标签: php jquery mysql ajax

我的数据库表id包含datetimecabinetcabinets列。 此外,我还有idcabinetNumbertitle列的表cabinet 8,9,10,11,12,13,14,15,16 102 - - - + - - + - - 103 - + - - - - + - - 。 我应该生成表,但它应该看起来像队列表。

+

如果时间很忙,它将为-,如果空闲$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()); ?>

我需要修改我的mysql查询。并且应该可以更改日期(usege datepicker和Ajax)并更改表行的值取决于日期。 但是如果date不存在,则查询将为空,表将没有行。我该如何解决它?

MySQL查询:

<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>

表:

<script>
    $(function() {
        $( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
    });
    $( ".date" ).on('change', function(){
        var date = $('#datepicker').val();
        $.ajax({
            type:'get',
            url:'table.php',
            data : {
                'date' : date
            },
            success: function(data) {
                $('#result').html(data);
            }
        });
    });
</script>

和jQuery / Ajax:

query2

我解决了我的问题。我从cabinet表中添加了新查询<?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>

<a target='_blank' href='https://twitter.com/intent/retweet?tweet_id={$tweet->id_str}'><i class='fa fa-retweet'></i> {$tweet->retweet_count}</a

1 个答案:

答案 0 :(得分:0)

关键是保持查询简单,使用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>