2016-03-27 9 views
0

ユーザーが入力する行数/列数に基づいて文字または数字を使用して作成された正方形または長方形を.phpを使って生成する方法を教えてください。ここに私が探しているもののサンプルがあります。私はjavascriptでこれをやったが、いくつかのサーバーサイドスクリプティング(.php)を学びたかった。数週間前に始めたので、初心者のエラーを許してください。私はこのjsファイルを使用して、異なる方法で解決方法を文字または数字を出力するユーザー入力から四角形を生成する方法

<?php 
#initial page load 
#create functions and call them in program 
#create variables 
#get user input 
$row = filter_input(INPUT_GET, "row", FILTER_VALIDATE_INT); 
$col = filter_input(INPUT_GET, "col", FILTER_VALIDATE_INT); 

#for loop to generate rectangle or square of "F" or "5" 
function forTangle() { 
    $theader = '<table border = "0">'; 
    $tbody = ' '; 
    $sum = 0; 
    $tfooter = ' '; 
    $output = "Nested For Loop"; 
    for ($i = 0; $i < $row; $i++) { 
     $tbody += '<tr>'; 
     for($j = 1; $j < $col; $j++) { 
      $sum = $i + $j; 
      $tbody += '<td>'; 
      $tbody += "$i + $j + = $sum"; 
      $tbody += '</td>'; 
     } 
     $tbody += '</tr>'; 
    } 
     $tfooter = '</table>'; 
     echo $output; 
     #Variable that outputs the table of "F" or "5"??? 
     #how do i call the DOM and put this in a container? 

} 
?> 

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<link rel="stylesheet" type="text/css" href="q3.css"> 
<title>Allen Ricardo's Magic Rectangle</title> 
</head> 

<body> 
<div class="header"> 
<header><h1>Rectangle</h1></header> 
</div> 
<div class="prompt"> 
<h3>Rectangle</h3> 
<br> 
<h3><?php "rectangle has ${row}s, and ${col}s."; ?></h3> 
</div> 
<div class="output"> 
<p><?php echo forTangle(); ?></p> 

<!--container for rectangles--> 


</div> 

<div class="link"> 
<a href="q3_index.html">Back to Homepage</a> 
</div> 
</body> 
</html> 

******************************************** 

これは、次のとおりです。ここで

FFFFF 
FFFFF 
FFFFF 
FFFFF 
FFFFF 

は、私がこれまで持っているものです。

function forTangle() { 
var num_rows = document.getElementById('rows').value; 
var num_cols = document.getElementById('cols').value; 
var theader = '<table border="0">\n'; 
var tbody = ''; 
var sum = 0; 
var i; 
var j; 

    for(i=1; i<=num_rows;i++) 
     { 
      tbody += '<tr>'; 
      for(j=1; j<=num_cols;j++) 
     { 
       sum = i + j; <!--sum of row + column number--> 
       tbody += '<td>'; 
       tbody += (i + "+" + j + "=" + sum); <!--values in the table cell--> 
       tbody += '</td>'; 
     } 
       tbody += '</tr>\n'; 
    } 
      var tfooter = '</table>'; 
      document.getElementById("output3").innerHTML ="Nested for loop rectangle" + "<br>" + theader + tbody + tfooter; <!--output to screen--> 
} 

答えて

0

これがために使用されるか、あまりにもわからないERMのイム....しかし、そこは。

$letter = 'F'; 
    $width = 5; 
    $height = 8; 

    $row = str_repeat ('<td>' . $letter . '</td>' , $width); 
    $rows = []; 
    for ($i=0; $i < $height; $i++) { 
     $rows[] = '<tr>' . $row . '</tr>'; 
    } 

    echo '<table>' . implode('', $rows) . '</table>'; 

ハッピーコーディング。 PHPコミュニティにようこそ:)

+0

これは私の本の中で例を次のものに過ぎません。PHPでループを使用する方法を初心者に教えるだけです。私は、メソッド 'implode'を調べなければならないことが分かります。この本の中でそれを読まないでください。これほど多くのことを学ばなければならず、これはasp.netよりも難しいようです。ヘルプをよろしくお願いします...乾杯! – allendks45

+0

phpは簡単です.....これらはすべてGoogleの機能です。非常にシンプルです。 str_repeatを使って1行目を作る;高さで複数の行。 $ rows配列にすべて入れてください。 Implodeはこれらのコンポーネントを取り、それらを一緒に接着します。テーブルはExcelと同じですが、フォーマットはhtmlです –

関連する問題