<分区>
以下元素来自数据库表。
<div id="some_content">
<a href="#">Data1</a><br>
<a href="#">Data2</a><br>
<a href="#">Data3</a><br>
<a href="#">Data4</a><br>
<a href="#">Data5</a><br>
</div>
可能存在超过 50 个元素的情况,如果发生这种情况,我不希望我的页面窗口溢出(和滚动)。
我只想要两列。
它应该自动将行数调整为一列/两列。
每当元素超过 10 个时,我希望网页中有两列。现在可以有 n 个 a
元素。
如果有50个,那么每列自动调整25个,如果有30个,那么每列自动调整15个。
最佳答案
这只是简单的数学运算...做类似的事情:
$number_of_columns = count($items) > 10 ? 2 : 1; // If more then 10, 2 columns
$number_of_items_per_column = floor($items / $number_of_columns); // Floor is needed because the result can be a fraction
while( $item = mysqli_fetch_array( ... ) ) {
if( $i === $number_of_items_per_column ) {
echo '</div><div>'; // close column and open new one
}
echo $item; // yeah not exactly like this, but you get the point
$i++;
}
关于php - 如何自动将一个div分成两部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38039122/