Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Przekazanie tablicy do metody wewnatrz klasy
Forum PHP.pl > Forum > Przedszkole
Lolek13
Witam, ucze sie programowania obiektowego i mam problem z przekazaniem tablicy. Postanowilem napisac prosty typer sportowy:

  1. class zaklad {
  2.  
  3. public $db;
  4.  
  5. function __construct() {
  6. $class = "baza";
  7. $path = "{$class}.php";
  8. if (!file_exists($path)) {
  9. throw new Exception("Brak pliku {$path}");
  10. }
  11. require_once($path);
  12.  
  13. if (!class_exists($class)) {
  14. throw new Exception("Brak klasy {$class}");
  15. }
  16. $this->db = new baza();
  17. }
  18.  
  19. function getAllBets() {
  20. if ($this->db->connect()) {
  21. if ($results = $this->db->select("SELECT * FROM typer_main")) {
  22. $returned = array();
  23. while ($row = mysql_fetch_array($results)) {
  24. $returned[] = array('zid' => $row['ZID'],
  25. 'text' => $row['Text'],
  26. 'res_x' => $row['Res_x'],
  27. 'res_y' => $row['Res_y'],
  28. 'kategoria' => $row['Kategoria'],
  29. 'data' => $row['Data'],
  30. 'status' => $row['Status']
  31. );
  32. }
  33. return $returned;
  34. } else {
  35. echo "Błąd pobrania danych " . $this->db->error;
  36. return false;
  37. }
  38. $this->db->close();
  39. } else {
  40. echo "błąd połączenia z bazą danych: " . $this->db->error;
  41. return false;
  42. }
  43. }
  44.  
  45. function showAllBets() {
  46. print_r($returned);
  47. }
  48. }


jezeli wyswietlam tablice returned z metody getAllBets wszystko jest ok. Chcialbym jednak przekazac ją do metody show w celu formatowania jej wygladu. Jak mozna to zrobic ? Kod z pliku index:

  1. $class = "zaklad";
  2. $path = "{$class}.php";
  3. if (file_exists($path)) {
  4. require_once($path);
  5. } else {
  6. throw new Exception("Brak pliku {$path}");
  7. }
  8. if (class_exists($class)) {
  9. $bet = new zaklad();
  10. $bet->showAllBets();
  11. }


Pozdrawiam
nospor
1) Poczytaj o właściwościach klasy oraz $this
2) ALbo przekaż normalnie jak do każdej funkcji - jako parametr funkcji.
Lolek13
1. Zrobilem to w ten sposob:

  1. class zaklad {
  2.  
  3. public $db;
  4. public $returned = array();
  5.  
  6. function __construct() {
  7. $class = "baza";
  8. $path = "{$class}.php";
  9. if (!file_exists($path)) {
  10. throw new Exception("Brak pliku {$path}");
  11. }
  12. require_once($path);
  13.  
  14. if (!class_exists($class)) {
  15. throw new Exception("Brak klasy {$class}");
  16. }
  17. $this->db = new baza();
  18. }
  19.  
  20. function getAllBets() {
  21. if ($this->db->connect()) {
  22. if ($results = $this->db->select("SELECT * FROM typer_main")) {
  23. //$returned = array();
  24. while ($row = mysql_fetch_array($results)) {
  25. $this->returned[] = array('zid' => $row['ZID'],
  26. 'text' => $row['Text'],
  27. 'res_x' => $row['Res_x'],
  28. 'res_y' => $row['Res_y'],
  29. 'kategoria' => $row['Kategoria'],
  30. 'data' => $row['Data'],
  31. 'status' => $row['Status']
  32. );
  33. }
  34. print_r($this->returned);
  35. } else {
  36. echo "Błąd pobrania danych " . $this->db->error;
  37. return false;
  38. }
  39. $this->db->close();
  40. } else {
  41. echo "błąd połączenia z bazą danych: " . $this->db->error;
  42. return false;
  43. }
  44. }
  45.  
  46. function showAllBets() {
  47. print_r($this->returned);
  48. }
  49.  
  50. }


i dziala poprawnie.

Dzieki za podpowiedz : )
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.