我试图在Zurb的Foundation 4中进行一些源代码排序,问题在于我的PHP逻辑。我目前正在开发CMS,开发人员可以做的是编辑列数和列的宽度(跨度),所以假设他创建了3列,下一个跨度= 1,3和8 :
Array
(
[0] => stdClass Object
(
[span] => 1
[position] => 0
)
[1] => stdClass Object
(
[span] => 3
[position] => 1
)
[2] => stdClass Object
(
[span] => 8
[position] => 2
)
)
渲染的标记为:
<div class="column small-1"></div>
<div class="column small-3"></div>
<div class="column small-8"></div>
所以在这种情况下,主要内容将位于较大的列中,因此我所做的是使用PHP对它们进行重新排序,以便它们从大到小排序(以便主要内容首先显示):
Array
(
[0] => stdClass Object
(
[span] => 8
[position] => 2
)
[1] => stdClass Object
(
[span] => 3
[position] => 1
)
[2] => stdClass Object
(
[span] => 1
[position] => 0
)
)
渲染的标记为:
<div class="column small-8"></div>
<div class="column small-3"></div>
<div class="column small-1"></div>
我需要知道的是如何自动添加相应的推/拉类以获得结果:
Array
(
[0] => stdClass Object
(
[span] => 8
[position] => 2
[push] => 4
[pull] => 0
)
[1] => stdClass Object
(
[span] => 3
[position] => 1
[push] => 0
[pull] => 7
)
[2] => stdClass Object
(
[span] => 1
[position] => 0
[push] => 0
[pull] => 11
)
)
结果标记应为:
<div class="column small-8 push-4"></div>
<div class="column small-3 pull-7"></div>
<div class="column small-1 pull-11"></div>
我已经制作了一个能够做到这一点的功能,但它没有给我正确的解决方案,而且我觉得它太复杂了:
/**
* Reorders the columns so that they are exactly like the design's order, because we are ordering the columns from largest to smallest
* hoping that the largest means more important content than the smallest, so we need a way to return to the original order
* @param $columns - The column array
* @param $col - the current column
* @param $key - current column key
* @return array
*/
private function source_ordering($columns, $col, $key)
{
$ordering = array();
$ordering['pull'] = 0;
$ordering['push'] = 0;
$total_spans = 0;
//How many positions to loop back/forth in the array
$position_key = $col->position - $key;
print_r('-------');
//Push column
if ($col->position > $key) {
//Loop forth in the array and add all the column "spans"
$i = 1;
while ($i <= $position_key) {
$total_spans += $columns[$key + $i]->span;
$i++;
}
$ordering['push'] = $col->push = $total_spans;
}
//Position hasn't changed - Pull?
else if ($position_key === 0) {
var_dump($key);
$pos = $key - 1;
while ($pos >= 0) {
print_r($columns[$pos]);
//Is the previous column pushed or pulled?
if ($columns[$pos]->push) {
$ordering['pull'] -= $columns[$pos]->span;
} else {
$ordering['pull'] += $columns[$pos]->span;
}
$ordering['pull'] = abs($ordering['pull']);
$pos--;
}
}
//Pull column
else {
//Make it positive (easier to calculate)
$position_key = abs($position_key);
//Loop forth in the array and add all the column "spans"
$i = 1;
while ($i <= $position_key) {
$total_spans += $columns[$key - $i]->span;
$i++;
}
$ordering['pull'] = $col->pull = $total_spans;
}
return $ordering;
}
在每个列数组上调用此函数:
foreach ($columns as $col_key => $col)
{
$ordering = $this->source_ordering($columns, $col, $col_key);
$col->pull = $ordering['pull'];
$col->push = $ordering['push'];
}
请记住,开发人员可以创建1,2,3或更多列,而不仅仅是我发布的示例。
我怎样才能做到这一点?