在php

时间:2018-03-16 19:38:20

标签: php html-table

如何调整以下代码?

我需要以下列方式创建一个表(6x6):

  • 第一行包含6个随机数,从10到19
  • 第二行将包含6个从20到29
  • 的随机数
  • 第三行将包含从30到39
  • 的6个随机数
  • 第四行将包含从40到49
  • 的6个随机数
  • 第五行将包含6个从50到59
  • 的随机数
  • 第六行将包含6个随机数,从60到69

代码的制作方式是,每行从10个随机数中选择6个数字,这6个数字放在表格中。

但我刚从老师那里听说需要首先创建一个表(6x6),然后必须逐个单元地生成一个数字,如果该数字已经存在则必须被忽略(不能有)重复的数字)。

如何调整以下代码(我只是不知道了):

<!DOCTYPE html>
<html>

<head>
<title>Bingo</title>
</head>

<body>

<?php

$omvang = 5; // grootte v/d bingo kaart (2..9)
$trekkingen = 30; // aantal getrokken getallen

// initialiseer kaart
$kaart = array();
for ($i = 1; $i <= $omvang; $i++) {
    // definieer range v/d regel
    $regel = range($i * 10, $i * 10 + 9);
    // random volgorde
    shuffle($regel);
    // voeg de eerste {$omvang} cijfers toe aan de kaart
    $kaart[$i - 1] = array_slice($regel, 0, $omvang);
}

// bepaal trekking reeks
$trekking = range(10, $omvang * 10 + 9);
// randomize volgorde
shuffle($trekking);
// pak de eerste {$trekkingen}
$trekking = array_slice($trekking, 0, $trekkingen - 1);

// teken kaart en bewaar score
$score = array();
echo "<table style='border:1px solid blue;'>";
for ($y = 0; $y < $omvang; $y++) {
    echo "<tr>";
    for ($x = 0; $x < $omvang; $x++) {
        $score[$y][$x] = 0;
        echo "<td>";
        echo $kaart[$y][$x] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

?>

</body>

</html>

这第一段代码是Bingo游戏的一大段代码的一部分;这是我遇到的第一个问题。

2.0:

<!DOCTYPE html>
<html>

<head>
<title>Bingo</title>
</head>

<body>

<?php

$rijen = array();
for($rij = 1; $rij <= 6; $rij++) {
    $rijen[$rij] = array();
    for($kolommen = 0; $kolommen < 6; $kolommen++) {
        do {
            $tabel = rand($rij * 10, ($rij * 10) + 9);
        } while(in_array($tabel, $rijen[$rij]));
        $rijen[$rij][$kolommen] = $tabel;
    }
}

$trekking = array();
for($trek = 11; $rij <= 70; $rij++) {
    $trekking[$trek] = array();
    for($kolom = 0; $kolom < 6; $kolom++) {
        do {
            $trektabel = rand($rij * 10, ($rij * 10) + 9);
        } while(in_array($tabel, $trekking[$trek]));
        $trekking[$trek][$kolommen] = $trektabel;
    }
}

$score = array ();
foreach($rijen as $r){
    echo '<tr>';
    foreach($r as $cel) {
        if (in_array($rijen[$rij][$kolommen], $trekking[$trek][$kolommen])) {
            // zit in de trekking:
            $score[$rij][$kolommen]=1;
            echo "<td style='background:blue;color:white;border:1px solid black;'>";
        else {
            $score[$rij][$kolommen]=0;
            echo "<td style='border:1px solid blue;'>" . $cel . '</td>';
        }
    }
    echo '</tr>';
}
echo '</table>';

</body>

</html>

3.0

<!DOCTYPE html>
<html>
<head>
<title>Bingo</title>
</head>
<body>
<h2>Bingokaart waarop BINGO is gevallen</h2>
<?php
function ishetBingo($tabelletje, $nummers,&$Rrij,&$Rkol) {
    $Rrij=
    $Rkol=
    $tabelRijen = array(0,0,0,0,0,0);
    $tabelKolommen = array(0,0,0,0,0,0);
    $voltooid = false;
    foreach($tabelletje as $rij => $kolommen) {
        foreach($kolommen as $kol => $nummer) {
            if(in_array($nummer, $nummers)) {
                $tabelRijen[$rij]++;
                $tabelKolommen[$kol]++;
                if($tabelRijen[$rij] == 6 ){
                  $voltooid = true;
                  $Rrij = $rij;  
                }
                if($tabelKolommen[$kol] == 6){
                  $voltooid = true;
                  $Rkol = $kol;   
                } 
            }
        }
    }
    return $voltooid;
}
// initialiseer kaart
$kaart = array();
for ($rijen = 0; $rijen < 6; $rijen++) {
    $kaart[$rijen] = array();
    for($k = 0; $k < 6; $k++) {
        do {
            $regel = rand(($rijen + 1)*10, (($rijen + 1)*10)+9);
        } while(in_array($regel, $kaart[$rijen]));
        $kaart[$rijen][$k] = $regel;
}
}
$trekking = array();
$DeControlerij = false;
$DeControlecol = false;
do {
    do {
        $rijen = rand(10, 69);
    } while(in_array($rijen, $trekking));
    $trekking[] = $rijen;
} while(!ishetBingo($kaart, $trekking, $DeControlerij, $DeControlecol));
// teken kaart en bewaar score
$columns=[];
echo "<table style='border:1px solid blue;'>";
foreach ($kaart as $rijkey => $tabelrij) {
    echo "<tr>";
    $iter = 0;
    $weggestreept = 0;
    foreach ($tabelrij as $colkey => $cel) {
        // bewaar nummer v/d cell
        if(!key_exists($iter,$columns)){
            $columns[$iter]=0;
        }
        if (in_array($cel, $trekking)) {
            if($DeControlerij!==false && $DeControlerij ==$rijkey){
                echo "<td style='background:green;color:white;border:1px solid black;'>" . $cel . "</td>";
            }
            elseif($DeControlecol!==false && $DeControlecol==$colkey){
                echo "<td style='background:green;color:white;border:1px solid black;'>" . $cel . "</td>";
            }
            else{
                echo "<td style='background:blue;color:white;border:1px solid black;'>" . $cel . "</td>";
            }
            $weggestreept++;
            $columns[$iter]++;
        } 
        else {
            //$columns[$iter] += 0;
            echo "<td>" . $cel . "</td>";
        }
        $iter++;

    }
    echo "<td>" . $weggestreept . "</td>";
    echo "</tr>";
}
echo "<tr>";
foreach($columns as $field){
    echo '<td>'.$field.'</td>';
}
echo "</tr>";
echo "</table>";
// Bingo melding
// if (ishetBingo($kaart, $trekking)) {
//     echo '<h2>BINGO!!!</h2>';
// };
// print trekking
echo "<br />Getrokken getallen:<table><tr>";
foreach ($trekking as $nummer) {
    if (in_array($nummer, $trekking)) {
    echo "<td>" . $nummer . "</td>";
    }
}
echo "</tr></table>";
echo "<br/>Aantal getallen dat getrokken is: " . count($trekking);
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您应首先初始化数组,然后以html格式打印。 类似的东西:

$rows = array();
for($r = 1; $r <= 6; $r++) {
    $rows[$r] = array();
    for($c = 0; $c < 6; $c++) {
        do {
            $t = rand($r * 10, ($r * 10) + 9);
        } while(in_array($t, $rows[$r]));
        $rows[$r][$c] = $t;
    }
}

技巧部分是&#34; in_array&#34;部分:)

然后你就打印出来了:

echo '<table>';
foreach($rows as $row){
    echo '<tr>';
    foreach($row as $cell) {
        echo '<td>' . $cell . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

一个完整的例子,带有绘图数组:

<!DOCTYPE html>
<html>

<head>
<title>Bingo</title>
</head>

<body>

<?php


$rows = array();
for($r = 1; $r <= 6; $r++) {
    $rows[$r] = array();
    for($c = 0; $c < 6; $c++) {
        do {
            $t = rand($r * 10, ($r * 10) + 9);
        } while(in_array($t, $rows[$r]));
        $rows[$r][$c] = $t;
    }
}

$draw = array();

$full = false;
do {
    do {
        $r = rand(1, 70);
    } while(in_array($r, $draw));
    $draw[] = $r;

    $cRows = array(0,0,0,0,0,0);
    $cCols = array(0,0,0,0,0,0);

        foreach($rows as $row => $cols) {
        foreach($cols as $col => $number) {
            if(in_array($number, $draw)) {
                $cRows[$row]++;
                $cCols[$col]++;

                if($cRows[$row] == 6 || $cCols[$col] == 6) $full = true;
            }
        }
    }

} while(!$full);

echo 'Numbers are: ' . implode(', ', $draw);

echo '<br /><br /><table>';
foreach($rows as $row){
    echo '<tr>';
    foreach($row as $cell) {
        echo '<td';
        if(in_array($cell, $draw)) echo ' style="color: white; background-color: blue;"';
        echo '>' . $cell . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

?>

</body>

</html>