Posted By

albertomori on 04/06/09


Tagged

php list columns


Versions (?)

3 columns list from db (php)


 / Published in: PHP
 

  1. function columnList( $articles, $articles_start_char ) {
  2. // divide list into three equal chunks
  3. $chunk = (int) (count ( $articles ) / 3);
  4.  
  5. // get and display header
  6. $r = '<table width="100%"><tr valign="top">';
  7.  
  8. $prev_start_char = 'none';
  9.  
  10. // loop through the chunks
  11. for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
  12. $chunkIndex < 3;
  13. $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
  14. {
  15. $r .= "<td>\n";
  16. $atColumnTop = true;
  17.  
  18. // output all articles in category
  19. for ($index = $startChunk ;
  20. $index < $endChunk && $index < count($articles);
  21. $index++ )
  22. {
  23. // check for change of starting letter or begining of chunk
  24. if ( ($index == $startChunk) ||
  25. ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
  26.  
  27. {
  28. if( $atColumnTop ) {
  29. $atColumnTop = false;
  30. } else {
  31. $r .= "</ul>\n";
  32. }
  33. $cont_msg = "";
  34. if ( $articles_start_char[$index] == $prev_start_char )
  35. $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
  36. $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
  37. $prev_start_char = $articles_start_char[$index];
  38. }
  39.  
  40. $r .= "<li>{$articles[$index]}</li>";
  41. }
  42. if( !$atColumnTop ) {
  43. $r .= "</ul>\n";
  44. }
  45. $r .= "</td>\n";
  46.  
  47.  
  48. }
  49. $r .= '</tr></table>';
  50. return $r;
  51. }

Report this snippet  

You need to login to post a comment.