Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old April 16th, 2005, 05:10 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
help : looping for template

based on this tutor
http://codewalkers.com/tutorials/58/1.html (Writing a Template System in PHP by Timothy Boronczyk)
and iam trying to implement it like this :
php Code:
Original - php Code
  1.  
  2. <?php
  3. require_once("lib/template.php");
  4.  
  5. $page = new Page("template.html");
  6.  
  7. $page->replace_tags(array(
  8.   "tittle_c_sender" => "Sender",
  9.   "title_c_message" => "Message",
  10.   ));
  11.  
  12. $query=('SELECT * FROM guestbook ORDER BY ID DESC LIMIT=10');
  13. $result=mysql_query($query);
  14. while ($row=mysql_fetch_array($result)){
  15.   $page->replace_tags(array(
  16.   "sender_name" => $row['sender_name'],
  17.   "sender_mail" => $row['sender_email'],
  18.   "message" => $row['message'],
  19.   ));
  20. }
  21. $page->output();
  22. ?>


but when i try to acces the page i am only see one record (from table guestbook) and the other is gone
pls any body help me to show all my guestbook content to my template

sorry for my bad english
thanks,



Reply With Quote
  #2  
Old April 19th, 2005, 02:38 AM
bluephoenix's Avatar
bluephoenix bluephoenix is offline
Levelheaded Curmudgeon
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Syracuse, NY
Posts: 507 bluephoenix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 13 m 2 sec
Reputation Power: 2
Send a message via AIM to bluephoenix
RE: help : looping for template

The problem is with the loop. The first iteration of the loop relaces the place-holder tags. The second time around the tags have already been replaced, so there will be nothing left to match.

-Tim

Reply With Quote
  #3  
Old April 20th, 2005, 01:09 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: help : looping for template

after iam try to doing the looping my code is like this :
php Code:
Original - php Code
  1.  
  2. <?php
  3. require_once("lib/template.php");
  4.  
  5. $page = new Page("template.html");
  6.  
  7. $page->replace_tags(array(
  8.   "tittle_c_sender" => "Sender",
  9.   "title_c_message" => "Message",
  10.   ));
  11.  
  12. $query=('SELECT * FROM guestbook ORDER BY ID DESC LIMIT=10');
  13. $result=mysql_query($query);
  14. $name_sender=array();
  15. $email_sender=array();
  16. $message=array();
  17. $total=array();
  18. while ($row=mysql_fetch_array($result)){
  19. $name_sender[]=$row['sender_name'];
  20. $email_sender[]=$row['sender_mail'];
  21. $message[]=$row['message']
  22. $total[]=$row['g_id'];
  23.   }
  24. $total_id=count($total);
  25. for($i = 0; $i < $total_id; $i++){
  26. page->replace_tags(array(
  27. "sender_name"   =>  $name_sender,
  28. "sender_mail"   =>  $email_sender,
  29. "message"       =>  $message,
  30. ));
  31. }
  32. $page->output();
  33. ?>


but iam still confused how to handle the replacement tag with variabel that content array

Reply With Quote
  #4  
Old April 26th, 2005, 02:37 AM
bluephoenix's Avatar
bluephoenix bluephoenix is offline
Levelheaded Curmudgeon
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Syracuse, NY
Posts: 507 bluephoenix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 13 m 2 sec
Reputation Power: 2
Send a message via AIM to bluephoenix
RE: help : looping for template

The class doesn't support looping like this. The place holder tags are replaced after the first run of the loop. There is nothing to replace the 2nd, 3rd, etc. time the loop runs.

-Tim

Reply With Quote
  #5  
Old April 26th, 2005, 12:01 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: help : looping for template

thanks,

Reply With Quote
  #6  
Old May 25th, 2005, 10:42 AM
BlackSkad BlackSkad is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 13 BlackSkad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: help : looping for template

I have added two methods so this class can handle with blocks/iterations from mysql-queries. But I'm not sure this is the fastest way for replacing the data.
a block would look like this:
php Code:
Original - php Code
  1. <!-- START block -->
  2. I need some repeated data here: {data}
  3. <!-- END block -->

All the code:
php Code:
Original - php Code
  1. class Page{
  2.   var $page;
  3.  
  4.   function Page($template = "templates/error.tpl") {
  5.     if (file_exists($template)){
  6.       $this->page = join("", file($template));
  7.       $this->page = str_replace(".", ".", $this->page);
  8.     }else{
  9.       die("De templatefile $template was niet gevonden.");
  10.     }
  11.   }
  12.  
  13.   function parse($file) {
  14.     ob_start();
  15.     include($file);
  16.     $buffer = ob_get_contents();
  17.     $buffer = str_replace(".", ".", $buffer);
  18.     ob_end_clean();
  19.     return $buffer;
  20.   }
  21.  
  22.   function replace_tags($tags = array()) {
  23.     if (sizeof($tags) > 0){
  24.       foreach ($tags as $tag => $data) {
  25.         $data = (file_exists($data)) ? $this->parse($data) : $data;
  26.     $data = ($tag == "bericht") ? $this->smily($data) : $data;
  27.         $this->page = str_replace("{" . $tag . "}", $data, $this->page);
  28.         }
  29.     } else {
  30.       die("Geen tags om te vervangen");
  31.     }
  32.   }
  33.  
  34.   function block($block){
  35.     preg_match ('#<!-- START '. $block . ' -->([^.]+)<!-- END '. $block . ' -->#',$this->page,$this->return);
  36.     $code = str_replace ('<!-- START '. $block . ' -->', "", $this->return[0]);
  37.     $code = str_replace ('<!-- END ' . $block . ' -->', "", $code);
  38.     return $code;
  39.   }
  40.  
  41.   function replace_block_tags($blockname,$query,$count){
  42.     $count = ($count == "") ? $count = mysql_num_rows($query) : $count;
  43.     $this->blockcode = $this->block($blockname);
  44.     if ($count > 0){
  45.         for ($i = 1; $i <= $count; $i++){
  46.             $tags = mysql_fetch_array($query);
  47.             foreach ($tags as $tag => $data) {
  48.                 $data = (file_exists($data)) ? $this->parse($data) : $data;
  49.                 $this->code = str_replace("{" . $tag . "}", $data, $this->blockcode);
  50.           }
  51.             $this->block_page .= $this->blockcode ."n";
  52.       }
  53.     } else {
  54.         $this->block_page = "No data to replace";
  55.     }
  56.     $this->page = str_replace($this->return[0], $this->block_page, $this->page);
  57.   }
  58.  
  59.   function output() {
  60.     $this->page = str_replace(".", ".", $this->page);
  61.     echo $this->page;
  62.   }
  63. }


Can anyone examine the code & give me some advise if there are faster solutions?
Thanx, BlackSkad

Reply With Quote
  #7  
Old June 9th, 2005, 01:45 PM
clericvash clericvash is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: plymouth, devon, uk
Posts: 14 clericvash User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to clericvash Send a message via AIM to clericvash Send a message via Yahoo to clericvash
RE: help : looping for template

ive tried using this block code above but i cant figure it?

im doing this;

$tp->block('choices');
$tags = array('choices' => $choices);
$tp->replace_block_tags('choices',"SELECT * FROM `poll_choices` WHERE `poll_id` = '" . $poll_info['id'] . "'",$count);

Am i doing it wrong?

Reply With Quote