Last week one of my friend called me up as he got stuck when he has to print data table vertically. Yes vertical table, it means that you want your data to print from top to bottom and not left to write, which is standard conventional way to generate html table.
I took it as a challenge and wrapped that up in few minutes. But at that time I was so close to the problem that I could not see that PHP has some inbuilt functions which can help me do that and I did it other way around without using inbuilt PHP function. Here is the sample code for the same, which prints table vertically with the help of inbuilt PHP function array_chunk.
Array is the most powerful feature of PHP and playing with array is always been fun.
// Function to print array data vertically function print_vertical_table_sys($data_list, $rows) { $columns = (count($data_list)/$rows); $data_list = array_chunk($data_list, $rows); echo ' <table border="1" style="border-collapse:collapse">'; for($row_index = 0; $row_index < $rows; $row_index++) { echo ' <tr>' . "\r\n"; for($column_index = 0; $column_index < $columns; $column_index++) { echo ' <td>' . $data_list[$column_index][$row_index] . '' . "\r\n"; } $column_index = 0; echo '' . "\r\n"; } echo '</table> '; } // Function Usage $list = array(); for($i = 0; $i <= 59; $i++) { $list[] = $i+1; } //print_vertical_table_sys ($list, 10);
Now my question to you is how would you do that if you come across such requirement? Assume that you have list of array items starting from 1, 2, 3, ..... N and you want to print it with fixed length rows which starts like 1 and next cell will have 11 or something similar (similar to what you see in above image).
It's not that difficult but I would suggest give it a try and you will enjoy. Doesn't matter what technology you work with as the example which I will show you not will not use any functionality specific to PHP. Basically above code snippet is very close to that but I would suggest that before reading further more you give it a try. Come out of your regular work and make this fun exercise. Read the rest of this entry »

