Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

smarty foreach more than 1 do something else do nothing

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
1linedesign
Smarty n00b


Joined: 21 Jan 2010
Posts: 3

PostPosted: Thu Jan 21, 2010 12:29 am    Post subject: smarty foreach more than 1 do something else do nothing Reply with quote

Hi Guys,

I am trying to do something which i think should be simple here but for some reason i am unable to get my head around it - i have even tried using {section} which looked to be the solution but also failed on what i need it to do.

I reverted my code back to the following:

{* $Id: product_files.tpl 7419 2009-05-05 11:40:34Z lexa $ *}

{foreach from=$files item="file" name="file_loop"}
{if $smarty.foreach.file_loop.total > 1}
<p>To preview tracks from this album please use the "preview" links below.</p>
{else}
<a href="{$index_script}?dispatch=orders.get_file&amp;file_id={$file.file_id}&amp;preview=Y" onclick="playTrack('{$index_script}?dispatch=orders.get_file&file_id={$file.file_id}&preview=Y'); return false;">preview</a>
{/if}
{/foreach}


which prints out the preview link as it should as for pages that only have one results and with the above code with someting more it does print out the message to preview track..... but it loops this message for each one it found in the foreach - i am trying to do the following:

if more than 1 result is found in the foreach show the message "To preview tracks from this album please use the "preview" links below" else if it is just one results so the "preview" link.

any ideas on this on how i can limit it to show only once if only 1 result is found and display the preview link and if more than one show that message without it showing the message for each result found?

Thanks,
James
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jan 21, 2010 12:47 am    Post subject: Reply with quote

Code:
{foreach ...}
  ... print row ...
{foreachelse}
   No rows are found.
{/foreach}


http://www.smarty.net/manual/en/language.function.foreach.php
Back to top
View user's profile Send private message Visit poster's website
1linedesign
Smarty n00b


Joined: 21 Jan 2010
Posts: 3

PostPosted: Thu Jan 21, 2010 12:58 am    Post subject: Reply with quote

The thing is rows will be found

i would to do something like

foreach

if more than more record is found

show single message

if 1 result is found

return the result




i just tried the method you suggested but i got the following:



To preview tracks from this album please use the "preview" links below.

To preview tracks from this album please use the "preview" links below.

To preview tracks from this album please use the "preview" links below.


because it noticed there was 3 results in foreach and showed the message which is correct but if it is more than 1 i just need it to show the message once


now it shows nothing if just 1 result is found when i need it to show the preview link


revised code is as follows:



{foreach from=$files item="file" name="file_loop"}
{if $smarty.foreach.file_loop.total > 1}
<p>To preview tracks from this album please use the "preview" links below.</p>
{/if}
{foreachelse}
<a href="{$index_script}?dispatch=orders.get_file&amp;file_id={$file.file_id}&amp;preview=Y" onclick="playTrack('{$index_script}?dispatch=orders.get_file&file_id={$file.file_id}&preview=Y'); return false;">preview</a>
{/foreach}


sorry to be a pain!

Thanks again,
James
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Thu Jan 21, 2010 1:36 am    Post subject: Reply with quote

Maybe I don't understand what you are saying, but the only purpose of a loop is to repeat something, otherwise there's no need to have it there.

Try:

Code:

{if count($files)  > 1}
    <p>To preview tracks from this album please use the "preview" links below.</p>
{elseif count($files) == 1}
    {assign var=file value=$files|@reset} {*This should get the first and only thing in $files and put it in $file *}
    <a href="{$index_script}?dispatch=orders.get_file&amp;file_id={$file.file_id}&amp;preview=Y" onclick="playTrack('{$index_script}?dispatch=orders.get_file&file_id={$file.file_id}&preview=Y'); return false;">preview</a>
{/if}
Back to top
View user's profile Send private message
1linedesign
Smarty n00b


Joined: 21 Jan 2010
Posts: 3

PostPosted: Thu Jan 21, 2010 1:55 am    Post subject: Reply with quote

Thanks, that seemed to work a treat however this was only a temp solution as i could njot work out how to drag in the main product_id > its related block products ID file to provide the file preview id on the main product page for its related product - it seems to pull in the price, product title and link to its product page ok but grabbing the file id for the related product seemed to be a problem.

if you think you could help with this, i think it would only be a quick solution to do and willing to pay for it - give me an email jamesmurphy[@]1linedesign.com as ideally i need this this done to complete this project and get rid of it!

Thanks again for you help
James
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP