...

View Full Version : Inject code in Foreach



Pcfr43k
01-27-2010, 04:31 PM
I have a website that uses a foreach to display all items in a row.
Now I want to inject some code trough these rows.

Here is the foreach code of the website:

{foreach item=job from=$jobs}
<div class="{cycle values='row,row-alt'}">
<span class="row-info">
<img src="{$BASE_URL}_templates/{$THEME}/img/icon-{$job.type_var_name}.png" alt="{$job.type_name}" />
<a href="{$BASE_URL}{$URL_JOB}/{$job.id}/{$job.url_title}/" title="{$job.title}">{$job.title}</a> <span class="la">{$translations.homepage.at}</span> {$job.company}{if $job.is_location_anywhere}, {$translations.jobs.location_anywhere}{else} <span class="la">{$translations.homepage.in}</span> {$job.location}{/if}
</span>
<span class="time-posted"><img src="{$BASE_URL}_templates/{$THEME}/img/clock.gif" alt="" /> {$job.created_on}</span>
</div>
{/foreach}
(This realy works on the website :p)

This looks like this in the output:

(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)

How can I inject some code after every 5 entries?
So instead of a long list you get this:

(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
---(code to be implemented)---
(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
(Fulltime)(Title with URL)(Location)---(space)---(Clock)(posttime)
...

Can this be done by another foreach that goes before this foreach?
Or an array that goes +1 for every "each" and when reaching 5 it gets reset and injects some code?

I have a hard time on this because I can't find any scripts of this nature.

JAY6390
01-27-2010, 05:04 PM
I'm a bit rusty with Smarty foreach loops but I think this will do it
{foreach item=job from=$jobs name=jobloop}
<div class="{cycle values='row,row-alt'}">
<span class="row-info">
<img src="{$BASE_URL}_templates/{$THEME}/img/icon-{$job.type_var_name}.png" alt="{$job.type_name}" />
<a href="{$BASE_URL}{$URL_JOB}/{$job.id}/{$job.url_title}/" title="{$job.title}">{$job.title}</a> <span class="la">{$translations.homepage.at}</span> {$job.company}{if $job.is_location_anywhere}, {$translations.jobs.location_anywhere}{else} <span class="la">{$translations.homepage.in}</span> {$job.location}{/if}
</span>
<span class="time-posted"><img src="{$BASE_URL}_templates/{$THEME}/img/clock.gif" alt="" /> {$job.created_on}</span>
</div>
{if $smarty.foreach.jobloop.iteration % 5}
<div>PUT YOUR CODE HERE</div>
{/if}
{/foreach}

Fou-Lu
01-27-2010, 05:05 PM
This isn't a foreach, its a custom interpreter for a foreach. You'll need to figure out what this does before you can consider modifying the code available.
Although its probably 10x easier to do it with a for loop, you can do it with a foreach:


$i = 0;
foreach ($a AS $k => $v)
{
// Do stuff
if (++$i % 5 == 0)
{
// Do stuff every fifth row
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum