模式输出卡住了 [英] Pattern Output, stuck

查看:195
本文介绍了模式输出卡住了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使用一个表(无边界)和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆