2016-05-25 10 views
0

私はいくつかの問題を抱えています。配列からランダムな値でテーブルを埋めることができません。助けてもらえますか?配列でランダムにPHPテーブルを塗りつぶします。

<?php 
 
header('Content-Type: text/html; charset=utf-8'); 
 
?> 
 

 
<form action="table2.php" method="POST"> 
 
<p>N: <input name = "row"; maxlength="2" size="1" value="5"></p> 
 
<p>M: <input name = "col"; maxlength="2" size="1" value="7"></p> 
 
    <input type="submit" name="zero" value="go"/> 
 
</form> 
 
<?php 
 

 
$array1 = array("1", "2", "3", "4", "5", "6", "7", "8"); 
 
$rand_keys = array_rand($array1, 2); 
 
echo $array1[$rand_keys[0]] . "\n"; 
 
function draw_table($row,$col) { 
 
    $table = "<table>"; 
 
    $i = 1; 
 
    do { 
 
     $table .= "<tr>"; 
 
     $j = 1; 
 
     do { 
 
      $table .= "<td>X $i Y $j"; 
 
      $table .= "</td>"; 
 
      $j++; 
 
     } 
 
     while($j <= $col); 
 
     $table .= "</tr>"; 
 
     $i++; 
 
    } 
 
    while($i <= $row); 
 
    $table .= "</table>"; 
 
    return $table; 
 
} 
 

 
if(isset($_POST['zero'])) { 
 
    $rows = $_POST['row']; 
 
    $cols = $_POST['col']; 
 
    
 
    echo(draw_table($rows, $cols)); 
 
} 
 
?>

私はarray_rand使用しようとしましたが、私はするつもりですので、私はdo while()ループを使用したことがない、どのように機能するために、このコードを変更

$table .= "<td>X $i Y $j"; 
$table .= "</td>"; 
$j++; 
+0

あなたは何を達成したいですか?配列の値でテーブルを埋めたいですか? –

+0

はい。私は配列を持っている、私はそれからランダムな値で私のテーブルを埋めるしたい。 – TwinSpike

答えて

0

理解することはできませんうまくいけばここであなたの人生を簡素化する:

$words = array("1", "2", "3", "4", "5", "6", "7", "8"); 

$rows = rand(1, 20); 
$cols = rand(1, 20); 

echo '<table>'."\n"; 
for($r = 1; $r <= $rows; ++$r) 
{ 
    echo '<tr>'."\n"; 
    for($c = 1; $c <= $cols; ++$c) 
    { 
     // One word 
     $td_text = $words[rand(0,(count($words)-1))]; 

     // One to five words separated by a space 
     // $td_text = substr(str_repeat($words[rand(0,(count($words)-1))].' ', rand(1, 5)), 0, -1); 

     echo '<td>'.$td_text.'</td>'; 
    } 
    echo '</tr>'."\n"; 
} 
echo '</table>'; 
+0

それはうまく動作しますが、私は配列の数値を単語に置き換えたいと思います。この例をありがとう。 シンプリフィケーションのために配列に数値を入力します。 – TwinSpike

+0

@TwinSpikeあなたは本当の言葉か無作為な手紙でそれを記入したいですか?そこからあなたはその言葉を手に入れますか? – MonkeyZeus

+0

私の配列から。開始のために。 – TwinSpike

関連する問題