Jump to content

filling in the rest of a table?


ferrit91

Recommended Posts

sorry if title doesnt make sence, but am trying to set something up so that if there is nothing left to insert it insert blank cells. example

 

td shows for every cell read from the database and say there are 7 in the database

 

tr shows after every 5 using php meaning there is a row 3 missing td

 

Picture may help a little

n3b5mc.gif

 

 

i need php to count how many are left to fill until the next 5.

Link to comment
https://forums.phpfreaks.com/topic/205782-filling-in-the-rest-of-a-table/
Share on other sites

It would have been helpful if you had provided your current code. But, here is one sample solution

<?php
//Config var to determine # of columns
$max_columns = 5;
    
//Function to create a table row
function createRow($rowRecords, $columns)
{
    $rowHTML = '';
    if(count($rowRecords)<1) { return $rowHTML; }
    $rowHTML .= "<tr>\n";
    for($i=0; $i<$columns; $i++)
    {
        //If less than 5 records, a space is used for the remaining cells
        $data = (isset($rowRecords[$i])) ? $rowRecords[$i] : ' ';
        $rowHTML .= "<td>{$data}</td>\n";
    }
    $rowHTML .= "</tr>\n";
    return $rowHTML;
}
    
//Run the query to get the data
$query = "SELECT something FROM table";
$results = mysql_query($query);
    
//Create the output
$tableHTML = ''; //Var to hold the output
$records = array(); //Temp array for the records
while ($records[] = mysql_fetch_assoc($result))
{
    if(count($records) == $max_columns)
    {
        //Create output for a complete row
        $tableHTML .= createRow($rowRecords, $max_columns);
        $records = array(); //Reset the array
    }
}
    
//Complete the final row
$tableHTML .= createRow($rowRecords, $max_columns);
    
?>
<html>
<body>
<table>
<?php echo $tableHTML; ?>
</table>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.