After trying out some very complex possible solutions that just didn't work, I figured out the simplest way to reorder my arrays to make them ready for the html page load.
So for example, if you have an array containing this order from say a SQL query:
1 2 3
4 5 6
7
And you want it to display like this:
1 4 6
2 5 7
3
In your php code (because it executes way faster than it would using jquery) you need this:
function reorderVertically($arr_base)
{
//the new array to store your reorganized data
$arr_at = array();
//the total items in the orginal array
$cnt = count($arr_base);
//the amount of rows
$amt = $cnt/3;
//the columns containing an extra row
$rest = $cnt%3;
//the value of the next item to copy. Starts at 0 because the first item is always 0
$next = 0;
//for the amount of items in the old array
for($i = 0; $i < $cnt ; $i++)
{
//copy the item
$arr_at[$i] = $arr_base[$next];
//calculate next value
$inc = ($i % 3 < $rest) ? ($amt+1):($amt);
//set next value
$next = $next + $inc;
//get real next - because if you dont, it goes out of range fast ;)
$next = ($next > ($cnt-1)) ? ($next - ($cnt-1)) : ($next);
}
return arr_at;
}
Now the end result will give you:
0 3 5
1 4 6
2
instead of:
0 1 2
3 4 5
6
No comments:
Post a Comment