PHP while循环和表头仅显示是否设置了db值

PHP while循环和表头仅显示是否设置了db值,php,mysql,while-loop,Php,Mysql,While Loop,这是我的密码 <table border="1" style="width:800px"> <?php $schedule_db = mysql_query("SELECT * FROM schedule WHERE '00:00' is not null and '01:00' is not null and '02:00' is not null and '03:00' is not null and '04:00' is not null

这是我的密码

<table border="1" style="width:800px">
                <?php $schedule_db = mysql_query("SELECT * FROM schedule WHERE '00:00' is not null and '01:00' is not null and '02:00' is not null and '03:00' is not null and '04:00' is not null and '05:00' is not null and '06:00' is not null and '07:00' is not null and '08:00' is not null and '09:00' is not null and '10:00' is not null and '11:00' is not null and '12:00' is not null and '13:00' is not null and '14:00' is not null and '15:00' is not null and '16:00' is not null and '17:00' is not null and '18:00' is not null and '19:00' is not null and '20:00' is not null and '21:00' is not null and '22:00' is not null and '23:00' is not null") 
                or die(mysql_error()); ?>
                <form method="post" action="config_action.php">
                <?php while($schedule = mysql_fetch_array( $schedule_db )) { ?>
                    <tr><?php if(isset($schedule['00:00'])) { ?><td style="width:32px"><?php echo $schedule['00:00'] ?></td><?php } if(isset($schedule['02:00'])) { ?><td style="width:32px"><?php echo $schedule['02:00'] ?></td><?php } if(isset($schedule['03:00'])) { ?><td style="width:32px"><?php echo $schedule['03:00'] ?></td><?php } if(isset($schedule['04:00'])) { ?><td style="width:32px"><?php echo $schedule['04:00'] ?></td><?php } if(isset($schedule['05:00'])) { ?><td style="width:32px"><?php echo $schedule['05:00'] ?></td><?php } if(isset($schedule['06:00'])) { ?><td style="width:32px"><?php echo $schedule['06:00'] ?></td><?php } if(isset($schedule['07:00'])) { ?><td style="width:32px"><?php echo $schedule['07:00'] ?></td><?php } if(isset($schedule['08:00'])) { ?><td style="width:32px"><?php echo $schedule['08:00'] ?></td><?php } if(isset($schedule['09:00'])) { ?><td style="width:32px"><?php echo $schedule['09:00'] ?></td><?php } if(isset($schedule['10:00'])) { ?><td style="width:32px"><?php echo $schedule['10:00'] ?></td><?php } if(isset($schedule['11:00'])) { ?><td style="width:32px"><?php echo $schedule['11:00'] ?></td><?php } if(isset($schedule['12:00'])) { ?><td style="width:32px"><?php echo $schedule['12:00'] ?></td><?php } if(isset($schedule['13:00'])) { ?><td style="width:32px"><?php echo $schedule['13:00'] ?></td><?php } if(isset($schedule['14:00'])) { ?><td style="width:32px"><?php echo $schedule['14:00'] ?></td><?php } if(isset($schedule['15:00'])) { ?><td style="width:32px"><?php echo $schedule['15:00'] ?></td><?php } if(isset($schedule['16:00'])) { ?><td style="width:32px"><?php echo $schedule['16:00'] ?></td><?php } if(isset($schedule['17:00'])) { ?><td style="width:32px"><?php echo $schedule['18:00'] ?></td><?php } if(isset($schedule['19:00'])) { ?><td style="width:32px"><?php echo $schedule['19:00'] ?></td><?php } if(isset($schedule['20:00'])) { ?><td style="width:32px"><?php echo $schedule['20:00'] ?></td><?php } if(isset($schedule['21:00'])) { ?><td style="width:32px"><?php echo $schedule['21:00'] ?></td><?php } if(isset($schedule['22:00'])) { ?><td style="width:32px"><?php echo $schedule['22:00'] ?></td><?php } if(isset($schedule['23:00'])) { ?><td style="width:32px"><?php echo $schedule['23:00'] ?></td><?php } ?></tr>
                <?php } ?>
                </form>
</table>


然后不要在while循环中输出行。将所有数据保存到一个变量中。如果该变量包含内容,则显示标题,然后显示包含所有内容行的变量

编辑

好吧,这是我写过的最难看的代码,你可以使用数组和其他东西来让代码变得更小。但这是一段代码片段来了解。。。捕获所有行。执行此操作时,请检查每个字段是否已填写。如果是这样,您将需要在头枕中为其设置一列。我们通过将
data0
设置为
true
来实现这一点。在处理完所有数据后,我们检查每个var,看看它是否为真。如果是,则在标题栏中打印该列。最后,您会看到,您输出了table标记,然后是headlow var,然后是包含所有格式化数据的字符串,然后是closing table标记。所以你得到了基本的想法。将头枕和其他行分别存放在不同的容器中,并按正确的顺序取出

// Get data from database-table...
$schedule_db = mysql_query('...your query...') or die(mysql_error());

$data0 = false;
$data1 = false;
$data2 = false;
// reapeat this for all hours till 23...

while($schedule = mysql_fetch_array($schedule_db))
{
    if(isset($schedule['00:00']))
    {
        $data0 = true;
        $row = '<td style="width: 32px;">' . $schedule['00:00'] . '</td>';
    }

    // Reapeat this for every hour you need...
    if(isset($schedule['01:00']))
    {
        $data1 = true;
        $row .= '<td style="width: 32px;">' . $schedule['01:00'] . '</td>';
    }

    //...

    $rows .= $row;
}

$headrow = '<tr>';
if($data0 === true)
    $headrow .= '<th>00:00</th>' 

if($data1 === true)
    $headrow .= '<th>01:00</th>';

// Repeat for all hours...


$headrow = '</tr>';

echo '<table>';
echo $headrow;
echo $rows;
echo '</table>';
//从数据库表中获取数据。。。
$schedule_db=mysql_query(“…您的查询…”)或die(mysql_error());
$data0=false;
$data1=假;
$data2=假;
//再重复几个小时直到23点。。。
而($schedule=mysql\u fetch\u数组($schedule\u db))
{
如果(isset($schedule['00:00']))
{
$data0=true;
$row='.$schedule['00:00'].';
}
//在你需要的每一个小时重复这个。。。
如果(isset($schedule['01:00']))
{
$data1=真;
$row.=''.$schedule['01:00'].';
}
//...
$rows.=$row;
}
$headlow='';
如果($data0==true)
$headlow.=“00:00”
如果($data1==true)
$headlow.='01:00';
//连续几个小时重复。。。
$headlow='';
回声';
回声$headlow;
echo$行;
回声';
// Get data from database-table...
$schedule_db = mysql_query('...your query...') or die(mysql_error());

$data0 = false;
$data1 = false;
$data2 = false;
// reapeat this for all hours till 23...

while($schedule = mysql_fetch_array($schedule_db))
{
    if(isset($schedule['00:00']))
    {
        $data0 = true;
        $row = '<td style="width: 32px;">' . $schedule['00:00'] . '</td>';
    }

    // Reapeat this for every hour you need...
    if(isset($schedule['01:00']))
    {
        $data1 = true;
        $row .= '<td style="width: 32px;">' . $schedule['01:00'] . '</td>';
    }

    //...

    $rows .= $row;
}

$headrow = '<tr>';
if($data0 === true)
    $headrow .= '<th>00:00</th>' 

if($data1 === true)
    $headrow .= '<th>01:00</th>';

// Repeat for all hours...


$headrow = '</tr>';

echo '<table>';
echo $headrow;
echo $rows;
echo '</table>';