Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z Parserem....
keedy
post 12.02.2005, 20:56:33
Post #1





Grupa: Zarejestrowani
Postów: 52
Pomógł: 0
Dołączył: 18.12.2003
Skąd: Gliwice

Ostrzeżenie: (10%)
X----


mam taką funckje:

  1. <?php
  2.  
  3. public function display( $strFile ) {
  4. $resFile = fopen( $this->strTemplateDir . '/' . $strFile, 'r' );
  5. $strBuff = fread( $resFile, filesize( $this->strTemplateDir . '/' . $strFile ) );
  6. foreach( $this->arrVars as $strVar => $strValue ) {
  7. $strOutput = str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  8. }
  9. echo $strOutput;
  10. }?>


Otóz jak widac zmienna $strOutput jest za kazdą petlą foreach jest nadpisywana, jak rozwiązac ten problem, aby zmienna nei byla nadpisywana?

Ten post edytował keedy 12.02.2005, 21:01:28


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
czachor
post 12.02.2005, 20:58:43
Post #2





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

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


Zrobić z tego tablicę...?


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
keedy
post 12.02.2005, 21:07:09
Post #3





Grupa: Zarejestrowani
Postów: 52
Pomógł: 0
Dołączył: 18.12.2003
Skąd: Gliwice

Ostrzeżenie: (10%)
X----


Moze dla ułatwienia podam cały kod:
  1. <?php
  2.  
  3. class Template {
  4.  
  5. public $strTemplateDir = './templates/2';
  6. public $strBeginTag = '{';
  7. public $strEndTag = '}';
  8. public $arrVars = array();
  9.  
  10. private function __contruct() {}
  11.  
  12. public function assign( $strVar, $strValue ) {
  13. $this->arrVars[$strVar] = $strValue;
  14. }
  15.  
  16. public function display( $strFile ) {
  17. $resFile = fopen( $this->strTemplateDir . '/' . $strFile, 'r' );
  18. $strBuff = fread( $resFile, filesize( $this->strTemplateDir . '/' . $strFile ) );
  19. foreach( $this->arrVars as $strVar => $strValue ) {
  20. $strOutput = str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  21. }
  22. echo $strOutput;
  23. }
  24. }
  25.  
  26. $tpl = new Template;
  27. $tpl->assign( 'Time', 'test' );
  28. $tpl->assign( 'Dwa', 'test-2' );
  29. $tpl->assign( 'Dwaq', 'test-2' );
  30. echo $tpl->display( 'index.tpl' );
  31. ?>


--------------------
Go to the top of the page
+Quote Post
SmokU
post 12.02.2005, 21:12:31
Post #4





Grupa: Zarejestrowani
Postów: 12
Pomógł: 0
Dołączył: 13.02.2004
Skąd: Katowice

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


  1. <?php
  2.  
  3. foreach( $this->arrVars as $strVar => $strValue ) {
  4. $strOutput = str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  5. }
  6. echo $strOutput;
  7.  
  8. ?>


zamień na

  1. <?php
  2.  
  3. foreach( $this->arrVars as $strVar => $strValue ) {
  4. $strOutput .= str_replace( $this->strBeginTag . '$' . $strVar . $this->strEndTag, $strValue, $strBuff );
  5. }
  6. echo $strOutput;
  7.  
  8. ?>


Zmiany dokonane:

$strOutput = zamienione na $strOutput .= . Tworzy to tablicę i dane są dopisywane do zmiennej a nie jak z przypadku bez kropki nadpisywane nowe na stare...


--------------------
Webing.pl... Destrukcja.org... OptykPlus.pl...
Go to the top of the page
+Quote Post
keedy
post 12.02.2005, 21:14:09
Post #5





Grupa: Zarejestrowani
Postów: 52
Pomógł: 0
Dołączył: 18.12.2003
Skąd: Gliwice

Ostrzeżenie: (10%)
X----


tak tylko wtedy caly tpl jest wyswietlany tyle razy ile zadeklarowano "zmiennych" w tym wypadku 3...


--------------------
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: 24.07.2025 - 15:34