Jump to content

Recommended Posts

You'll have to get the number of elements in the array first, then calculate how many items per row it would give. Once that's done you need to loop through the array, up until the max number of items per row. Inside this loop you should add one item to each column, with column 2 having the item at index+num_per_column and column 3 the item at index + 2*num_per_column.

 

PS: This is in the wrong section, unless you're using third party systems in PHP. If you want to get hints on how to design your own code, which this looks like, then you should have posted in "Application design".

Here's a script that does the inverse - given a fixed number of rows down, produce the needed columns across. By changing this to instead set $num_cols to 3 and calculate the $num_rows, you can get it to do what you want -

 

<?php

// retrieve your data into an array in the order that you want it (faked here for demo purposes)
$data[] = 'Academic Advisory';
$data[] = 'Academic Assistance';
$data[] = 'Academic Calendars';
$data[] = 'Academics Office';
$data[] = 'Administration';
$data[] = 'Adult Learners';
$data[] = 'Alumni Chapters';
$data[] = 'Alumni Events';
$data[] = 'Athletics';
$data[] = 'Campus Life At a Glance';
$data[] = 'Campus Recreation';
$data[] = 'Campus Safety & Security';
$data[] = 'Class Schedules';
$data[] = 'Counseling Center';
$data[] = 'Course Descriptions and Catalog';
$data[] = 'Department Directory';
$data[] = 'Departments & Programs';
$data[] = 'Fellowships';
$data[] = 'Finals Schedules';
$data[] = 'Class Schedules';
$data[] = 'Counseling Center';
$data[] = 'Course Descriptions and Catalog';
$data[] = 'Department Directory';
$data[] = 'Departments & Programs';
$data[] = 'Departments & Programs';


$num_rows = 12; // number of rows down

if(count($data) < $num_rows){$num_rows = count($data);} // prevent empty rows if not enough data
$num_cols = ceil(count($data)/$num_rows); // calculate number of columns

echo "<table>";
for($i=0; $i<$num_rows;$i++){
   echo "<tr>";
   for($j=0; $j<$num_cols;$j++){
       $element = $i + $j * $num_rows;
       if(isset($data[$element])){
           echo "<td>$data[$element]</td>"; // cell with data
       } else {
           echo "<td> </td>"; // empty cell
       }
   }
   echo "</tr>\n";
}
echo "</table>";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.