Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Inject code in Foreach

    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:
    PHP Code:
    {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 )

    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.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    I'm a bit rusty with Smarty foreach loops but I think this will do it
    PHP Code:
    {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} 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    $i 0;
    foreach (
    $a AS $k => $v)
    {
        
    // Do stuff
        
    if (++$i == 0)
        {
            
    // Do stuff every fifth row
        
    }

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •