Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Algorytm rysowania tekstowych tabelek
Forum PHP.pl > Forum > PHP
kiler129
Witajcie!
Czy ktoś ma pomysł jak rysować takie tabelki:

Kod
+---------------+----------------+------------+-----------------+--------------+--------------+-----------+---------------+
| TABLE_CATALOG | TABLE_SCHEMA   | TABLE_NAME | VIEW_DEFINITION | CHECK_OPTION | IS_UPDATABLE | DEFINER   | SECURITY_TYPE |
+---------------+----------------+------------+-----------------+--------------+--------------+-----------+---------------+
| NULL          | abcdefg        | users_data |                 | NONE         | NO           | abcdefg@% | DEFINER       |
| NULL          | abcdefg_BACKUP | users_data |                 | NONE         | NO           | abcdefg@% | DEFINER       |
+---------------+----------------+------------+-----------------+--------------+--------------+-----------+---------------+

/tu akurat z mysql/

Widzę, że oni używają spacji wink.gif Próbowałem używać \t ale jesli jedno ma 10 znaków a drugie 3 to jest problem wink.gif
Jakieś rady?
#luq
No bo tabulator ma to do siebie, że jest przekształcany na JAKĄŚ przerwę, ile będzie miała to nie jest zdefiniowane.
Używaj spacji i będzie git wink.gif
kiler129
Oto znalazłem, przerobiłem, pogmerałem i usprawniłem wink.gif

  1. <?php
  2.  
  3. /*
  4.   === INPUT ===
  5.   $table[1]['id'] = '1';
  6.   $table[1]['make'] = 'Citroen';
  7.   $table[1]['model'] = 'Saxo';
  8.   $table[1]['version'] = '1.4 West Coast';
  9.  
  10.   $table[2]['id'] = '2';
  11.   $table[2]['make'] = 'Honda';
  12.   $table[2]['model'] = 'Civic';
  13.   $table[2]['version'] = '1.6 VTi';
  14.  
  15.   $table[3]['id'] = '3';
  16.   $table[3]['make'] = 'BMW';
  17.   $table[3]['model'] = '3 Series';
  18.   $table[3]['version'] = '328 Ci';
  19.   */
  20. function drawTextTable($table, $centering=true) {
  21. $headers = array_keys($table[0]);
  22. foreach ($headers as $key => $header) {
  23. $headers[$header] = $header;
  24. unset($headers[$key]);
  25. }
  26.  
  27. $ttable = $table;
  28. $ttable[] = $headers;
  29.  
  30. # Work out max lengths of each cell
  31. foreach ($ttable AS $row) {
  32. $cell_count = 0;
  33. foreach ($row AS $key => $cell) {
  34. $cell_length = strlen($cell);
  35. $cell_count++;
  36. if(!isset($cell_lengths[$key]) || $cell_length > $cell_lengths[$key]) $cell_lengths[$key] = $cell_length;
  37. }
  38. }
  39.  
  40. # Build header bar
  41. $bar = "+";
  42. $header = "|";
  43. $i = 0;
  44. foreach ($cell_lengths AS $fieldname => $length) {
  45. $i++;
  46. $bar .= str_pad('', $length+2, "-")."+";
  47. $name = $fieldname;
  48. if(strlen($name) > $length) {
  49. # crop long headings
  50. $name = substr($name, 0, $length-1);
  51. }
  52.  
  53. $header .= " ".str_pad($name, $length, " ", STR_PAD_BOTH) . " |";
  54. }
  55.  
  56. $output = "";
  57. $output .= $bar."\n";
  58. $output .= $header."\n";
  59. $output .= $bar."\n";
  60.  
  61. # Draw rows
  62. foreach ($table AS $row) {
  63. $output .= "|";
  64. foreach ($row AS $key=>$cell) {
  65. $output .= " ".str_pad($cell, $cell_lengths[$key], " ", ($centering)?STR_PAD_BOTH:STR_PAD_RIGHT)." |";
  66. }
  67.  
  68. $output .= "\n";
  69. }
  70.  
  71. $output .= $bar."\n";
  72. return $output;
  73. }
  74. ?>

Have fun with tables wink.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.