Can not use while loop

Hello,

I want to use while loop in my view file. But it’s not working. I know foreach is working. But I need while loop. Any idea?

Here is my view file

<?php while ($models as $n=>$model){ ?>

&lt;tr class=&quot;&lt;?php echo &#036;n%2?'even':'odd';?&gt;&quot;&gt;


 &lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;name); ?&gt;&lt;/td&gt;


&lt;/tr&gt;  

<?php } ?>

That’s not how you use a while loop. The parameter of a while loop must evaluate to true or false so essentially it’ll be saying while(true): or while(false):… What is the problem with using a foreach loop here?

Here is my actual code. If use foreach then I see the total number of $test records in all rows(all same number 101010). But it should be this way

a count(*)

6 9

19 9500

20 9000

21 9000

22 5000

23 9500

24 9000

25 9000

26 5000

45 60000

<?php foreach($models as $n=>$model): ?>

&lt;tr class=&quot;&lt;?php echo &#036;n%2?'even':'odd';?&gt;&quot;&gt;


&lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;id); ?&gt;&lt;/td&gt;





 &lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;name); ?&gt;&lt;/td&gt;


	


&lt;?php


&#036;tw2 = Yii::app()-&gt;user-&gt;id;


&#036;test = Yii::app()-&gt;db-&gt;createCommand(&quot;SELECT COUNT(a_id) FROM test WHERE a_id &#33;= '' AND User_id = '&#036;tw2' AND b_id = '1'&quot;)-&gt;queryScalar();


?&gt;





&lt;td&gt;&lt;?php echo CHtml::link(&#036;test, array('show','id'=&gt;&#036;model-&gt;id), array('target'=&gt;'_blank')) ; ?&gt;&lt;/td&gt;


&lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;note); ?&gt;&lt;/td&gt;


&lt;/tr&gt;  

<?php endforeach; ?>

I’m not sure i understood what you said but if you perform this action inside a loop


<?php

$tw2 = Yii::app()->user->id;

$test = Yii::app()->db->createCommand("SELECT COUNT(a_id) FROM test WHERE a_id != '' AND User_id = '$tw2' AND b_id = '1'")->queryScalar();

?>


<td><?php echo CHtml::link($test, array('show','id'=>$model->id), array('target'=>'_blank')) ; ?></td>

<td><?php echo CHtml::encode($model->note); ?></td>

</tr>

you will get the same result on each iteration providing that the value of $tw2 doesn’t change.

It was my mistake. It’s working now.

Thanks