在循环中创建 HTML 元素

发布于 2024-12-01 03:30:57 字数 218 浏览 2 评论 0原文

给定一个像 ie: 6 这样的数字,我需要生成 6 个 DIV 元素。
例如:

$number = 6;
// PHP generates the DIV for $number of times (6 in this case).

我该怎么做?如果是这种情况,我不是 PHP 循环专家。谢谢!

Given a number like i.e: 6 I need to generate 6 DIV elements.
For example:

$number = 6;
// PHP generates the DIV for $number of times (6 in this case).

How can I do it? I am not an expert of PHP loops, if this is the case. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

习惯成性 2024-12-08 03:30:57

您可以使用的不同类型循环的示例用途。希望您能看到它们是如何工作的。

Foreach 循环

    $element = "<div></div>";
    $count = 6;
    foreach( range(1,$count) as $item){
        echo $element;
    }

While 循环

   $element = "<div></div>";
   $count = 0;
   while($count < 6){
       $count++;
       echo $element;
   }

简单的 For 循环

$element = "<div></div>"; 
$count = 6;
for ($i = 0; $i < $count; $i++) {
    echo $element;
}

Example uses of the different types of loops you could use. Hopefully you can see how they work.

Foreach Loop

    $element = "<div></div>";
    $count = 6;
    foreach( range(1,$count) as $item){
        echo $element;
    }

While Loop

   $element = "<div></div>";
   $count = 0;
   while($count < 6){
       $count++;
       echo $element;
   }

Simple For Loop

$element = "<div></div>"; 
$count = 6;
for ($i = 0; $i < $count; $i++) {
    echo $element;
}
万劫不复 2024-12-08 03:30:57
function generateDIVs($number)
{
  for ($i = 0; $i <= $number; $i++)
  {
    echo "<div><div/>";
  }
}
function generateDIVs($number)
{
  for ($i = 0; $i <= $number; $i++)
  {
    echo "<div><div/>";
  }
}
草莓酥 2024-12-08 03:30:57

为了生成 6 个 div 元素,循环是必要的。

使用 while 循环:

$count = 1;
while($count <= 6){
    $count++;
    echo "<div></div>";
}

使用 for 循环:

$count = 6;
for ($i = 0; $i < $count; $i++) {
    echo "<div></div>";
}

In order to generate 6 div elements loop is necessary.

using while loop:

$count = 1;
while($count <= 6){
    $count++;
    echo "<div></div>";
}

using for loop:

$count = 6;
for ($i = 0; $i < $count; $i++) {
    echo "<div></div>";
}
三月梨花 2024-12-08 03:30:57
for ($i = 0; $i < 6; $i++) {
    echo "<div class=\"example\"></div>";
}

请注意,ID(# 部分)在页面上必须是唯一的,因此不能有 6 个不同的 div 具有相同的 #example id。

http://php.net/manual/en/control-structs.for.php

for ($i = 0; $i < 6; $i++) {
    echo "<div class=\"example\"></div>";
}

Note that IDs (the # part) have to be unique on a page, so you can't have 6 different divs with the same #example id.

http://php.net/manual/en/control-structures.for.php

凉城 2024-12-08 03:30:57

以下是我经常使用的一些示例,用于在使用 PHP 时快速模拟重复的 HTML

array_walk() 带有迭代索引和范围

$range = range(0, 5);
array_walk($range, function($i) {
  echo "
  <section class='fooBar'>
    The $i content
  </section>
  ";
});

如果厌倦了转义双引号 \" 或使用 '连接见鬼,你可以简单地使用Heredoc

$html = function($i) {
echo <<<EOT
  <section class="fooBar">
    The $i content
  </section>
EOT;
};

array_map($html, range(1, 6));

。 php#language.types.string.syntax.heredoc" rel="nofollow noreferrer">Heredoc 是结束 EOT; 不能有前导和后续空格或制表符 - 这可能看起来很难看以良好的结构 时使用

标记,因此我经常将函数放在文档顶部,并在需要 http://php.net/manual/en/function.str-repeat.php" rel="nofollow noreferrer">str_repeat() 当不需要索引时

$html = "
  <section class='fooBar'>
    Some content
  </section>
";

echo str_repeat($html, 6);
    

Here are some examples I use often, for quick mock-upping repeated HTML while working with PHP

array_walk() with iteration index and range

$range = range(0, 5);
array_walk($range, function($i) {
  echo "
  <section class='fooBar'>
    The $i content
  </section>
  ";
});

If tired of escaping double quotes \" or using ' or concatenation hell, you could simply use Heredoc.

$html = function($i) {
echo <<<EOT
  <section class="fooBar">
    The $i content
  </section>
EOT;
};

array_map($html, range(1, 6));

The only small "disadvantage" of using Heredoc is that the closing EOT; cannot have leading and following spaces or tabs - which might look ugly in a well structured markup, so I often place my functions on top of the document, and use <?php array_map($html, range(0, 5)) ?> where needed.

str_repeat() when an index is not needed

$html = "
  <section class='fooBar'>
    Some content
  </section>
";

echo str_repeat($html, 6);
    
策马西风 2024-12-08 03:30:57

你需要 echo 命令。基本上你是通过打印字符串来生成 html 的。示例

echo '<div> </div>';

将生成 1 个 div。你需要它 6 次。您可能也想使用循环,但这是一个太基本的问题,我给了您一个开始。

you need echo command. Basically you are generating html by printing a string. Example

echo '<div> </div>';

will generate 1 div. You need it 6 times. You might want to use loop as well, but this is too basic question and I gave you a start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文