kabar916 Posted September 24, 2012 Share Posted September 24, 2012 Whats going on you guys. i have a question. How can i populate a page with data with only three columns and infinite amount of rows. I have a image attached for and example. the data is coming from mysql database on my c drive. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 24, 2012 Share Posted September 24, 2012 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". Quote Link to comment Share on other sites More sharing options...
kabar916 Posted September 24, 2012 Author Share Posted September 24, 2012 Do you think you can show me an example of what the coding may look like. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2012 Share Posted September 24, 2012 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>"; Quote Link to comment Share on other sites More sharing options...
kabar916 Posted September 25, 2012 Author Share Posted September 25, 2012 Thanks alot. that helped a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.