0

I Found this post PHP: Limit foreach() statement? It talks about doing what I want to do but i'm unsure how to edit my code and or where to place it. Can anyone show me where to put the code / How to edit it to make it show only X Number of the array?

{~* Get Ads *~}
{~$ads2=array()~}
{~foreach $ads as $data~}
{~capture append="ads2"~}
{~if $data.code~}
<div>{~$data.code|decode~}</div><br />
{~else~}
<div><a rel="nofollow" href="{~$conf.path~}side/sideClick.php?spid={~$data.id~}" target="_blank">{~if $data.url~}<img src="{~$data.url~}" border="0" alt="{~$data.title~}" {~if $config->get('sideForce')~} width="120" height="60"{~/if~}>{~else~}{~$data.title~}{~/if~}</a></div>
{~/if~}
{~/capture~}
{~/foreach~}

{~* Advertise Here *~}
{~if !$config->get('sideNoFill') || $config->get('sideNoFill')=='2'~}

{~* Advertising Spots *~}
{~if $adsRows>=5 || $config->get('sideNoFill')=='2'~}
{~$rows=1~}
{~else~}
{~$rows=5-$adsRows~}
{~/if~}

{~* Advertising Text *~}
{~if $config->get('sideAds')=='image'~}
{~$adText="banner"~}
{~elseif $config->get('sideAds')=='text'~}
{~$adText="text link"~}
{~else~}
{~$adText="banner or text link"~}
{~/if~}

{~* Loop *~}
{~section loop=$rows name="sidePanel"~}
{~capture append="ads2"~}
<div><a href="{~$conf.path~}side/index.php">Your 120x60 {~$adText~} here, <br>As Low As 8 Cents Per Day!</a></div>
{~/capture~}
{~/section~}
{~/if~}

{~* Horizontal Ads *~}
{~if $horizontal>0~}
{~if count($ads2)~}
<table cellpadding="4" cellspacing="0" style="border:0px;width:100%;table-layout:fixed">
{~$i=1~}
{~foreach $ads2 as $data~}
  {~if $i==1~}
<tr valign="top">
  {~/if~}
<td>{~$data~}</td>
  {~if $i==$horizontal~}
    {~$i=1~}
</tr>
  {~elseif $data@last~}
<td colspan="{~$horizontal-$i~}"></td>
</tr>
  {~else~}
    {~$i=$i+1~}
  {~/if~}
{~if (++$i == 5) break;~}{~/if~}
{~/foreach~}
</table>
{~/if~}

{~* Vertical Ads *~}
{~else~}
{~foreach $ads2 as $data~}
<div>{~$data~}</div>
{~if !$data@last~}<br />{~/if~}
{~/foreach~}
{~/if~}

I Tried messing around with it to find where to put it but i just keep getting errors.

I am using the Vertical Ads Ver. (There shown one on top of each other.)

I want to Limit the Number of ads being show, as it is now it shows all of them, all of the time, each time coming up in a Random order. I want it to be the same except only show the First X number of them that are pulled and forget the rest tell next load. Since they are coming in random order they will all be shown, just not all at the same time.

Community
  • 1
  • 1

1 Answers1

0

1) You're editing Smarty template code, not PHP code. That's why the code you linked to didn't work. Although Smarty tries to be very similar to PHP, it's not the same.

2) You can break out of loops with the {break} command.

{foreach $ads2 as $data}
  {if $data@index >= 2}
   {break}   
  {/if}
{/foreach}

3) Please can you submit that code to http://thedailywtf.com/ ?

Danack
  • 24,939
  • 16
  • 90
  • 122
  • Where do I Add that? I Added it on the Here... {~* Vertical Ads *~} {~else~} {~foreach $ads2 as $data~} {~foreach $ads2 as $data~} {~if $data@index >= 2~} {~break~} {~/if~} {~/foreach~}
    {~$data~}
    {~if !$data@last~}
    {~/if~} {~/foreach~} {~/if~} The Same Images shows like 10 or 15 times in a row.
    – David Macdonald Feb 05 '13 at 11:09
  • Shit I Got it! LoL {~* Vertical Ads *~} {~else~} {~foreach $ads2 as $data~} {~if $data@index >= 2~} {~break~} {~/if~}
    {~$data~}
    {~if !$data@last~}
    {~/if~} {~/foreach~} {~/if~}
    – David Macdonald Feb 05 '13 at 11:20
  • What is that URL that you posted for? – David Macdonald Feb 05 '13 at 11:21
  • "What is that URL" "The Daily WTF, also known as "Worse than Failure" during most of 2007, is a humorous blog dedicated to "Curious Perversions in Information Technology". The blog, "offers living examples of code that invites the exclamation 'WTF!?'" and "recounts tales of disastrous development, from project management gone spectacularly bad to inexplicable coding choices."" – Danack Feb 05 '13 at 12:20