简体   繁体   English

如何将n的动态生成量 <div> 在 <li>

[英]How to put dynamically-generated n-amount of <div>s in <li>

Hello :) I realy need your help here. 您好:)我真的需要您的帮助。 I dynamically generate list of items but instead of putting each item in separate <li> I want to get something like this: 我动态生成项目列表,但我不想将每个项目放在单独的<li>而是要获得以下内容:

<ul>
   <li>
      <div>$pt</div>
      <div>$pt</div>
      <div>$pt</div>
   </li>
   <li>
      <div>$pt</div>
      <div>$pt</div>
      <div>$pt</div>
   </li>
</ul>

Here is code I have: 这是我有的代码:

<ul class="some-ul-class">
    <?php $itemCount = 3; ?>
       <?php $i=0; foreach ($p->getItems() as $pt): ?>
           <?php if ($i++%$itemCount==0): ?>
              <li class="item">
           <?php endif; ?>
                 <div>$pt</div>
              </li>
       <?php endforeach; ?>
</ul>

But as the result I get structure like this: 但是结果是我得到这样的结构:

<ul>
   <li>
      <div>$pt</div>
   </li>
   <div>$pt</div>
   <div>$pt</div>

   <li>
      <div>$pt</div>
   </li>
   <div>$pt</div>
   <div>$pt</div>
</ul>

Thank you for help 谢谢你的帮助

<ul class="some-ul-class">
<?php $itemCount = 3; ?>
   <?php $i=0; foreach ($p->getItems() as $pt): ?>
       <?php if ($i%$itemCount==0): ?>
          <li class="item">
       <?php endif; ?>
             <div>$pt</div>
       <?php if ($i%$itemCount==2): ?>
          </li>
       <?php endif; $i++; ?>
   <?php endforeach; ?>
</ul>

You can try this. 你可以试试看

<ul class="some-ul-class">
  <?php $itemCount = 3;
    $i=0;
    foreach ($p->getItems() as $pt):
      if ($i%$itemCount==0):
        echo '<li class="item">';
      endif;
        echo "<div>$pt</div>";
      if ($i%$itemCount==2):
        echo '</li>';
      endif; $i++;
    endforeach; ?>
</ul>

Try something like this: 尝试这样的事情:

<ul class="some-ul-class">
    <?php $itemCount = 4; ?>
    <li>
    <?php $i = 1; foreach ($p->getItems() as $pt): ?>
        <?php if ( $i % $itemCount == 0): ?>
            </li><li>
        <?php endif; ?>
        <?php $i++; ?>
        <div><?php echo $pt; ?></div>
    <?php endforeach; ?>
     </li>
</ul>

This generates: 这将产生:

<ul class="some-ul-class">
    <li>
        <div>1</div>
        <div>2</div>
        <div>3</div>
    </li><li>
            <div>4</div>
            <div>5</div>
            <div>6</div>
    </li>
</ul>

Demo 演示版

Your code will not achieve nested DIVS in LI because you need a multidimensional array to nest the items within the container. 您的代码将无法在LI中实现嵌套DIVS,因为您需要一个多维数组才能将项目嵌套在容器中。 The solution is to break the initial DB result set in to chunks with array chunk. 解决方案是将初始数据库结果集分成具有数组块的块。

This just splits your array (1,2,3,4,5,6) to ([0] => array(1,2,3), [2] => array(4,5,6) 这只是将数组(1,2,3,4,5,6)拆分为([0] => array(1,2,3),[2] => array(4,5,6)

Run through the loop below you will get two LI with 3 nested DIV. 通过下面的循环,您将获得两个带有3个嵌套DIV的LI。 The code is not tested but should be something like operational. 该代码未经测试,但应类似于可操作性。

<?php 

$items = array(1,2,3,4,5,6,8,9,10,11,12,13,14,15);         
// Your initial item array

$rows = 3;                           
// Number of rows in each li 

$items = array_chunk($items, $rows); 
// Final nested array in blocks of 3

if ($items) {       
    echo "<ul class='some-ul-class'>\n";        
    foreach ( $items as $item ) {           
        echo "<li class='items'>\n";            
        foreach ($item as $divs) {
            echo "<div>{$divs}</div>\n";
        }           
        echo "</li>\n";             
    }       
    echo "</ul>\n";
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM