Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Utworzenie pliku zawierajacego tabele
Forum PHP.pl > Forum > Przedszkole
Spooky2
Kurcze, czy sa jakies specjalne klasy czy funkcje, ktore umozliwia zapisanie danych do pliku w postaci tabelki?
Nie wiem jak sie to tego zabrac. Klepiac recznie, tresc sie rozjedza... Chodzi mi o uzyskanie czegos takiego:

-------------------------------
| Pole1 | Pole 2 | Pole 3 |
-------------------------------
| aaaa | bbbbb | cccc |
-------------------------------
croc
Czy na pewno potrzebujesz czegoś takiego? Napisz proszę do czego ci to potrzebne.
Spooky2
Niestety takie mam wytyczne... Tak powinien wygladac plik.
croc
OK, jako że mi się nudzi to zrobiłem coś na szybko.

Plik klasy (TableASCII.php):

  1. <?php
  2.  
  3. class TableASCII {
  4.  
  5. private $data;
  6. private $columnsWidths;
  7.  
  8. public function __construct() {
  9. $this->data = array();
  10. $this->columnsWidths = array();
  11. }
  12.  
  13. public function addRow() {
  14. $column = 0;
  15. $this->data[$this->getRowsNumber()] = array();
  16. foreach(func_get_args() as $element) {
  17. $this->data[$this->getRowsNumber() - 1][$column] = $element;
  18. $length = strlen($element);
  19. if(array_key_exists($column, $this->columnsWidths)) {
  20. if($length > $this->columnsWidths[$column]) {
  21. $this->columnsWidths[$column] = $length;
  22. }
  23. }
  24. else {
  25. $this->columnsWidths[$column] = $length;
  26. }
  27. ++$column;
  28. }
  29. }
  30.  
  31. public function getRowsNumber() {
  32. return count($this->data);
  33. }
  34.  
  35. public function getColumnsNumber() {
  36. return count($this->columnsWidths);
  37. }
  38.  
  39. public function getElement($row, $column) {
  40. return $this->data[$row][$column];
  41. }
  42.  
  43. public function output($fileName = null) {
  44. $horizontalBorder = str_repeat('-', array_sum($this->columnsWidths) + $this->getColumnsNumber() + 1);
  45. $output = $horizontalBorder;
  46. foreach($this->data as $row) {
  47. $output .= "\r\n" . '|';
  48. $currentColumn = 0;
  49. foreach($row as $element) {
  50. $length = strlen($element);
  51. $output .= $element . ($length < $this->getColumnWidth($currentColumn) ? str_repeat(' ', $this->getColumnWidth($currentColumn) - $length) : '') . '|';
  52. ++$currentColumn;
  53. }
  54. }
  55. $output .= "\r\n" . $horizontalBorder;
  56. if(isset($fileName)) {
  57. $file = fopen($fileName, 'w');
  58. fwrite($file, $output);
  59. fclose($file);
  60. }
  61. return $output;
  62. }
  63.  
  64. private function getColumnWidth($index) {
  65. return $this->columnsWidths[$index];
  66. }
  67.  
  68. }
  69.  
  70. ?>


I przykład użycia:
  1. <?php
  2.  
  3. header('Content-type: text/plain');
  4.  
  5. require_once('TableASCII.php');
  6.  
  7. $table = new TableASCII();
  8. $table->addRow('Tata', 'Muminka', 63);
  9. $table->addRow('Mała', 'Mi', 15);
  10. $table->addRow('Buuuuuuuuuuuuuuuuuka', 'Zła', 78423);
  11. $table->addRow('Paszczak', 'Paszczakowski', 75);
  12.  
  13. echo $table->output('muminkowo.txt');
  14.  
  15. ?>


Klasa pozwala wypisywać tabelkę i opcjonalnie zapisywać ją do pliku. Powinna zabezpieczać się przez nieprawidłowymi liczbami kolumn itd., ale tego już charytatywnie nie mogę zrobić smile.gif
Spooky2
Wow, jestem pod wrazeniem :-) Kawal dobrej roboty. WIELKIE DZIEKI.
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.