Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pierwszy system szablonów - błąd związany z foreach()
Michael2318
post
Post #1





Grupa: Zarejestrowani
Postów: 651
Pomógł: 116
Dołączył: 3.06.2012
Skąd: Lędziny

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


Mam takie coś:

  1. <?php
  2. class template
  3. {
  4. public $dir = "templates/default/"; // katalog z szablonami
  5. public $file; // plik szablonu
  6. public $tags; // tagi
  7.  
  8. public function __construct($file, $tags = NULL)
  9. {
  10. $this->file = $file; // ustala plik
  11. foreach($tags as $tag => $tagvalue)
  12. {
  13. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  14. }
  15. }// end of construct
  16.  
  17. public function tag($tags, $value = NULL) // dodaje nowe tagi
  18. {
  19. if(!isset($value))
  20. {
  21. foreach($tags as $tag => $tagvalue)
  22. {
  23. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  24. }
  25. }
  26. else
  27. {
  28. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  29. }
  30. }// end of tag
  31.  
  32. public function display() // wyświetla szablon
  33. {
  34. $tmp = file_get_contents($this->dir.$this->file); // pobira zawartość
  35. $tmp = str_replace(array_keys($this->tags), array_values($this->tags), $tmp); // zamienia tagi na wartości
  36. $tmp = preg_replace("#\(\.(.*?)\.\)#", "", $tmp); // komentaarze
  37. echo $tmp; // wyświetla obrobiony wynik
  38. }// end of display
  39. }// end of template
  40. ?>


A następnie tak się do tego odwołuję:

  1. $error_type = "Bład";
  2. $szablon = new template("msg_error.tpl");
  3. $szablon->tag(array(
  4. "U_TYPE_ERROR" => $error_type,
  5. "U_MSG_ERROR" => $msg_error,
  6. "U_FILE" => $file,
  7. "U_LINE" => $line)
  8. );


Efekt:

Invalid argument supplied for foreach() in /home/majkelo/public_html/strona/includes/template.class.php on line 11

Przy czym, linia 11 to:

  1. foreach($tags as $tag => $tagvalue)


Przecież jest jak byk tablica tutaj:

  1. $szablon->tag(array(
  2. "U_TYPE_ERROR" => $error_type,
  3. "U_MSG_ERROR" => $msg_error,
  4. "U_FILE" => $file,
  5. "U_LINE" => $line)


więc czemu strzela takie 'fochy'?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
Damonsson
post
Post #2





Grupa: Zarejestrowani
Postów: 2 355
Pomógł: 533
Dołączył: 15.01.2010
Skąd: Bydgoszcz

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


  1. <?php
  2.  
  3. class template
  4.  
  5. {
  6.  
  7. public $dir = "templates/default/"; // katalog z szablonami
  8.  
  9. public $file; // plik szablonu
  10.  
  11. public $tags; // tagi
  12.  
  13.  
  14.  
  15. public function __construct($file, $tags = NULL)
  16.  
  17. {
  18.  
  19. $this->file = $file; // ustala plik
  20.  
  21. print_r($tags); die;
  22. foreach($tags as $tag => $tagvalue)
  23.  
  24. {
  25.  
  26. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  27.  
  28. }
  29.  
  30. }// end of construct
  31.  
  32.  
  33.  
  34. public function tag($tags, $value = NULL) // dodaje nowe tagi
  35.  
  36. {
  37.  
  38. if(!isset($value))
  39.  
  40. {
  41. print_r($tags); die;
  42. foreach($tags as $tag => $tagvalue)
  43.  
  44. {
  45.  
  46. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  47.  
  48. }
  49.  
  50. }
  51.  
  52. else
  53.  
  54. {
  55.  
  56. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  57.  
  58. }
  59.  
  60. }// end of tag
  61.  
  62.  
  63.  
  64. public function display() // wyświetla szablon
  65.  
  66. {
  67.  
  68. $tmp = file_get_contents($this->dir.$this->file); // pobira zawartość
  69.  
  70. $tmp = str_replace(array_keys($this->tags), array_values($this->tags), $tmp); // zamienia tagi na wartości
  71.  
  72. $tmp = preg_replace("#\(\.(.*?)\.\)#", "", $tmp); // komentaarze
  73.  
  74. echo $tmp; // wyświetla obrobiony wynik
  75.  
  76. }// end of display
  77.  
  78. }// end of template
  79.  
  80. ?>


Co pokazało?

Ten post edytował Damonsson 16.12.2012, 15:09:37
Go to the top of the page
+Quote Post
Michael2318
post
Post #3





Grupa: Zarejestrowani
Postów: 651
Pomógł: 116
Dołączył: 3.06.2012
Skąd: Lędziny

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


Warning: Invalid argument supplied for foreach() in /home/majkelo/public_html/strona/includes/template.class.php on line 21
Array ( [U_TYPE_ERROR] => Bład [U_MSG_ERROR] => Table 'majkelo_home.srata_users' doesn't exist [U_FILE] => /home/majkelo/public_html/strona/common.php [U_LINE] => 11 )

Żeby nie było to chcę sobie zrobić taki fajny komunikat o błędach SQL i celowo zapytanie SQL zrobiłem błędnie i jak widać, cała treść ładnie w tablicy siedzi, ale już pod widok się nie chce podpiąc przez ten błąd ;/
Go to the top of the page
+Quote Post
Damonsson
post
Post #4





Grupa: Zarejestrowani
Postów: 2 355
Pomógł: 533
Dołączył: 15.01.2010
Skąd: Bydgoszcz

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


Przed 21 linię wstaw print_r($tags); die;
Go to the top of the page
+Quote Post
Michael2318
post
Post #5





Grupa: Zarejestrowani
Postów: 651
Pomógł: 116
Dołączył: 3.06.2012
Skąd: Lędziny

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


Rozwiązanie:

  1. $szablon = new template("msg_error.tpl", array());


Do zamknięcia.
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 22.08.2025 - 17:20