PHP 中随机数量的 div 和随机数量的元素

发布于 2024-10-09 17:15:59 字数 801 浏览 0 评论 0原文

我需要从随机数量的 $totalItems 生成随机数量的 div,每个 div 包含五个项目(以及最后一个 div 中的剩余项目),而且并非所有项目都满足 $OKItems...希望代码比我解释得更好。

我的问题是这个脚本生成空的 div,其中没有内容。

<?php

  $OKItems = 0;
  $totalItems = rand(2,30);

  for ($i = 0; $i < $totalItems; $i++) { 
    echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br />' : '';

    $testValue = rand(0, 1);
    if ($testValue != 0) {
      echo '1'; 
      $OKItems++;
    }

    echo ($OKItems % 5 == 0 || $i+1 == $totalItems) ? '<br />div close<br />' : '';
  } 

?>

这就是我可能得到的:

div open

div close
div open
11111
div close
div open

div close
div open

div close
div open
11
div close

这就是我在这种情况下想要的:

div open
11111
div close
div open
11
div close

I need to generate random number of divs with five items per div (and remaining items in the last div) from random number of $totalItems and also not all the items satisfy $OKItems... Hopefully the code explains better than me.

My problem is that this script generates empty divs with no content in them.

<?php

  $OKItems = 0;
  $totalItems = rand(2,30);

  for ($i = 0; $i < $totalItems; $i++) { 
    echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br />' : '';

    $testValue = rand(0, 1);
    if ($testValue != 0) {
      echo '1'; 
      $OKItems++;
    }

    echo ($OKItems % 5 == 0 || $i+1 == $totalItems) ? '<br />div close<br />' : '';
  } 

?>

This is what I might get:

div open

div close
div open
11111
div close
div open

div close
div open

div close
div open
11
div close

And this is what I would have wanted in this case:

div open
11111
div close
div open
11
div close

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

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

发布评论

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

评论(3

り繁华旳梦境 2024-10-16 17:15:59
<?php

const N = 5;
$totalItems = rand(2,30);

$items = array() ;
for ($i = 0; $i < $totalItems; $i++) { 
    $testValue = rand(0, 1);
    if ($testValue != 0) {
      $items[] = 1 ;
    }

    if( N == sizeof($items) || (($i == $totalItems - 1) && 0 < sizeof($items))  ) {
        echo "<div>" . join(",", $items) . "</div>";
        $items = array() ;
    }
} 
<?php

const N = 5;
$totalItems = rand(2,30);

$items = array() ;
for ($i = 0; $i < $totalItems; $i++) { 
    $testValue = rand(0, 1);
    if ($testValue != 0) {
      $items[] = 1 ;
    }

    if( N == sizeof($items) || (($i == $totalItems - 1) && 0 < sizeof($items))  ) {
        echo "<div>" . join(",", $items) . "</div>";
        $items = array() ;
    }
} 
当梦初醒 2024-10-16 17:15:59

我认为你的代码需要更多的结构。

我的方法是将其分为几个阶段,而不是尝试在输出数据的循环中执行所有逻辑。

我的建议:

  1. 决定要测试多少个项目
  2. 测试每个项目并仅复制传递到新数组中的项目 将
  3. 这个新数组划分为 5 个集合
  4. 将每个分区输出为 div

代码(未经测试):

// Decide how many items to test
$totalItems = rand(2,30);

// Test these items and add them to an accepted array
$items = Array();
for ($i = 0; $i < $totalItems; $i++) { 
  $testValue = rand(0, 1);
  if ($testValue != 0) { $items[] = "1" }
}

//Partition them into sections
$partitions = array_chunk($items,5);

//Output as divs
foreach($partitions as $partition):
  echo 'div open <br />';
    foreach($partition as $item):
      echo $item . "<br />";
    endforeach;
  echo 'div close <br />';
endforeach;

当您将代码拆分为逻辑步骤时,维护和调试变得更加容易。

I think you need a bit more structure to your code.

My approach would be to break it up into several stages, as opposed to trying to do all the logic in the loop that outputs data.

What I'd suggest:

  1. Decide how many items to be tested
  2. Test each item and only copy the ones that pass into a new array
  3. Partition this new array into sets of 5
  4. Output each partition as a div

Code (untested):

// Decide how many items to test
$totalItems = rand(2,30);

// Test these items and add them to an accepted array
$items = Array();
for ($i = 0; $i < $totalItems; $i++) { 
  $testValue = rand(0, 1);
  if ($testValue != 0) { $items[] = "1" }
}

//Partition them into sections
$partitions = array_chunk($items,5);

//Output as divs
foreach($partitions as $partition):
  echo 'div open <br />';
    foreach($partition as $item):
      echo $item . "<br />";
    endforeach;
  echo 'div close <br />';
endforeach;

When you split up the code into logical steps, it becomes much easier to maintain and debug.

黎夕旧梦 2024-10-16 17:15:59
<?php
$OKItems = 0;
$totalItems = rand(2,30);

for ($i = 0; $i < $totalItems; $i++) { 
  echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br>' : '';

  $testValue = rand(0, 1);
  if ($testValue != 0) {
    echo '1'; 
    $OKItems++;
  }

  if($OKItems % 5 == 0 || $i+1 == $totalItems) {
      echo '<br>div close<br>';
      $OKItems = 0;
  }
} 

?>

这应该可以工作;)

我更改了 if 函数的检查行,该函数也重置了 $OKItems。你遇到的问题(我认为)是你得到了 0 作为随机值,这将使 $OKitems 保持在 5 上。

<?php
$OKItems = 0;
$totalItems = rand(2,30);

for ($i = 0; $i < $totalItems; $i++) { 
  echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br>' : '';

  $testValue = rand(0, 1);
  if ($testValue != 0) {
    echo '1'; 
    $OKItems++;
  }

  if($OKItems % 5 == 0 || $i+1 == $totalItems) {
      echo '<br>div close<br>';
      $OKItems = 0;
  }
} 

?>

That should be working ;)

I changed your check line for an if function that also resets your $OKItems. The problem you had (i think) was that you got a 0 as the random value and that would keep $OKitems on 5.

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