views:

45

answers:

1

Hey everyone. Here is my code.

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

What I want to do is a have a table header (just a row above all the other rows) that shows what time each column is but only if there is a value in the column.

I thought I code to something like:

<tr><?php if(isset($schedule['00:00'])) { ?><td style="width:32px">00:00</td><?php } if(isset($schedule['01:00'])) { ?><td style="width:32px">01:00</td><?php } ?> etc.

However because it is in a while loop, it will happen before every actual row, which isn't what I want to happen, I just want it to happen once on the top.

How can I solve this problem? Is there something I could append to that to make it only loop once?

Thanks!

+1  A: 

Then don't output your rows within the while loop. Save all data into a var. And then if the var contains content, put out the headrow and after that the var containing all content-rows.

EDIT

Okay that's the most ugly code I've ever written, you could probably use arrays and stuff to make the code much smaller. But this is the code-snippet to get an idea... Capture all rows. While doing that check each field if it is filled in. If so, than you will need a column for that in the headrow. We do that by setting data0 for example to true. After processing the whole data we check out each var and look if it is true. If so, than you print the column in the headrow. So in the end you see, that you ouput the table-tag, then the headrow-var, then the string with all formatted data and then the closing table-tag. So you get the basic idea. Store headrow and the other rows in seperate vars and put them out in the right order...

// 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>';
faileN
Could you please rephrase this? It's not very clear.
Sam
I edited my post. This is more than ugly, but I hope you get the basic idea...
faileN
I understand now, thankyou :).
Sam