saynotojava Posted October 9, 2013 Share Posted October 9, 2013 (edited) I have currently this code: echo'<table border="1">'; while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo '<tr><td>' .$row['Domain'].'</td><td><input type="checkbox" name="a[]" value="n"></td></tr>';}echo'</table>'; Which will output query from database and it will show output in that table format.But the problem is, when there is a lot of info pulled of,it look bad as a lot of space in unused then,so i want to make it every fifth output to show in new line(compared to current where every output goes to new line).Also,output does'nt need to be with tables,i just want to make it look normaly aligned. Edited October 9, 2013 by saynotojava Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/ Share on other sites More sharing options...
Solution PravinS Posted October 9, 2013 Solution Share Posted October 9, 2013 Try replacing while loop using below code $i = 1; echo '<tr>'; while($row = $ret->fetchArray(SQLITE3_ASSOC) ) { echo '<td>' .$row['Domain'].'</td><td><input type="checkbox" name="a[]" value="n"></td>'; if ($i % 5 == 0) echo '</tr><tr>'; $i++; } echo '</tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453225 Share on other sites More sharing options...
Ch0cu3r Posted October 9, 2013 Share Posted October 9, 2013 (edited) echo'<table border="1">'; $col = 0; // column counter $max_cols = 5; // how many columns per row $tresults = 0; // results counter $max_results = $ret->numRows(); // total number of results while($row = $ret->fetchArray(SQLITE3_ASSOC)) { if($col == 0) // if col is 0, start new table row echo '<tr>'; echo '<td>' .$row['Domain'].'</td><td><input type="checkbox" name="a[]" value="n"></td>'; // echo a column $col++; // increment column counter $tresults++; // increment results counter if($col == $max_cols) { // if col equals to max columns echo '</tr>'; // end table row $col = 0; // reset column counter } elseif($tresults == $max_results && $col < $max_cols) // if we have processed all results, but $col is still less than max number of columns echo str_repeat('<td></td>', $max_cols - $col) . '</tr>'; // then clean up table, output empty columns and end table row } echo'</table>'; Edited October 9, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453230 Share on other sites More sharing options...
Barand Posted October 9, 2013 Share Posted October 9, 2013 Alternative method using divs instead of table http://forums.phpfreaks.com/topic/282474-i-want-to-create-thumbnail-but-facing-problems/?do=findComment&comment=1451471 Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453243 Share on other sites More sharing options...
saynotojava Posted October 9, 2013 Author Share Posted October 9, 2013 Try replacing while loop using below code $i = 1; echo '<tr>'; while($row = $ret->fetchArray(SQLITE3_ASSOC) ) { echo '<td>' .$row['Domain'].'</td><td><input type="checkbox" name="a[]" value="n"></td>'; if ($i % 5 == 0) echo '</tr><tr>'; $i++; } echo '</tr>'; Works as charm,thanks.I know it trick is in increment and if but didn't know how exactly to put it. Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453250 Share on other sites More sharing options...
cyberRobot Posted October 9, 2013 Share Posted October 9, 2013 In case you're interested, array_chuck() could be used to break the column groups. This doesn't require a counter. For an example which closely relates to your question, check out http://www.cyberscorpion.com/2013-08/build-html-tables-dynamically-with-php-part-2-simplify-with-array_chunk/ Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453255 Share on other sites More sharing options...
Barand Posted October 9, 2013 Share Posted October 9, 2013 array_chunk() Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453257 Share on other sites More sharing options...
mac_gyver Posted October 9, 2013 Share Posted October 9, 2013 array_chunk() his function name was probably because Halloween is coming - http://horror-movies.wikia.com/wiki/Chucky Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453258 Share on other sites More sharing options...
cyberRobot Posted October 9, 2013 Share Posted October 9, 2013 his function name was probably because Halloween is coming - http://horror-movies.wikia.com/wiki/Chucky Hey, I didn't name the function. You can thank the developers of PHP. I am a Goonies fan, however, so "chunk" works for me. Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453260 Share on other sites More sharing options...
cyberRobot Posted October 9, 2013 Share Posted October 9, 2013 array_chunk() Ah, I finally got that...thanks for correcting the typo. Quote Link to comment https://forums.phpfreaks.com/topic/282828-problem-with-while-loop-and-html-output/#findComment-1453264 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.