Hello. There is a markup, like:

<div class="row-4"> тут 4 пункта из базы </div> <div class="row-4"> тут 4 пункта из базы </div> 

There is an indefinite number of points in the database, I need to open the block <div class="row-4"> before every fourth first point, and after 4 points, close </div> .

It is also necessary to take into account that in the last block there may be points less than 4.

I tried this:

 <? $sp = 0; $table_all = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC"); while ($table = mysql_fetch_assoc($table_all)) { $sp++; ?> <? if ($sp == 4) { ?> <div class="row-4"> <? } ?> <div> <input type="checkbox" class="checkbox" id="checkbox-<?=$table['id'];?>" name="<?=$table['id'];?>" /> <label for="checkbox-<?=$table['id'];?>"><?=$table['name'];?></label> </div> <? if ($sp == 4) { ?> </div> <? $sp = 0; } ?> <? } ?> 

But something I confuse apparently. I would be grateful for the help.

  • The easiest way to pre-decompose the results into an array of 4. - vp_arth
  • one
    Daku print the initial <div class="row-4"> and final </div> and inside make a loop of output elements. and when ($idx + 1) % 4 == 0 then output intermediate </div><div class="row-4"> - teran
  • one
    The second part of the @vp_arth response is about this. - teran
  • one
    in general, complete govnokodit, and share logic, data, and representation. - teran
  • one
    By tradition, mysql_* api remind you that mysql_* api outdated and cut from modern php versions. If you can, go to pdo/mysqli - vp_arth

3 answers 3

The simplest thing is to prepare a view of the desired structure while retrieving data from the database.

 <?php $view = array(); $row = array(); while ($item = mysql_fetch_assoc($table_all)) { if (count($row) == 4) { $view[] = $row; $row = array(); } $row[] = $item; } if ($row) { $view[] = $row; } ?> <?foreach($view as $row):?> <!-- Render row --> <?foreach($row as $item):?> <!-- Render item --> <?endforeach?> <?endforeach?> 

If the presence of </tag><tag> in the template does not confuse, you can:

 $index = 0; echo '<div class="row">'; while ($item = mysql_fetch_assoc($table_all)): if (($index+1)%4 == 0) echo '</div><div class="row">'; // render item $index++; endwhile; echo '</div>'; 
  • ++$index % 4 immediately 8) - teran

You select all the data in the array, you break the array into parts using array_chunk

 <?php $table_all = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC"); $data = array(); while ($table = mysql_fetch_assoc($table_all)) { $data[] = $table; } $data = array_chunk($data, 4); ?> <?php foreach ($data as $chunk): ?> <div class="row-4"> <?php foreach ($chunk as $table):?> <div> <label> <input type="checkbox" class="checkbox" name="<?=$table['id'];?>" /> <?=$table['name'];?> </label> <div> <?php endforeach;?> </div> <?php endforeach;?> 

By the way, the <label><input type="checkbox" /> Название чекбокса</label> construction allows you not to set IDs for elements, but to set numeric names for input fields - a bad tone.

  • Similarly, I could not remember) - vp_arth
 $code; $out = array(); $tag_start = '<div class=\'row-4\'>'; $tag_end = '</div>'; while(++$i < 100){ $code .= $i.' ';//Сюда - заполняем пункты нужные, как бы рендерили в хтмл if($i % 4 == 0){ $out[] = $code; $code = ''; } } if($code){//Если мы не обнуляли код в последней итерации(она была меньше 4х) $out[] = $code;//делаем это сейчас } echo $tag_start. implode( $tag_end. $tag_start, $out ). $tag_end; 

Sendbox example

  • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky