12, May 2008

foreach question - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ 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

Author Topic: foreach question  (Read 897 times)
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« on: Mar 17, 2007, 03: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7822
39867 credits
Members referred : 3



« Reply #1 on: Mar 17, 2007, 03: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
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #2 on: Mar 17, 2007, 03: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Mar 17, 2007, 03: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 : Website layout dimensions today
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7822
39867 credits
Members referred : 3



« Reply #4 on: Mar 17, 2007, 04: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
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #5 on: Mar 17, 2007, 04: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Mar 17, 2007, 04: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 : Website layout dimensions today
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #7 on: Mar 17, 2007, 04:57:22 PM »

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

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

Last blog : Privacy Policy for Blog About Crafts
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7822
39867 credits
Members referred : 3



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

What code you used?

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #9 on: Mar 17, 2007, 05: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7822
39867 credits
Members referred : 3



« Reply #10 on: Mar 17, 2007, 05:18:49 PM »

Try what I posted before (post #2)

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #11 on: Mar 17, 2007, 05: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #12 on: Mar 17, 2007, 05: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7822
39867 credits
Members referred : 3



« Reply #13 on: Mar 17, 2007, 11:35:13 PM »

So what is the problem now?

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Mar 18, 2007, 10: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 : Website layout dimensions today
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #15 on: Mar 18, 2007, 04: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Mar 18, 2007, 04: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 : Website layout dimensions today
I am a fanatic. So?
*****
Gender: Female
Posts: 584
3633 credits
Members referred : 2



« Reply #17 on: Mar 19, 2007, 09: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 Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Privacy Policy for Blog About Crafts
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....

Topic sponsors:
Get a permanent link here for $1.99!


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?
May 12, 2008, 09:34:58 AM





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!


Forum Statistics
Total Posts: 34.924
Total Topics: 7.261
Total Members: 3.476
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: ratatoile

31 Guests, 3 Users online :

10 users online today:



Readers

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