Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Utworzenie pliku zawierajacego tabele
Spooky2
post 15.05.2010, 00:16:40
Post #1





Grupa: Zarejestrowani
Postów: 79
Pomógł: 0
Dołączył: 26.06.2007

Ostrzeżenie: (0%)
-----


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 |
-------------------------------
Go to the top of the page
+Quote Post
croc
post 15.05.2010, 07:26:27
Post #2





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

Ostrzeżenie: (0%)
-----


Czy na pewno potrzebujesz czegoś takiego? Napisz proszę do czego ci to potrzebne.
Go to the top of the page
+Quote Post
Spooky2
post 15.05.2010, 09:18:18
Post #3





Grupa: Zarejestrowani
Postów: 79
Pomógł: 0
Dołączył: 26.06.2007

Ostrzeżenie: (0%)
-----


Niestety takie mam wytyczne... Tak powinien wygladac plik.
Go to the top of the page
+Quote Post
croc
post 15.05.2010, 09:29:25
Post #4





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
Spooky2
post 15.05.2010, 10:13:48
Post #5





Grupa: Zarejestrowani
Postów: 79
Pomógł: 0
Dołączył: 26.06.2007

Ostrzeżenie: (0%)
-----


Wow, jestem pod wrazeniem :-) Kawal dobrej roboty. WIELKIE DZIEKI.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 12.06.2025 - 12:48