PHP Display Arrays in Columns


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function column_display($items,$numcols) {
  2.  
  3. $numitems = count($items);
  4.  
  5. // Number of Rows
  6. $numrows = ceil($numitems/$numcols);
  7.  
  8. $output = '<table>';
  9. for ($row=1; $row <= $numrows; $row++)
  10. {
  11. $cell = 0;
  12. $output .= ' <tr>'."\n";
  13. for ($col=1; $col <= $numcols; $col++)
  14. {
  15. $output .= ' <td>'."\n";
  16.  
  17. if ($col===1)
  18. {
  19. $cell += $row;
  20.  
  21. $output .= $items[$cell - 1]->name;
  22.  
  23. }
  24. else {
  25. $cell += $numrows;
  26. $output .= $items[$cell - 1]->name;
  27. }
  28. $output .= ' </td>'."\n";
  29. }
  30. $output .= ' </tr>'."\n";
  31. }
  32. $output .= '</table>';
  33. return $output;
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.