neel
(Kazi Neel)
January 29, 2010, 2:46pm
1
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){ ?>
<tr class="<?php echo $n%2?'even':'odd';?>">
<td><?php echo CHtml::encode($model->name); ?></td>
</tr>
<?php } ?>
jayrulez
(Waprave)
January 29, 2010, 3:00pm
2
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){ ?>
<tr class="<?php echo $n%2?'even':'odd';?>">
<td><?php echo CHtml::encode($model->name); ?></td>
</tr>
<?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?
neel
(Kazi Neel)
January 29, 2010, 3:09pm
3
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): ?>
<tr class="<?php echo $n%2?'even':'odd';?>">
<td><?php echo CHtml::encode($model->id); ?></td>
<td><?php echo CHtml::encode($model->name); ?></td>
<?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>
<?php endforeach; ?>
jayrulez
(Waprave)
January 29, 2010, 3:21pm
4
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.
neel
(Kazi Neel)
January 29, 2010, 3:23pm
5
jayrulez:
I’m not sure i get what you are saying 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 unless providing that the value of $tw2 doesn’t change.
It was my mistake. It’s working now.
Thanks