如何在while循环中创建更多列


How to make more columns in a while loop?

我有这样的代码

while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
        echo "
        <div><h4><a href='"page.php?id={$row['subcategory_id']}'">{$row['subcategory']}</a></h4>
        </div>'n";
    }

当我在页面上用子类别画一条竖线的时候。例:

foo
foo
foo
foo
foo
foo
foo

我想把这些子类别限制为10个,每10个之后再从顶部开始,像这样:

foo foo foo
foo foo foo
foo foo foo
foo foo foo
foo foo foo
foo foo foo
foo foo foo

谁能告诉我怎么做?

下面是一个工作示例:https://3v4l.org/MmWJA

下面是一个使用你自己代码的例子:

// get your array in a var
$array = mysqli_fetch_all($r, MYSQLI_ASSOC);
// set column to 10 items tall
$col_height = 10; 
// count the array
$count = count($array);
// determine number of columns based on column height and array size
$row_len = $count / $col_height;
//create iterator
$i = 0;
// loop that shits
foreach($array as $row) {
    // added display = inline 
    echo "<div style='display:inline-block;'><h4><a href='"page.php?id={$row['subcategory_id']}'">{$row['subcategory']}</a></h4></div>'n";
    // echo a line break only when needed, reset the iterator
    if($i == $row_len){
        echo "<br>";
        $i = 0;
    }
}