sigmahokies Posted May 28, 2015 Share Posted May 28, 2015 Hi everyone, I am still learning PHP, I am getting better a little, but i am still struggle with while and foreach loop to create few columns. I create one column with while or foreach, but I couldn't figure to create few columns intsead of one long column. Can you help me or make any suggest? Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/ Share on other sites More sharing options...
Barand Posted May 28, 2015 Share Posted May 28, 2015 If you post your code we can see what you are doing and where you are going wrong Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1512743 Share on other sites More sharing options...
ginerjm Posted May 28, 2015 Share Posted May 28, 2015 Didn't you post this same question last month? At least post some code for us to work with. Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1512744 Share on other sites More sharing options...
Barand Posted May 28, 2015 Share Posted May 28, 2015 Yes, he did. http://forums.phpfreaks.com/topic/296326-foreach-or-while-table-columns-and-rows/ Going around in circles with this one. Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1512746 Share on other sites More sharing options...
sigmahokies Posted May 28, 2015 Author Share Posted May 28, 2015 Oh sorry, I forget about my first thread about while and foreach loop...I asked administrator to remove this thread, Thank you for remind me... Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1512757 Share on other sites More sharing options...
QuickOldCar Posted May 28, 2015 Share Posted May 28, 2015 Since we are in the php forum could use array_chunk() to split the array of results into chunks. You can also do this with html or css. Table,style,css...get creative. As simple as can explain it without getting crazy styling it. <?php $html = array(); $mysqli = new mysqli("localhost", "user", "password", "database"); $sql = "SELECT column_name FROM table_name"; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { $html[] = "<td>" . $row['column_name'] . "</td>"; } $html = array_chunk($html, 3); // 3 chunks ?> <!DOCTYPE html> <html> <head> </head> <table> <?php foreach ($html as $chunk) { echo "<tr>" . implode('', $chunk) . "</tr>"; } ?> </table> </html> an array using array_chunk and css <?php //how many display columns $number_columns = 8; //dummy array data $array = range(1, 100); //percentage for css division of page width $percentage = floor(100 / $number_columns); //create chunks from the array calculating amount in array and number of columns $columns = array_chunk($array, ceil(count($array) / $number_columns)); //loop the chunked columns foreach ($columns as $chunk) { //opted to make a divider and style echo "<div style='padding:10px;'>"; //loop each array value within the chunk foreach ($chunk as $values) { //opted to make a paragraph and style echo "<p style='display:block;float:left;width: " . $percentage . "%;'>" . $values . "</p>"; } echo "</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1512758 Share on other sites More sharing options...
sigmahokies Posted June 2, 2015 Author Share Posted June 2, 2015 Hi Guru, your code is working, but it is horizon data. I can take horizon data, but I prefer vertical data. If there is possible, Let me know. Thanks, Gary Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1513046 Share on other sites More sharing options...
Barand Posted June 2, 2015 Share Posted June 2, 2015 Put the left float on the <div>s and not the <p> tags. Also removed the padding. <?php //how many display columns $number_columns = 8; //dummy array data $array = range(1, 100); //percentage for css division of page width $percentage = floor(100 / $number_columns); //create chunks from the array calculating amount in array and number of columns $columns = array_chunk($array, ceil(count($array) / $number_columns)); //loop the chunked columns foreach ($columns as $chunk) { //opted to make a divider and style echo "<div style='float:left;width: " . $percentage . "%;'>"; //loop each array value within the chunk foreach ($chunk as $values) { //opted to make a paragraph and style echo "<p>" . $values . "</p>"; } echo "</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/296520-multiple-columns-in-while-or-foreach-loop/#findComment-1513049 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.