php - 使用 PHP/MySQL 的队列表

标签 php mysql

我有包含 iddatetimecabinet 列的数据库表。

我应该用数据库中的值生成表,但它应该看起来像队列表。

   time 8,9,10,11,12,13,14,15,16
     102 -- -  +  -   -  +  - - 
     103 -+ -  -  -   -  +  - - 

如果时间很忙,它将是+,如果有空则为-。 所以我尝试使用这段代码来完成。

我应该改变循环..一些想法?谢谢!

我还有带有 idcabinetNumbertitle 列的 table 柜。 我需要修改我的 mysql 查询。应该可以更改日期(使用日期选择器和 Ajax)并更改表行值。

  $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"><?=$busy?></td>
                <?php else: ?>
                    <td class="free"><?=$cabinet; ?>
                        <form action="1.php" method="post">
                            <input type="hidden" name="time"  value="<?= $j;?>" /><?=$data['date']?>
                            <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>

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

最佳答案

我猜你不需要按 $i 循环

<table class="table table-hover">
    <tr class=".info"><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 for($j=8; $j<=15; $j++)  : ?>
           <?php $time = $data['time'];
                 $time = round($time);
                 $cabinet = $data['cabinet'];
           ?>
           <?php if($j == $time): ?>
               <td class="busy"></td>
           <?php else: ?>
           <td class="free">
               <form action="1.php" method="post"><?=$cabinet; ?>
                   <input type="hidden" name="time"  value="<?= $j; ?>" />
                   <input type="submit" style="free" value="" name="sub"/>
               </form>
           </td>
           <?php endif?>
        <?php  endfor ?>
    </tr>
<?php endwhile ?>

</table>

更新将您的查询更改为此:

http://sqlfiddle.com/#!9/84bae/3

SELECT cab,
   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 `dayshedule`
GROUP BY cab

然后将您的 php 代码更改为:

<?php while($data=mysql_fetch_array($query)) :?>
    <tr>
       <?php for($j=8; $j<=15; $j++)  : ?>
           <?php 
                 $busy = $data['h'.$j];
                 $cabinet = $data['cab'];
           ?>
           <?php if($busy>0): ?>
               <td class="busy"></td>
           <?php else: ?>
           <td class="free">
               <form action="1.php" method="post"><?=$cabinet; ?>
                   <input type="hidden" name="time"  value="<?= $j; ?>" />
                   <input type="submit" style="free" value="" name="sub"/>
               </form>
           </td>
           <?php endif?>
        <?php  endfor ?>
    </tr>
<?php endwhile ?>

更新 在此处更新您的代码:

<tr class=".info"><td>Cab #</td><td >8:00</td>...

这里:

<tr>
   <?php  $cabinet = $data['cab']; ?>
   <td><?=$cabinet ?></td>
   <?php  for($j=8; $j<=15; $j++)  : ?>
       <?php 
             $busy = $data['h'.$j];

       ?>

关于php - 使用 PHP/MySQL 的队列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080597/

相关文章:

php - 使用 Apple 登录 = invalid_client

php - 如何修复内存耗尽的postgresql

java - JBoss6 JPA : Entity with @Lob results in GenericJDBCException

mysql - 如果 ID 不在数组/逗号分隔列表中,则获取行

MySQL奇怪的插入错误

php - 是否可以使用 cURL 通过 SSH 抓取文件?

php - 在 PHP 中使用 LDAP 函数获取 Active Directory tokenGroups 属性

php - 如何获取实体关联的旧值?

MySQL - View 或外键的适当应用

mysql - 两个表之间的异常报告