PHP宾果脚本 - 回显某些数字

时间:2015-05-19 15:14:47

标签: php arrays echo

我想要回显6行和6列数字。第一行必须是10-20,下一行是20-30等。我想同时在一行中回显6个数字,所以代码保持有点干净和高效。有人能引导我朝着正确的方向前进吗?

这是我到目前为止的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"     "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Indzendopdracht 051R3</title>
</head>

<body>
<?PHP

function printBingocard(){
$bingoNumbers = array(
    "numbers" => array(10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69)
);

$rand_keys = array_rand($bingoNumbers, 1);  


    foreach($bingoNumbers as $bingoNumbers){
    echo "<table border='1'>";
    echo "<tr>";


    if($bingoNumbers > 10 and < 20){
    echo "<td>" . $bingoNumbers[array_rand($bingoNumbers)] . "<br />" . "</td>";
    }
    else{
        echo "";
    }


    echo "</tr>";
    echo "</table>";
    }
}

printBingocard();
?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您在数组中声明了数字,该数组名为&#34;数字&#34;在$ bingoNumbers数组里面。

所以当你循环它时,你必须使用#34;数字&#34;阵列.. 像这样......

,"00:00:00:00",""
,"00:01:06:15",""
,"00:07:19:12",""
,"00:09:52:03",""

此外,您使用的是AND,而不是&amp;&amp;在你的比较中,并没有在第二次比较中包含变量。

 foreach($bingoNumbers["numbers"] as $bn){
   if($bn > 10 && $bn < 20){
    echo "<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] . "<br />" . "</td>";
   }
 }

现在,实际上可以根据需要回显表格。您需要将每一行放入另一个数组中,以便稍后以正确的顺序将它们回显到表中。由于键的值是随机的。

像这样...

  $bungoNumbers > 10 and < 20        should be        $bn>10 && $bn<20

然后你需要循环遍历那些行数组,并遍历其内容以回显行。

像这样...

$rows=array();

foreach($bingoNumbers["numbers"] as $bn){
  if($bn > 10 && $bn <= 20){
   $rows["row1"][]="<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] ."</td>";
  }
  if($bn > 20 && $bn <= 30){
   $rows["row2"][]= "<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] ."</td>";
  }
  if($bn > 30 && $bn <= 40){
   $rows["row3"][]= "<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] ."</td>";
  }
  if($bn > 40 && $bn <= 50){
   $rows["row4"][]= "<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] ."</td>";
  } 
  if($bn > 50 && $bn <= 60){
   $rows["row5"][]= "<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] ."</td>";
  } 
  if($bn > 60 && $bn <= 70){
   $rows["row6"][]= "<td>" . $bingoNumbers["numbers"][array_rand($bingoNumbers["numbers"])] ."</td>";
  }   
}

希望有帮助:) 这是您已完成代码的PHP SANDBOX