ferrit91 Posted June 24, 2010 Share Posted June 24, 2010 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 i need php to count how many are left to fill until the next 5. Quote Link to comment https://forums.phpfreaks.com/topic/205782-filling-in-the-rest-of-a-table/ Share on other sites More sharing options...
aeroswat Posted June 24, 2010 Share Posted June 24, 2010 for loop starting at 1 and going to 5 minus whatever the variable is that holds your count divided by 5 if you keep going or you reset it everytime u dont need to divide it Quote Link to comment https://forums.phpfreaks.com/topic/205782-filling-in-the-rest-of-a-table/#findComment-1076853 Share on other sites More sharing options...
Psycho Posted June 24, 2010 Share Posted June 24, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/205782-filling-in-the-rest-of-a-table/#findComment-1076875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.