0

我写了下面的代码,

我想要的是当变量为 4 时,创建一个新的<li>. 在下面的代码中,{$counter} 总是 1 为什么?我不能增加它

  <div id="slider">
            <ul>
                <li>
                {foreach from=$books value=book}
                {counter start=0 print=false assign=mycounter}
            { math equation="x + 1" x=$mycounter assign="counter" }


                {$counter}

                     <div class="video2">

                        <div class="thumb">
                            <div class="kitap"><a href="http://cmkzl.nokta.com/kitaplar/{$book.title}"><img src="{$imageUrl}/{$book.image}" width="145" height="205"></a></div>
                            <div class="kitap_baslik"><strong>{$book.title}</strong></div>

                            <div class="alt_baslik"><em>{$book.description|truncate:65:"..."}</em></div>
                            <div class="yorumlar_btn"><a href="b_u_tirmanirken"><img src="{$imageUrl}/yorumlar_btn.jpg" width="145" height="50"></a></div>
                        </div>
                    </div>
                </li>
                {if $counter is div by 4}
                <li>
                    {/if}
            {/foreach}

             </ul>

      </div>
4

1 回答 1

0

不如我用 PHP 给你答案

<div id="slider">
<ul>
    <li>
<?php
    $counter = 0;
    foreach( $book in $books)
    { ?>              
         <div class="video2">

            <div class="thumb">
                <div class="kitap"><a href="http://cmkzl.nokta.com/kitaplar/<?= $book.title ?>"><img src="<?=$imageUrl?>/<?=$book.image?>" width="145" height="205"></a></div>
                <div class="kitap_baslik"><strong><?=$book.title?></strong></div>

                <div class="alt_baslik"><em><?= truncateString($book.description,65,true,"...")?></em></div>
                <div class="yorumlar_btn"><a href="b_u_tirmanirken"><img src="<?=$imageUrl?>/yorumlar_btn.jpg" width="145" height="50"></a></div>
            </div>
        </div>

    <?php 
        $counter++;
        if($counter == 4){
            $counter = 0; ?>
            </li>
            <li>
    <?php
        }

    }

 function truncateString($str, $chars, $to_space, $replacement="...") {
   if($chars > strlen($str)) return $str;

   $str = substr($str, 0, $chars);

   $space_pos = strrpos($str, " ");
   if($to_space && $space_pos >= 0) {
      $str = substr($str, 0, strrpos($str, " "));
   }

   return($str . $replacement);
 }

    ?>
    </li>
 </ul>

于 2012-05-09T12:59:18.637 回答