...

View Full Version : Arranging array content into columns



hmvrulz
09-05-2009, 12:07 PM
This is a very basic question i guess. i am not able to figure it out.
I am calling from a db a set of content which will be in the array

Array
(
[1] => Harsha M V
[2] => Azhar Ibrahim
[3] => Avinash G
[5] => Zishan Sayeed
[4] => Vinay Thomas
[6] => Zishan Aayeed
[7] => Harsha G C
[8] => Santosh S Kumar
[9] => Vachana M V
[10] => ICs
[11] => Car
[12] => Bus
[13] => Slate
[14] => Candy
[15] => Jamica
[16] => Bike
[18] => Kick u
[17] => PC
)

I wanna wanna make a function where on sending the Array and no of columns i should be able to print the content is such a way that

The above array has 18 elements. so by dividing it by 4 i get 4.5.
and the remainder is 2.
First two columns need to have 5elemenets each. Then next two should have 4 elements each.

I want the structure of the elements to be like this

<div>
<span> column 1 </span>
<span> column 2 </span>
<span> column 3 </span>
<span> column 4 </span>
</div>

i will use css to align it side by.




function makeCol($array, $colNo, $field, $div, $span)
{
asort($array);
echo $noEntries = sizeof($array);
echo "<br/>";
echo $floorValue = floor($noEntries / $colNo);
echo "<br/>";
echo $remainder = $noEntries % $colNo;
$count = 1;
echo "<br/>";

foreach ($array as $id => $label) {

// echo "$id => $label<br/>";
//echo $floorValue;
// if ($remainder != 0) {
if ($colNo > 0) {
echo "Col number : $colNo ";
if ($floorValue > 0) {

echo "$id => $label = $floorValue<br/>";

}
$floorValue--;
// }
// $remainder--;
}
$colNo--;
}

echo "<br/>";
/*
echo "<div class=\"$div\">";
foreach ($array as $id => $label) {
if ($count <= $colNo) {
echo "<span class=\"$span\"><input type=\"checkbox\" name=\"data[$field][$field][]\" value=\"$id\" id=\"$field$field$id\" />$label</span><br/>";
} else {
$count = 1;
echo "</div><div class=\"$div\"><span class=\"$span\"><input type=\"checkbox\" name=\"data[$field][$field][]\" value=\"$id\" id=\"$field$field$id\" />$label</span><br/>";
}
$count++;
}
echo "</div>";
*/
}

this is what i have been trying. i am getting confused with the logic of using the foreach :confused:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum