Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.