Nikolas Posted May 27, 2014 Report Share Posted May 27, 2014 Привет,Подскажите, как вывести новости в списки....Нужна примерно такая структура<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> Quote Link to post Share on other sites
Чудилла Posted May 27, 2014 Report Share Posted May 27, 2014 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> Quote Link to post Share on other sites
Nikolas Posted May 27, 2014 Author Report Share Posted May 27, 2014 А так, чтоб первый <ul> был с классом <ul class="item active">А остальные просто <ul class="item"> Quote Link to post Share on other sites
Nikolas Posted May 27, 2014 Author Report Share Posted May 27, 2014 {section name=last_post loop=$last_posts} {if $smarty.section.last_post.first} <ul class="item active"> {else:} <ul class="item"> {/if} {/section} Я все правильно понял? Quote Link to post Share on other sites
Nikolas Posted May 27, 2014 Author Report Share Posted May 27, 2014 Не работает ... Не нравится мне этот параметр name=last_post Quote Link to post Share on other sites
Чудилла Posted May 27, 2014 Report Share Posted May 27, 2014 @first {foreach array_chunk($last_posts, 4, false) as $post} <ul{if $post@first} class="item active"{else} class="item"{/if}> {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} Quote Link to post Share on other sites
Kosjak76 Posted May 27, 2014 Report Share Posted May 27, 2014 Нет, я бы вывел так: <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> Quote Link to post Share on other sites
Kosjak76 Posted May 27, 2014 Report Share Posted May 27, 2014 Если вы считаете, что два цикла лучше одного - дело ваше Quote Link to post Share on other sites
Kors Posted August 28, 2015 Report Share Posted August 28, 2015 В данном случае два цикла лучше, так как код проще, легче, понятнее.С точки зрения экономности выполнения разницы практически нет - выполнять один цикл в 100 итераций или столько же в двойном цикле (4 итерации во внешнем) * (25 итераций во внутреннем).С точки зрения легкости повторного использования - код Чудилла, например, при использовании таблиц в верстке легко и просто модифицируется, а код Kosjak76 - уже повозиться надо.С точки зрения логичности и оптимальности - код Kosjak76 выводит, например </ul> в ДВУХ разных местах. Это неоправданное усложнение, такое типично для начинающих программистов. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.