14, February 2012

foreach question - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: foreach question
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: foreach question  (Read 2116 times)
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« on: Mar 17, 2007, 02:35:44 pm »

Hi All,

I think foreach is a php statement?

Anyway, I have a database application (purchased script) for a directory site. In the new upgrade they appear to have removed the functionality that allowed one to limit the number of results for 2 particular pages to a certain number of pages or results. I now have 2 sections of my site with 18 pages - totally unacceptable.

I would like to limit the results to 20 total links - rather than every link in the directory, which is what it is now doing.

With the foreach, could I put some sort of counter and when it reaches 20 bust out of the foreach loop.

I believe I could figure out the counter part, but how do I signal to the foreach to quit? Or do I have to let the foreach run through the entire database and using a counter and an if statement only display the first 20?

I hope that makes sense. Any suggestions on how to do this?

Quote
{foreach from=$links item=link}
            {include file="link-display.tpl"}                  
{/foreach}


www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #1 on: Mar 17, 2007, 02:45:58 pm »

The code you posted looks like code from the smarty template system.

You can replace it with something like this :

Code:
{foreach from=$links item=link}
            {php}$i++;{/php}
            {if $i < 21}
                   {include file="link-display.tpl"}                 
            {/if}
{/foreach}

The {php}$i++;{/php} part could be replaced with smarty's code, but I am not sure how to do that.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #2 on: Mar 17, 2007, 02:57:34 pm »

This loop is already being used, can I use the same thing, but just change the name by replacing links with say links2? Am I correct that it is better to use a different name for a loop in the same page?

Quote
{foreach from=$links item=link name=links}
<br />
{include file="link-display.tpl"}   
<br />
{if $smarty.foreach.links.iteration eq 1 or
         $smarty.foreach.links.iteration eq 5 or
         $smarty.foreach.links.iteration eq 10}
.
.
.
{/if}
{/foreach}

So for what I need, would this work?

Quote
{foreach from=$links item=link name=links2}

{if $smarty.foreach.links2.iteration lt 21}
 {include file="link-display.tpl"}
{/if}
{/foreach}

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Mar 17, 2007, 02:59:22 pm »

Which file do you want to change Michele? and tell me the row number (I have still a copy on local disk)


Last blog : A new Wordpress theme for our blog
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #4 on: Mar 17, 2007, 03:13:06 pm »

BTW if you know where the query is, you could just add a LIMIT 20 in the end, without modifying any template

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #5 on: Mar 17, 2007, 03:19:24 pm »

It's the popular-links.tpl and the new-links.tpl. The if-then I showed is used to display ads between the listings and from the index.tpl; would seem that this issue is similar in that I need to count the number of listings fetched from the database. Part of me is hoping that there's a way to break out of the foreach to save some processing and page loading time.

I can't believe they dropped the admin panel functionality to limit how many links are displayed on these 2 pages. But then again why should I be suprised with anything that crowd does anymore?


BTW if you know where the query is, you could just add a LIMIT 20 in the end, without modifying any template

I'm not sure that would work, because it would limit the listings in the categories to 20 as well. I think they are reusing the same query for everytime links are displayed.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Mar 17, 2007, 03:21:00 pm »

BTW if you know where the query is, you could just add a LIMIT 20 in the end, without modifying any template

I don't think Michele should change the script files because of later updates...


Last blog : A new Wordpress theme for our blog
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #7 on: Mar 17, 2007, 03:57:22 pm »

Hmm, tried the code, didn't change anything. Any other ideas?

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #8 on: Mar 17, 2007, 04:04:17 pm »

What code you used?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #9 on: Mar 17, 2007, 04:15:11 pm »

This code:

Quote
{foreach from=$links item=link name=links2}

{if $smarty.foreach.links2.iteration lt 21}
 {include file="link-display.tpl"}
{/if}
{/foreach}

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #10 on: Mar 17, 2007, 04:18:49 pm »

Try what I posted before (post #2)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #11 on: Mar 17, 2007, 04:29:21 pm »

That didn't change it either. The foreach statement is surrounded by statements that create the navigation menu. I thought it might be coming from that and tried changing that too:

Quote
{navigation aTotal=$total_links aTemplate='popular-links%1.html' aItemsPerPage=$config.num_index_links aLinksPerPage=$config.num_navig_pages aType=3}
         {foreach from=$links item=link}
            {php}$i++;{/php}
            {if $i < 21}
                   {include file="link-display.tpl"}                 
            {/if}
{/foreach}
{navigation aTotal=$total_links aTemplate='popular-links%1.html' aItemsPerPage=$config.num_index_links aLinksPerPage=$config.num_navig_pages aType=3}
      
</div>

I tried changing $total_links to the actual value 20 and to 2 and it didn't change anything which seems odd to me. Seems like that should have done something even if it broke the page, but to do nothing seems really strange.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #12 on: Mar 17, 2007, 04:51:32 pm »

Oh, good grief, I just remembered they changed and no longer use popular-links.tpl and new-links.tpl.  Roll Eyes  Both of those pages are now running off the same template file. Here's the section of that file that controls link display along with my formatting code for the content box:

Quote
{if $listings}
         <div class="links">
         {navigation aTotal=$total_links aTemplate=$view|cat:"-links%1.html" aItemsPerPage=$config.num_index_links aLinksPerPage=$config.num_navig_pages aType=3}
         {foreach from=$listings item=link name=links}

<br />
{include file="link-display.tpl"}   
<br />
{if $smarty.foreach.links.iteration eq 1 or
         $smarty.foreach.links.iteration eq 5 or
         $smarty.foreach.links.iteration eq 10}

<div class="xsnazzy" style="padding-right: 30px;">
<b class="xtop"><b class="xb1"></b><b class="xb2 red_box"></b><b class="xb3 red_box"></b><b class="xb4 red_box"></b></b>
<div class="xboxcontent red_box">
<div style="padding-left: 130px;">
(adsense code here - removed for example)
</div>
</div>
<b class="xbottom"><b class="xb4 red_box"></b><b class="xb3 red_box"></b><b class="xb2 red_box"></b><b class="xb1"></b></b>
</div>

    {/if}

         {/foreach}
      
         </div>

{else}
<div class="box" style="border: none;">
   {$lang.no_links}
</div>   
{/if}


www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #13 on: Mar 17, 2007, 10:35:13 pm »

So what is the problem now?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Mar 18, 2007, 09:29:18 am »

Oh, good grief, I just remembered they changed and no longer use popular-links.tpl and new-links.tpl.  Roll Eyes  Both of those pages are now running off the same template file. Here's the section of that file that controls link display along with my formatting code for the content box:


I asked that before Smiley, what is the name of this file?


Last blog : A new Wordpress theme for our blog
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #15 on: Mar 18, 2007, 03:19:42 pm »

That file is from the 2.1 version - listings.tpl

I'm terribly sorry, I had forgotten they had changed this from the old way of having a template file for each of those pages. Seems more streamlined this way, but has only added more confusion to an already difficult upgrade.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Mar 18, 2007, 03:29:26 pm »

That file is from the 2.1 version - listings.tpl

I'm terribly sorry, I had forgotten they had changed this from the old way of having a template file for each of those pages. Seems more streamlined this way, but has only added more confusion to an already difficult upgrade.

I think your template is not yet upgraded right?

I have this if/foreach in one of their def. templates:
Code:
{if $listings}
<div class="box">
<div class="box-content">
<div class="links">
{navigation aTotal=$total_links aTemplate=$view|cat:"-links%1.html" aItemsPerPage=$config.num_index_links aLinksPerPage=$config.num_navig_pages aType=3}
<table cellspacing="0" cellpadding="0">
{foreach from=$listings item=link name=links}
{include file="link-display.tpl"}
{/foreach}
</table>
</div>
</div>
</div>
{else}
<div class="box" style="border: none;">
{$lang.no_links}
</div>
{/if}

will say it look to me that the number of pages is dynamic right?


Last blog : A new Wordpress theme for our blog
Bill Gates is my home boy
*****
Gender: Female
Posts: 710
4449 credits
Members referred : 2



« Reply #17 on: Mar 19, 2007, 08:31:01 pm »

I should have the latest version's template.

Yes, it is dynamically generating the number of pages depending upon a control panel setting for how many links to display on each page.

Do you think the easiest solution would be to break the navigation menu part to only put links to the first and second pages? It would still generate the other pages, but one would have to know the file name to get to it.

www.yourmessageconsultant.com, providing online content and printed marketing materials.
www.helpforwebbeginners.com, Tutorials and how to's for new  webmasters.
www.CraftyTips.com, a unique Arts & Crafts Directory
www.nocans.com - Pet Food Recipe Site
www.petsiteguides.com - A New Pet Directory

Last blog : Spring Cleaning at Crafty Tips
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6246
Tags : foreach programming databases Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: foreach question
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Feb 14, 2012, 01:24:51 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.