I would like to use a for loop to access 10 objects of the same class.
The reason is that I want to make a table of the data and I don’t find it proper to write by hand all the html markup for each row (object) on the table.
My code is :
<?php
for ($i=1;$i<=10;$i++){
?>
<tr>
<td><? echo $i;?></td><td><?php echo $office1->pc;?></td>
<td><?php echo $office1->pc*$office1->pcPowerPerUnit;?></td>
<td><? echo $office1->printer;?></td>
<td><?php echo $office1->printer*$office1->printerPowerPerUnit;?></td>
<td><? echo $office1->lights;?></td>
<td><?php echo $office1->lights*$office1->lightsPowerPerUnit;?></td>
<td><? echo $office1->aircondition;?></td>
<td><?php echo $office1->aircondition*$office1->airconPowerPerUnit;?></td>
<td><? echo $office1->server;?></td>
<td><?php echo $office1->server*$office1->serverPowerPerUnit;?></td>
</tr>
<?php } ?>
What I thought that could be done is to change the references $office1->pc (for example) to $office[$i]->pc or something like that but that doesn’t seem to work. I also searched for object iteration in the php manual but that wasn’t helpful.
The number of objects is fixed (10) and the properties are already calculated and ready to be echoed out.
You can use a special syntax to refer to your variables like that. Ty this:
<?php echo ${'office'.$i}->pc;?>