【问题标题】:php foreach and htmlphp foreach 和 html
【发布时间】:2018-03-30 13:48:25
【问题描述】:

所以我这里有一个逻辑问题。

我想要什么:

通过数组循环(实际上是一个可访问的对象) 如果它是第一个元素回显<div>,则每三个元素回显结束 </div>.

如果少于 3 个,当然也结束 div。 编辑:由于我不清楚,我想要实现的是基于元素的 n°。 矛盾的是,我也需要它来处理 0 个元素。

0 个元素:

<div><div>

1 个元素:

 <div>
   <a></a>
 </div>

3 个元素:

 <div>
      <a></a>
      <a></a>
      <a></a>
 </div>

5 个元素:

    <div>
      <a></a>
      <a></a>
      <a></a>
    </div>
    <div>
      <a></a>
      <a></a>
    </div>

等等......你明白了

我的错误代码:

        $counter = 0;
        foreach ($prods as $prod) {
            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '<div data-count="' . $counter . '">';  //just keeping track
            }
            $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';

            $counter++;

            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '</div>';
            }               
        }
        $counter = 0;
        if($counter == 0 || ($counter % 3) == 0) {
            $htm_l .= '</div>';
        } 

我能做什么? (prod 只是一个对象的集合,可以是一个字符串数组)

【问题讨论】:

  • 请附上我们可以测试的MVCE。我们看不到您的代码现在不起作用,因为我们无法测试它,因为我们不知道 $prods 是什么。
  • 您要归档左侧的文件吗??
  • 不,问题不在于 $prod 或 $variable,如果您愿意,可以将其视为字符串数组。我希望能够同时实现右侧或左侧,这取决于 prods ""array"" 中有多少项目
  • 你的代码的输出是什么?
  • 告诉我们你需要什么输出

标签: php html foreach


【解决方案1】:

您希望无条件地以 &lt;div&gt; 开头并以 &lt;/div&gt; 结尾。唯一的条件是在循环期间何时关闭/打开。所以这样的事情应该可以工作:

$counter = 0;
$htm_l   = '<div data-count="0">';

foreach ( $prods as $prod ) {
    $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';

    $counter++;

    if ( $counter % 3 == 0 ) {
        $htm_l .= '</div>';

        if ( $counter != count( $prods )) {
            $htm_l .= "<div data-count=\"$counter\">";
        }
    }
}

if ( $counter % 3 ) {
    $htm_l .= '</div>';
}

【讨论】:

    【解决方案2】:

    尝试以下方法:

    $counter = 1;
    $total = count($prods);
    $htm_l = '';
    $htm_l .= "<div data-count=\"1\">\r\n";
    foreach ($prods as $prod)
    {
        $prod->id = $counter;
    
        $htm_l .= "<a data-id=\"{$prod->id}\"> link </a>\r\n";
    
    
        if (($counter % 3) == 0)
        {
            $htm_l .= "</div>\r\n";
        }
        if($counter == $total){$counter++;continue;}
        if (($counter % 3) == 0)
        {
            $htm_l .= "<div data-count=\"$counter\">\r\n";
        }
    
        $counter++;
    }
    if (($counter % 3) == 0)
    {
        $htm_l .= '</div>';
    }
    
    echo($htm_l);
    

    这将遍历$prods,并在每3个&lt;a&gt;标签之后添加&lt;div&gt;

    【讨论】:

      【解决方案3】:

      这是未经测试的代码,但应该适合您,除非您有空 prods 对象的特殊情况并且您不希望生成空 div。这将需要一个稍微复杂的解决方案

      $counter = 0;
      // start the first div
      $htm_l = '<div data-count="' . $counter . '">';
      foreach ($prods as $prod) {
          $counter++;
          // close the current div and open a new one after 3 objects
          if ($counter % 3 == 0) {
              $htm_l .= '</div> <div data-count="' . $counter . '">';
          }
          // add the object specific data
          $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';
      }
      // close the last div
      $htm_l .= '</div>';
      

      要处理这种特殊情况,您可以这样做。

      $counter = 0;
      // find out how many total objects we will be stepping through
      $total_count = count($prods);
      $htm_l = '';
      foreach ($prods as $prod) {
          $counter++;
          // always start with a div for the first of 3 objects
          if ($counter % 3 == 1) {
              $htm_l .= '<div data-count="' . $counter . '">';
          }
          // add the object specific data
          $htm_l .= '<a data-id="' . $prod->id . '"> link </a>';
          // close the div after 3 objects or after the last object
          if ($counter % 3 == 0 || $counter == $total_count) {
              $htm_l .= '</div>';
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-17
        • 1970-01-01
        • 1970-01-01
        • 2010-11-13
        • 2015-12-08
        相关资源
        最近更新 更多