11
JCDunnart
Re: Stop FOREACH looping using BREAK (PHP)
  • 2007/4/8 9:10

  • JCDunnart

  • Not too shy to talk

  • Posts: 114

  • Since: 2006/7/1 5


Possible solution:
<table>
<
tr>
    <
td>
    <{foreach 
item=blog_user from=$block.popnupblog name=foo}>
        <{if 
$smarty.foreach.foo.index == 5}>
            </
td><td>
        <{/if}>
        ... [
content]
    <{/foreach}>
    </
td>
</
tr>
</
table>

Keep in mind that arrays are zero-based - the first item is 0, the next is 1 etc...

12
tzvook
Re: Stop FOREACH looping using BREAK (PHP)
  • 2007/4/8 10:45

  • tzvook

  • Just can't stay away

  • Posts: 875

  • Since: 2003/2/1 2


Quote:

JCDunnart wrote:
Possible solution:
<table>
<
tr>
    <
td>
    <{foreach 
item=blog_user from=$block.popnupblog name=foo}>
        <{if 
$smarty.foreach.foo.index == 5}>
            </
td><td>
        <{/if}>
        ... [
content]
    <{/foreach}>
    </
td>
</
tr>
</
table>

Keep in mind that arrays are zero-based - the first item is 0, the next is 1 etc...


Tryed it in the articles module and code is not working .... seems that the line
<{if $smarty.foreach.foo.index == 5}>

is not doing it's job

13
JCDunnart
Re: Stop FOREACH looping using BREAK (PHP)
  • 2007/4/8 11:17

  • JCDunnart

  • Not too shy to talk

  • Posts: 114

  • Since: 2006/7/1 5


Post the code you're using.

This Smarty structure works, so it must be something out of place in your code...

14
tzvook
Re: Stop FOREACH looping using BREAK (PHP)
  • 2007/4/8 11:39

  • tzvook

  • Just can't stay away

  • Posts: 875

  • Since: 2003/2/1 2


10x
The code ...

<table border="1">
<
tr>
    <
td>
<{foreach 
item=article from=$block.articles name=tztz}>
        <{if 
$smarty.foreach.tztz.index == 5}>
        </
td><td>
        <{/if}>
<
div style="min-width:270px; min-height:77px;  width:100%; margin-bottom:3px; padding-bottom:4px;"><img src="<{$xoops_url}>/themes/default/images/w-pixel.gif" width="270" style="width:270px;" height="1" alt="" border="0"><br>
<{if 
$article.image}>
<
span style="float:right; background-color: transparent;"><a href="<{$xoops_url}>/modules/<{$block.dirname}>/view.article.php<{$smarty.const.URL_DELIMITER}><{$article.art_id}>/c<{$article.cat_id}>"><img style="margin-left:7px;" src="<{$article.image.url}>" align="left" hspace"0" vspace"0"  alt="<{$article.image.caption}>"></a></span
<{/if}>

<
a href="<{$xoops_url}>/modules/<{$block.dirname}>/view.article.php<{$smarty.const.URL_DELIMITER}><{$article.art_id}>/c<{$article.cat_id}>"><b style="color:#b48243;"><{$article.art_title}></b></a> <span>(<a href="<{$xoops_url}>/modules/<{$block.dirname}>/view.category.php<{$smarty.const.URL_DELIMITER}><{$article.cat_id}>"><{$article.category}></a>)</span> <br>

<{if 
$article.summary}>
<{
$article.summary}>
<{/if}>

</
div>
    <{/foreach}>
    </
td>
</
tr>
</
table>

15
tzvook
Re: Stop FOREACH looping using BREAK (PHP)
  • 2007/4/8 11:54

  • tzvook

  • Just can't stay away

  • Posts: 875

  • Since: 2003/2/1 2


10x JCDunnart
... I read a bit and found the solution ...

it simply shoud be:

<{if $smarty.foreach.tztz.iteration == 6}>

instead of

<{if $smarty.foreach.tztz.index == 5}>

And in that way I think almost each block or <foreach> result in XOOPS templates can be divided to one's needs
Which helps me A LOT ... hope it will be to use for all ...

16
JCDunnart
Re: Stop FOREACH looping using BREAK (PHP)
  • 2007/4/9 10:14

  • JCDunnart

  • Not too shy to talk

  • Posts: 114

  • Since: 2006/7/1 5


Well, I don't understand it - both the 'index' and 'iteration' syntax should have given the same result.

Very weird... but you got it to work, that's the main thing

17
Knuxchan
Re: Stop FOREACH looping using BREAK (PHP)
  • 2009/2/13 1:56

  • Knuxchan

  • Just popping in

  • Posts: 54

  • Since: 2008/12/10


I don't understand how that fixed the problem...

This code:
<{foreach item=article from=$block.articles name=tztz}> 
        <{if 
$smarty.foreach.tztz.index == 5}> 
        </
td><td
        <{/if}> 
</foreach>


won't break a foreach loop. All it's doing is checking to see if the current index is 5, but there's no 'break' syntax to actually break the loop so it will keep doing interations to the very end while it continues to check if the index is still equal to 5, even though it may be way above 5.

Is there not any sort of 'break' syntax for Smarty? or a 'Goto' to try to get out of the loop?

I was trying to display a certain amount of items on the screen, but I wanted the loop to stop once it reached 3, but there's no way to break out of the loop when this condition is met.

EDIT: Nevermind, I found this on another site and it works. I'm going to go ahead and post it here in case someone else has the same problem as me:

Quote:
(From:http://qaix.com/php-web-programming/148-751-smarty-break-out-of-foreach-loop-read.shtml)
I think that guy means a {foreach} tag inside a template ­.

Well the answer is easy - such a function is missing. If you really
need it (normally such logic should be in your php scripts, not in your
templates...), write your own (compiler) plugins for that.


<?php
// compiler.break.php
function smarty_compiler_bre­ak($contents, &$smarty)
{
return 'break;';
}

?>
<?php
//compiler.continue­.php
function smarty_compiler_con­tinue($contents, &$smarty)
{
return 'continue;';
}

?>

Create these two files and put them into your plugins directory
(notice the naming convention compiler.xxx.php). Now, you should
be able to leave foreach and section loops with {break} and to leave
out one step with {continue}.

18
tzvook
Re: Stop FOREACH looping using BREAK (PHP)
  • 2009/2/13 11:57

  • tzvook

  • Just can't stay away

  • Posts: 875

  • Since: 2003/2/1 2


@ Knuxchan
I needed it then not for "breaking the loop" but for dividing it into 2 colums
I needed to divide string of 10 results into 2 colums of 5 results .... so my solution was good for my needs - not for yours

That one .... was long ago ... 2 years ... when the world was young

19
Knuxchan
Re: Stop FOREACH looping using BREAK (PHP)
  • 2009/2/13 13:08

  • Knuxchan

  • Just popping in

  • Posts: 54

  • Since: 2008/12/10


ah ok, then. Well I was doing a search in the forum for trying to stop a foreach loop and this one came up.

Login

Who's Online

166 user(s) are online (93 user(s) are browsing Support Forums)


Members: 0


Guests: 166


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits