newbieprogrammer Posted April 4, 2020 Share Posted April 4, 2020 Hello everybody, I have been racking my brain trying to decipher something that should be really simple for a seasoned programmer. The following code shows random numbers from 0 to 60. How can I get the code to display six columns as opposed to ten? Support is much appreciated! Newbieprogrammer <html> <head> <?php mt_srand((double)microtime()*1000000); function Vullen() { $randomNr = array(0,1,2,3,4,5,6,7,8,9); shuffle($randomNr); return $randomNr; } $randomNr = Vullen(); function display($randomNr) { $table = '<table border="1px" cellpadding="1px">'; $bingokaart = array(); for ($row = 1; $row < 7; ++$row) { $table .= '<tr>'; foreach ($randomNr as $number) { $table .= '<td>'.$row.$number.'</td>'; } $table .= '</tr>'; } $table .='</table>'; echo $table; } display($randomNr); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/310460-constraining-the-of-columns-of-an-existing-table/ Share on other sites More sharing options...
Barand Posted April 4, 2020 Share Posted April 4, 2020 (edited) Your randomNr array contains 10 elements so foreach($randomNr as $number) will give 10 columns. You need to pick a random 6 numbers out of the 10. Separate the php code from the html. Use CSS for styling the output. Example <?php $randomNr = range(0,9); $bingokaart = display($randomNr); function display ($arr) { $result = ""; for ($row = 1; $row < 7; ++$row) { $rand6 = array_rand($arr, 6); $result .= '<tr>'; foreach ($rand6 as $n) { $result .= "<td>$row$arr[$n]</td>"; } $result .= "</tr>\n"; } return $result; } ?> <!DOCTYPE html> <html> <head> <title>Sample</title> <style type="text/css"> table { border-collapse: collapse; } td { padding: 2px; } </style> </head> <body> <table border='1'> <?= $bingokaart ?> </table> </body> </html> Edited April 4, 2020 by Barand 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/310460-constraining-the-of-columns-of-an-existing-table/#findComment-1576271 Share on other sites More sharing options...
newbieprogrammer Posted April 4, 2020 Author Share Posted April 4, 2020 (edited) Hello Barand, Thank you very much for your time! During my brainstorming for possible solutions, I had entertained the thought of extracting 6 random numbers from the shuffled array. It is nice to see this is not a bad approach and that in your solution, this is brilliantly executed. Best wishes & stay safe, Ramon Edited April 4, 2020 by newbieprogrammer Quote Link to comment https://forums.phpfreaks.com/topic/310460-constraining-the-of-columns-of-an-existing-table/#findComment-1576278 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.