loop array values

Hi guys

I have retrieved data from the database using this code:

$floor= Floor::model()->findAll(“id = :x”,(array(’:x’ => $_REQUEST[‘id’])));

$floor->no_of_rooms gets the value of the column no_of_rooms which is an integer

How can I then loop the value of the integer?

Kindly help

What do you mean with "loop the value of the integer"?

1 Like

$floor= Floor::model()->findAll(“id = :x”,(array(’:x’ => $_REQUEST[‘id’])));

echo $floor->no_of_rooms; gives the value e.g 5

I want to 5 rows each for one room




$floors = Floor::model()->findAll("id = :x",(array(':x' => $_REQUEST['id'])));

foreach($floors as $floor) {

 echo  $floor->no_of_rooms;

}


this code will print each no_of_rooms



I want to have the same number of forms of different model as the no_of_rooms

E.g if echo $floor->no_of_rooms is 3, it should generate 3 forms

I guess you need something like this:


$floor = Floor::model()->findOne("id = :x",(array(':x' => $_REQUEST['id'])));


for ($i=0; $i < $floor->no_of_rooms; $i++) { 

    # your code...

}



You also should check the guide. http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#selecting-extra-fields. Here it describes how you can create virtual fields which will be produced after some calculations, I think this case matches your question

Yes, thats what I need and I have implemented but its crushing the whole code

Thanks a lot. I appreciate your help

Its now working, I appreciate