-1

我刚刚开始使用 PHP 和 mySQL(现在大约 4 天)并且一直在尝试找到解决这个问题的方法......

我有一组从 mySQL 数据库返回的项目。我让这些流入一组 4 个“ul”列和一类“模型”,它将结果数除以 4 并返回“li”,无法弄清楚为什么我在结尾。我曾尝试将“ul”放入 foreach 语句中,但这也没有返回所需的结果。

谢谢你的帮助。

这是php:

<grid>
  <ul class="models">
    <?php

    $c = count($model);

    $s = ($c / 4); // change 4 to the number of columns you want to have.

    $i=1;

    foreach ($model as $models): ?>
      <li>
        <a href="model/?id=<?php htmlout($models['sn']); ?>" class="id<?php htmlout($models['sn']); ?>">
          <div class="<?php htmlout($models['callout']); ?>"></div>
          <div id="description">
             <h2><?php htmlout($models['firstname']); ?> <?php htmlout($models['lastname']); ?>
             </h2>
             <h3><span class="number">40</span> photo sets</h3>
             <h3><span class="number">756</span> images</h3>
             <p><?php htmlout($models['rightstext_left']); ?>
                <?php htmlout($models['firstname']); ?>
                <?php htmlout($models['rightstext_right']); ?>
             </p>
          </div>
       </a>
    </li>
    <?php if($i != 0 && $i % $s == 0)
    {
        ?>
  </ul>
  <ul class="models">
    <?php
    }
    $i++; ?>
    <?php endforeach; ?>
  </ul>
</grid>

的HTML

<grid>    
**excluded**
<ul class="models">
<li>
   <a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
   <div class="exclusive"></div>
   <div id="description">
      <h2>Model 19</h2>
      <h3><span class="number">1</span> attribute</h3>
      <h3><span class="number">6</span> attributes</h3>
      <p>textleft name textright</p>
    </div>
   </a>
</li>
<li>
   <a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
   <div class="exclusive"></div>
   <div id="description">
      <h2>Model 19</h2>
      <h3><span class="number">1</span> attribute</h3>
      <h3><span class="number">6</span> attributes</h3>
      <p>textleft name textright</p>
    </div>
   </a>
</li>
<li>
   <a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
   <div class="exclusive"></div>
   <div id="description">
      <h2>Model 19</h2>
      <h3><span class="number">1</span> attribute</h3>
      <h3><span class="number">6</span> attributes</h3>
      <p>textleft name textright</p>
    </div>
   </a>
</li>
<li>
   <a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
   <div class="exclusive"></div>
   <div id="description">
      <h2>Model 19</h2>
      <h3><span class="number">1</span> attribute</h3>
      <h3><span class="number">6</span> attributes</h3>
      <p>textleft name textright</p>
    </div>
   </a>
</li>
<li>
   <a href="model/?id=AZWW5RJE" class="idAZWW5RJE">
   <div class="exclusive"></div>
   <div id="description">
      <h2>Model 19</h2>
      <h3><span class="number">1</span> attribute</h3>
      <h3><span class="number">6</span> attributes</h3>
      <p>textleft name textright</p>
    </div>
   </a>
</li>
</ul>

4

1 回答 1

0

我稍微改变了你的逻辑。由于您使用模数,因此从以下开始会更有意义$i = 0

当您进行模运算时,您希望确保有一个整数,因此请尝试:

$s = ceil($c / 4);

然后在你的中使用它if来测试你是否在一个元素的末尾<ul>,并确保你没有在最后一次迭代中创建一个:

<?php if ($i % $s == $s - 1 && $i != $c - 1)
于 2012-08-24T17:52:08.207 回答