It is necessary to visualize the house with apartments. Suppose there is a 3-storey house and three entrances, it has 27 apartments

As a result, get something like this As a result, get something like this

There is an array of data, you need to bring it to the form, as in the picture (approximately)

$ array = ['number' => 1, 'floor' => 3, 'enters' => 3, 'rooms' => 27];

With HTML + CSS no problem. The problem is how to properly assemble an array for output.

Yes, here is the usual mathematics: 27 apartments / 3 entrances We get 9 apartments in the 1st entrance 9 apartments / 3 floors We get 3 apartments on the floor

But how to make the generation in the 1st entrance in the following order 7,8,9 - 4,5,6 - 1,2,3, I can not think of

  • Add sample code. - Ep1demic
  • There is an array in PHP from which you need to build something similar to that in the picture $ array = ['number' => 1, 'floor' => 3, 'enters' => 3, 'rooms' => 27]; - denis.dc
  • And what is the difficulty? Arrange the cycle correctly? - Vorobyev Alexander
  • Yes, I can’t figure out how to divide and invert the data so that exactly three cells are built in the desired algorithm - denis.dc
  • We also can not understand how to share and turn your data. For bulk, show them as they are. - ilyaplot

2 answers 2

PHP is not intended for visualization. For this there is html. To begin with, implement it simply in html. After that, if you have an idea about arrays and loops, generate the dynamic part of the page with PHP

<?php // $array = [ 'number' => 1, 'floor' => 3, 'enters'=> 3, 'rooms' => 27 ]; $array = [ 'number' => 2, 'floor' => 3, 'enters'=> 2, 'rooms' => 24 ]; $arResult = []; $roomsInEnter = ceil($array['rooms'] / $array['enters']); $roomsInFloor = ceil($roomsInEnter / $array['floor'] ); for ($i=1;$i<=$array['rooms'];++$i) { $curEnter = ceil($i / $roomsInEnter); $curFloor = ceil(($i - ($curEnter-1 ) * $roomsInEnter)/ $roomsInFloor) ; $arResult[$array['floor']-$curFloor][] = $i; } for($i=0;$i<$array['floor'];++$i) { foreach($arResult[$i] as $room) { echo $room."\t"; } echo "\n"; } 
  • That's just with the cycles and the question is, do the HTML + CSS no problem. - denis.dc
  • Thanks for the example! Practically what you need. I will study! - denis.dc
  • $ array = ['number' => 2, 'floor' => 3, 'enters' => 2, 'rooms' => 24]; It doesn't work with this anymore - denis.dc
  • What does the number mean? - Vorobyev Alexander
  • @ denis.dc The floor was not correctly identified. Fixed answer code - Vorobyev Alexander

A triple loop is not the best solution, but if it is the output in PHP that causes the question, then this will be an explanation:

 <!-- Π³Π΄Π΅-Ρ‚ΠΎ Π² Π²ΠΈΠ΄Π΅ $floors - массив этаТСй $padiki - массив массивов Π²ΠΈΠ΄Π° { 'label' строка, 'enters' число 'rooms' массив, Π³Π΄Π΅ ΠΊΠ»ΡŽΡ‡ - этаТ, Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ - Π½ΠΎΠΌΠ΅Ρ€ ΠΊΠΎΠΌΠ½Π°Ρ‚Ρ‹ } --> <table> <? foreach($floors as $floor){ ?> <tr> <td> <?=$floor?> </td> <? foreach($padiki as $podezd){ ?> <? foreach($podezd['rooms'][$floor] as $room){ ?> <td> <?=$room?> </td> <? } ?> <? } ?> </tr> <? } ?> <tr class="footer"> <td></td> <? foreach($padiki as $podezd){ ?> <td colspan="<?=podezd['enters']?>"> <?=$podezd['label']?> </td> <? } ?> </tr> </table> 

You can create $floors and $padiki nested loop based on the $padiki data. I suggest the author to do it himself.

  • Oh, I'll correct it now - Goncharov Alexander
  • Thanks, I will try - denis.dc