从<div>的数组创建网格

时间:2018-10-01 20:39:39

标签: php html arrays

我正在尝试编写一个简单的函数,该函数接受项目数组,然后以嵌套的divs返回该数组。

到目前为止我所拥有的:

// items that need to be nested
$arr_items = array('<div>A</div>','<div>B</div>','<div>C</div>','<div>D</div>','<div>E</div>','<div>F</div>','<div>G</div>','<div>H</div>','<div>I</div>','<div>J</div>','<div>K</div>','<div>L</div>');

// build columns of items
$cols_per_row   = 2;
$cols_last      = $cols_per_row + 1;
$column_group_open  = "\n<div class='columns'>\n";
$column_group_close = "</div>\n";
$c_items = count($arr_items);

if ($c_items>0)
{
    $html = $column_group_open;
    $c=0;
    foreach ($arr_items as $item)
    {
        // add item to current colun
        $html .= $item;
        if ($c==$cols_per_row)
        {
            // end current column 
            $html .= $column_group_close;

            // are there more items left?
            if ($c<$c_items)
            {
                // start another column
                $html .= $column_group_open;
            }
        }

        $c++;
    }
    // close last column
    if ($c<$cols_last) {$html .= $column_group_close;}

}

很明显,有什么问题。当前输出:

<div class='columns'>
<div>A</div><div>B</div><div>C</div></div>

<div class='columns'>
<div>D</div><div>E</div><div>F</div><div>G</div><div>H</div><div>I</div><div>J</div><div>K</div><div>L</div>

所需的输出:

<div class='columns'>
<div>A</div><div>B</div></div>

<div class='columns'>
<div>C</div><div>D</div></div>

<div class='columns'>
<div>E</div><div>F</div></div>

<div class='columns'>
<div>G</div><div>H</div></div>

<div class='columns'>
<div>I</div><div>J</div></div>

<div class='columns'>
<div>L</div></div>

我的示例代码是否?我需要检查或修改才能使它正常工作吗?还是有我可以使用的现有库?

3 个答案:

答案 0 :(得分:2)

您可以使用PHP的一些内置数组操作函数,而无需执行任何循环:

table : order_line_item
+-------+------------+-----------------+    
|   id  |   order_id |   active_orders |     
+-------+------------+-----------------+
|   1   |   1        |        1        |   
|   2   |   2        |        1        |   
|   3   |   3        |        0        |  
|   4   |   4        |        1        |  
|   5   |   4        |        1        |   
+-------+------------+-----------------+

table : orders
+-------+-------------+    
|   id  |   valid     |     
+-------+-------------+
|   1   |       1     |   
|   2   |       1     |   
|   3   |       0     |  
|   4   |       1     |  
|   5   |       1     |   
+-------+-------------+

答案 1 :(得分:1)

每添加%列,就用$c运算符获得$cols_per_row除以$cols_per_row来得到余数。

if ($c > 0 && $c % $cols_per_row == $cols_per_row - 1)

答案 2 :(得分:0)

您的代码可以简化。

请检查以下内容。您可能需要删除PHP_EOL位才能在语法上正确地处理HTML。

// items that need to be nested
$items = ['<div>A</div>','<div>B</div>','<div>C</div>','<div>D</div>','<div>E</div>','<div>F</div>','<div>G</div>','<div>H</div>','<div>I</div>','<div>J</div>','<div>K</div>'];

$item_count = count($items);
$final_html = '';
$temp_container = [];
$columns = 2;

$i = 0;
$j = 1;
$html_1 = "<div class='columns'>";
$html_2 = "</div>";

foreach ($items as $item)
{
    $temp_container[$j] = $item;

    if ($j == $columns OR $i == $item_count-1) {
        $temp_html = '';

        foreach ($temp_container as $temp) {
            $temp_html .= $temp;
        }

        $final_html .= $html_1 . PHP_EOL . $temp_html . $html_2 . PHP_EOL;

        $temp_container = [];
        $j = 0;
    }

    $j++;
    $i++;
}

var_dump($final_html);