Christian Tucker
Christian Tucker

Reputation: 627

Stumped with table formatting

If you remember me from asking the 20+ questions yesterday, I'm still awake and still at it. I'd like to inform anyone who cares I've made GREAT progress.

What I'm trying to figure out now is how to create a minimum about of rows for a table to have. The reason being is if the Table on my page doesn't have exactly 12 rows, it looks bad. The problem is there won't always be 12 rows worth of data in the SQL Database, so I need to find a way to make it so if there's not 12 results it fills in the table with a character of choice, probably "--"

Also, I'm trying to figure out how to create pages for the table. I haven't done any research on that one yet and it's not what this questions about, but if you know how and want to drop it here, that'd be cool.

Upvotes: 0

Views: 37

Answers (3)

Mark Miller
Mark Miller

Reputation: 7447

There are a few different ways you could go about this, but the basic idea is to know how many rows you printed, and if it's less than 12, print empty rows until you have the 12 you need. Here's one example:

Assuming you have your query results in an array called $data:

<table>
<?php
    foreach ($data as $row) {
        echo "<tr><td>{$row['col1']}</td><td>{$row['col2']}</td><td>{$row['col3']}</td></tr>";
    }
    if (count($data) < 12) {
        for ($i=0; $i < 12-count($data); $i++) {                        
            echo "<tr><td colspan='3'>EMPTY ROW</td></tr>";
        }
    }
?>
</table>

Upvotes: 1

serakfalcon
serakfalcon

Reputation: 3531

instead of the normal while($stmt->fetch()) use a for loop that iterates 12 times. something like:

$dofetch = true;
for ($i = 0;$i<12;$i++) {
    ?><tr><?php
    if ($dofetch && $stmt->fetch()) {
       echo $stuff;
    } else {
        $dofetch = false;
        echo '--';
    }
    ?></tr><?php

}

Upvotes: 0

Subha
Subha

Reputation: 1051

You can try using

table-layout: fixed;

Property in table style.

Upvotes: 1

Related Questions