Welcome Guest, Not a member yet? Register   Sign In
Increment Variable in View
#1

[eluser]n00bPhpCoder[/eluser]
<?php foreach($articleEntry as $myarticle):?>
<h3><span>&lt;?=$myarticle->title;?&gt;<br/>(&lt;?=$myarticle->author;?&gtWink on
&lt;?=$myarticle->dateposted;?&gt;</span></h3><br/>
&lt;?=auto_link($myarticle->content);?&gt;<br/>
&lt;?php endforeach ?&gt;<br/>

Hi that is a code from my view. I want to increment a counter on every loop can i do that? It says variable is not defined since variables are passed from controller to view right? Is there a way i can do it like this?

&lt;?php foreach($articleEntry as $myarticle):?&gt;
&lt;?php
if($ctr==5){
?&gt;
HTML Here
&lt;?php
}
?&gt;

<h3><span>&lt;?=$myarticle->title;?&gt;<br/>(&lt;?=$myarticle->author;?&gtWink on
&lt;?=$myarticle->dateposted;?&gt;</span></h3><br/>
&lt;?=auto_link($myarticle->content);?&gt;<br/>
&lt;?php
$ctr++;
?&gt;
&lt;?php endforeach ?&gt;<br/>

is this possible?
#2

[eluser]Armchair Samurai[/eluser]
Set up the counter variable outside of the loop and increment it for each pass.

PS You might want to use the [ code ] tag in your posts to make your code more readable.
#3

[eluser]Michael Wales[/eluser]
Code:
$i = 0;
foreach ($articleEntry as $article) {
  $i++;
}
#4

[eluser]MASS MASS[/eluser]
declare $ctr = 0 before forloop

inside forloop .........$ctr++


then check

if($ctr==5)
{
//HTML...............
break ..........for it will come out from forloop

}




Theme © iAndrew 2016 - Forum software by © MyBB