Jump to content

Recommended Posts

Привет,

Подскажите, как вывести новости в списки....
Нужна примерно такая структура
<ul>
   <li></li>
   <li></li>
   <li></li>
   <li></li>
</ul>
<ul>
   <li></li>
   <li></li>
   <li></li>
   <li></li>
</ul>
<ul>
   <li></li>
   <li></li>
   <li></li>
   <li></li>
</ul>
и т.д.
Как ограничить вывод по 4 <li> в 1 <ul>

Link to post
Share on other sites

array_chunk() php

{foreach array_chunk($last_posts, 4, false) as $post}
    <ul>
        {foreach $post as $p}
            <li data-post="{$p->id}">{$p->date|date} <a href="blog/{$p->url}">{$p->name|escape}</a></li>
        {/foreach}
    </ul>	
{/foreach}

Выдаст что то типо:

<ul>
     <li></li>
     <li></li>
     <li></li>
     <li></li>
</ul>
<ul>
     <li></li>
     <li></li>
     <li></li>
     <li></li>
</ul>
<ul>
     <li></li>
     <li></li>
     <li></li>
     <li></li>
</ul>
<ul>
     <li></li>
     <li></li>
     <li></li>
     <li></li>
</ul>
Link to post
Share on other sites
{section name=last_post loop=$last_posts}

{if $smarty.section.last_post.first}

      <ul class="item active">

{else:}

      <ul class="item">

{/if}

{/section}

Я все правильно понял?

Link to post
Share on other sites

Нет, я бы вывел так:

    <ul class="item active"> 
        {foreach $post as $p} 
            <li data-post="{$p->id}">{$p->date|date} <a href="blog/{$p->url}">{$p->name|escape}</a></li>
 {if $p@iteration%4 == 0 && !$post@last}
</ul><ul class="item">
{/if}
        {/foreach} 
    </ul>
Link to post
Share on other sites
  • 1 year later...

В данном случае два цикла лучше, так как код проще, легче, понятнее.

С точки зрения экономности выполнения разницы  практически нет - выполнять один цикл в 100 итераций или столько же в двойном цикле (4 итерации во внешнем) * (25 итераций во внутреннем).

С точки зрения легкости повторного использования - код Чудилла, например, при использовании таблиц в верстке легко и просто модифицируется, а код Kosjak76 - уже повозиться надо.

С точки зрения логичности и оптимальности - код Kosjak76 выводит, например </ul> в ДВУХ разных местах. Это неоправданное усложнение, такое типично для начинающих программистов.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...