模式输出卡住了 [英] Pattern Output, stuck
问题描述
我需要使用一个表(无边界)和for循环来创建2个模式:
/ p>
1 -
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
< table> ;
< b> Patroon I< / b>
<?php
$ pattern = array(); ($ pyramid = 1; $ pyramid< = 6; $ pyramid ++){
$ pattern [$ pyramid] = $ pyramid;
;
echo< tr>
< td class ='td1'> 。 $ pattern [1]。 < / TD> 中;
if(array_key_exists(2,$ pattern)){
echo< td class ='td1'> 。 $ pattern [2]。 < / TD> 中;
if(array_key_exists(3,$ pattern)){
echo< td class ='td1'> 。 $ pattern [3]。 < / TD> 中;
if(array_key_exists(4,$ pattern)){
echo< td class ='td1'> 。 $ pattern [4]。 < / TD> 中;
if(array_key_exists(5,$ pattern)){
echo< td class ='td1'> 。 $ pattern [5]。 < / TD> 中;
if(array_key_exists(6,$ pattern)){
echo< td class ='td1'> 。 $ pattern [6]。 < / TD> 中;
}
echo< / tr>;
}
?>
< / table>
和其他模式
lockquote
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
我似乎无法弄清楚。
我试图把前面的代码,尝试rsort($模式),尝试了很多的IF语句,现在我坚持:S $ / / b
$ b
任何人对我有什么提示?
谢谢!
< table>不需要任何数组或花式函数,只需要两个嵌套for循环。
<?php
// 1
// 1 2
// 1 2 3
// ...
$ rows = 6;
for($ row = 1; $ row <= $ rows; $ row ++){
echo< tr>;
for($ col = 1; $ col <= $ row; $ col ++){
echo< td class ='td1'> 。 $ col。 < / TD> 中;
}
echo< / tr>;
}
?>
< / table>
< br />
< table>
<?php
// 1 2 3 4 5 6
// 1 2 3 4 5
// 1 2 3 4
// ...
$ rows = 6;
for $($ row = $ rows; $ row> 0; $ row--){
echo< tr>;
for($ col = 1; $ col <= $ row; $ col ++){
echo< td class ='td1'> 。 $ col。 < / TD> 中;
}
echo< / tr>;
}
?>
< / table>
在平衡表行的关闭行标记之前添加以下代码。
if($ span = $ rows - $ row)
echo< td colspan ='$ span'>< / td> ;
Pretty new to all this, but here i go...
I need to make 2 patterns using a table (without borders) and for-loops:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
I did work this one out with (probably not the easiest way tho, but it works):
<table>
<b>Patroon I</b>
<?php
$pattern = array();
for($pyramid = 1; $pyramid <=6; $pyramid++){
$pattern[$pyramid] = $pyramid;
echo "<tr>
<td class='td1'>" . $pattern[1] . "</td>";
if(array_key_exists(2, $pattern)){
echo "<td class='td1'>" . $pattern[2] . "</td>";
}
if(array_key_exists(3, $pattern)){
echo "<td class='td1'>" . $pattern[3] . "</td>";
}
if(array_key_exists(4, $pattern)){
echo "<td class='td1'>" . $pattern[4] . "</td>";
}
if(array_key_exists(5, $pattern)){
echo "<td class='td1'>" . $pattern[5] . "</td>";
}
if(array_key_exists(6, $pattern)){
echo "<td class='td1'>" . $pattern[6] . "</td>";
}
echo "</tr>";
}
?>
</table>
and the other pattern
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Which I can't seem to figure out.
I tried to turn the previous code around, tried rsort($pattern), tried a lot of IF-statements, and now im stuck :S
Anyone has a hint for me?
Thanks!
You don't need any array or fancy function, just two nested for loops.
<table>
<?php
// 1
// 1 2
// 1 2 3
// ...
$rows = 6;
for ($row = 1; $row <= $rows; $row++) {
echo "<tr>";
for ($col = 1; $col <= $row; $col++) {
echo "<td class='td1'>" . $col . "</td>";
}
echo "</tr>";
}
?>
</table>
<br />
<table>
<?php
// 1 2 3 4 5 6
// 1 2 3 4 5
// 1 2 3 4
// ...
$rows = 6;
for ($row = $rows; $row > 0; $row--) {
echo "<tr>";
for ($col = 1; $col <= $row; $col++) {
echo "<td class='td1'>" . $col . "</td>";
}
echo "</tr>";
}
?>
</table>
Add the following code before the closing row tag for balanced table rows.
if ($span = $rows - $row)
echo "<td colspan='$span'></td>";
这篇关于模式输出卡住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!