Jump to content
newbieprogrammer

Constraining the # of columns of an existing table

Recommended Posts

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>

Share this post


Link to post
Share on other sites
Posted (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> 

image.png.34052060b7909bb271670fd5feda892b.png

Edited by Barand
  • Like 1
  • Great Answer 1

Share this post


Link to post
Share on other sites
Posted (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 by newbieprogrammer

Share this post


Link to post
Share on other sites

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.