Jump to content

Break out of foreach loop after first loop?


MichaelMcG

Recommended Posts

Hi,

 

I'd like to cancel my foreach loop after the first loop:

 

{foreach from=$entries item=entry}
		<li><a class="bloglink" {if $rewrite==1}href="{$content_dir}blog/entry/{$entry.id_entry}-{$entry.link}"{else}href="{$content_dir}modules/blog/index.php?entry={$entry.id_entry}"{/if}>{$entry.title|escape|truncate:30}</a><br />{$entry.entry|truncate:130}</li>
		{/foreach}

 

How do I achieve this?

 

 

Thank you.

Link to comment
Share on other sites

Hi,

 

I'd like to cancel my foreach loop after the first loop:

 

{foreach from=$entries item=entry}
		<li><a class="bloglink" {if $rewrite==1}href="{$content_dir}blog/entry/{$entry.id_entry}-{$entry.link}"{else}href="{$content_dir}modules/blog/index.php?entry={$entry.id_entry}"{/if}>{$entry.title|escape|truncate:30}</a><br />{$entry.entry|truncate:130}</li>
		{/foreach}

 

How do I achieve this?

 

 

Thank you.

 

I'm assuming in the {foreach} loop I'd do something like:

 

i = 0;
i++;
// show contents
if {$i=1} break;

 

but I'm not sure how to achieve this with PrestaShop.

 

 

Thanks for any help.

Link to comment
Share on other sites

please try this (assume you want execute only one time.

 

 

{foreach from=$entries item=entry name=foo}
   {if $smarty.foreach.foo.index == 1} {break}{/if}
   <li><a class="bloglink" {if $rewrite==1}href="{$content_dir}blog/entry/{$entry.id_entry}-{$entry.link}"{else}href="{$content_dir}modules/blog/index.php?entry={$entry.id_entry}"{/if}>{$entry.title|escape|truncate:30}</a><br />{$entry.entry|truncate:130}</li>
{/foreach}

 

 

the above code can used for any number of executions, you can execute 2 time, or 3 times, just changes the number.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...