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
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #16  
Old October 22nd, 2005, 06:03 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

I don't see where it says to actually use the block though?

Surly you need to say something like use_block('song_list') or else it wont appear?

Reply With Quote
  #17  
Old October 22nd, 2005, 09:15 PM
BassFace BassFace is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Denver, Colorado USA
Posts: 9 BassFace 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

It's in there - where it says $page->replace_block_tags(etc....);
php Code:
Original - php Code
  1.  
  2. // query for the songs loop and call to the replace_block_tags function for this block only
  3. $songs_sql = "SELECT * FROM album_info WHERE product_id = '$pid' ORDER BY track_number";
  4. $songs_query = mysql_query($songs_sql);
  5.  
  6. $page->replace_block_tags("songs_block", $songs_query);
  7.  
  8. mysql_free_result($songs_query);
  9.  
  10. // send array of non looping query results to the template for display
  11. $page->replace_tags(array(
  12.     'band_id' => $product['band_id'],
  13.     'band_name' => strtoupper($product['band_name']),
  14.     'product_cat' => strtoupper($product['product_cat']),
  15.     'product_title' => strtoupper($product['product_title']),
  16.     'image_path' => $product['band_name']."/images/",   
  17.     'album_cover' => $product['product_img'],
  18.     'band_photo' => $product['band_photo'] ,
  19.     'footer' => 'footer.php'
  20.     ));
  21.  
  22. // output the results = send to browser!
  23. $page->output();
  24. ?>

Reply With Quote
  #18  
Old November 3rd, 2005, 01:01 AM
BassFace BassFace is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Denver, Colorado USA
Posts: 9 BassFace 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

After living with this class for a few weeks, I've run into a few issues and have found (as far as I know) solutions, so I thought I'd share the new code here.
I also cleaned up the code a little bit, and added alternating background colors for table rows output in the looped blocks. (I'll explain it at the bottom...)

Problems found:
PARSE ISSUE
If any tag data was a file name - even a file you didn't want parsed, like an image imagefile.jpg, the template engine would attempt to parse the file and it would break.
Solved by allowing only files with extension php to be sent to the parse function.

REGEX ISSUE
Again with file names, but in the template file this time, not in the tags. Any dot (.) in the looping sections in the template file would break the preg_match() function used to pull the parts of the template that would be sent to the block looping function. So if you had a link, like nextpage.php, within the section to be looped, it would break. Changed the regular expression used from ([^.]+) to ([^*]+) and it seems to work now.

Here's the new template class file in it's entirety:
php Code:
Original - php Code
  1.  
  2. <?php
  3. class Page{
  4.   var $page;
  5.  
  6.   function Page($template = "/templates/template.tpl") {
  7.         $this->page = (is_file($template)) ? file_get_contents($template) : die("Template file $template not found.");
  8.     }
  9.  
  10.   function parse($file) {
  11.     clearstatcache(); // clears info gathered from -- $path_parts = pathinfo($data); -- in replace_block_tags function
  12.       ob_start();
  13.     include($file);
  14.     $buffer = ob_get_clean();
  15.     return $buffer;
  16.   }
  17.  
  18.   function replace_tags($tags = array()) {
  19.     foreach ($tags as $tag => $data) {
  20.       $path_parts = pathinfo($data);
  21.             $ext = $path_parts['extension'];
  22.             $data = ( is_file($data) &&  $ext == 'php') ? $this->parse($data) : $data;
  23.       $this->page = str_replace("{" . $tag . "}", $data, $this->page);
  24.       }
  25.     }
  26.  
  27.   function get_block($block){
  28.     preg_match ('#<!-- START '. $block . ' -->([^*]+)<!-- END '. $block . ' -->#',$this->page,$this->return);
  29.     $code = str_replace ('<!-- START '. $block . ' -->', "", $this->return[0]);
  30.     $code = str_replace ('<!-- END '  . $block . ' -->', "", $code);
  31.     return $code;
  32.     }
  33.  
  34. function replace_block_tags($blockname, $query, $alt = false, $bg1 = "#EFEFEF", $bg2 = "#FEFEFE") {
  35.     $this->blockcode = $this->get_block($blockname);
  36.     while ($tags = mysql_fetch_assoc($query)) {
  37.         // if $alt is set to true, alternate row background color - use like <tr bgcolor="{bgcolor}">, else leave bgcolor empty
  38.         if($alt == true) {
  39.             if ($this->bgcolor == $bg1):
  40.                     $tags['bgcolor']=$bg2; $this->bgcolor=$bg2;
  41.                 else:
  42.                     $tags['bgcolor']=$bg1; $this->bgcolor=$bg1;
  43.                 endif;
  44.         } else {
  45.             $tags['bgcolor']='';
  46.         }
  47.         $block = $this->blockcode;
  48.             foreach ($tags as $tag => $data) {
  49.                 $path_parts = pathinfo($data);
  50.                 $ext = $path_parts['extension'];
  51.                 $data = ( is_file($data) &&  $ext == 'php') ? $this->parse($data) : $data;
  52.                 $block = str_replace("{" . $tag . "}", $data, $block);
  53.         }
  54.       $this->block_page .= $block ."n";
  55.         }
  56.     $this->page = str_replace($this->return[0], $this->block_page, $this->page);
  57.     unset($this->block_page);
  58. }
  59.  
  60.   function output() {
  61.     print $this->page;
  62.   }
  63.  
  64. }
  65. ?>


Now for the replace_block_tags additions:
It uses this format:
php Code:
Original - php Code
  1. $page->replace_block_tags("block to be looped", $db_query_for_loop_data, alternate_bg true/false, $bgcolor1,$bgcolor2)

You can set default bg colors to alternate right here in the class, just replace
php Code:
Original - php Code
  1. $bg1 = "#EFEFEF", $bg2 = "#FEFEFE"
with the colors you want.
To make it not alternate, just call it like before, with the first 2 parameters only:
php Code:
Original - php Code
  1. $page->replace_block_tags("band_block", $band_list_query);

To make it alternate with the default colors, do it with the first 3 parameters:
php Code:
Original - php Code
  1. $page->replace_block_tags("band_block", $band_list_query, true);

To have it alternate and use custom colors, send it all 5 parameters:
php Code:
Original - php Code
  1. $page->replace_block_tags("band_block", $band_list_query, true, "red", "blue");

or
php Code:
Original - php Code
  1. $page->replace_block_tags("band_block", $band_list_query, true, "#FF0000", "#0000FF");


Hope someone else finds this useful, I'm pretty satisfied with it now, and it's still pretty simple to use.

Reply With Quote
  #19  
Old November 3rd, 2005, 09:55 AM
PlaGuE PlaGuE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Edmonton Alberta Canada
Posts: 16 PlaGuE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to PlaGuE
RE: help : looping for template

i know this topic is probably very old.. and i may be bumping it... but, i need help. And any help is apperciated.


im using
php Code:
Original - php Code
  1. $sql = "SELECT * FROM members";
  2. $query = mysql_query($sql);
  3. $count = mysql_count_rows($query);
  4.  
  5. $page->replace_block_tags("members",$query,$count);

modified from
php Code:
Original - php Code
  1.  
  2. $sql = "SELECT * FROM `poll_choices` WHERE `poll_id` = '" . $poll_info['id'] . "'";
  3. $query = mysql_query($sql);
  4. $count = mysql_count_rows($query);
  5.  
  6. $tp->replace_block_tags("choices",$query,$count);


And

php Code:
Original - php Code
  1.  
  2. <?php
  3. class Page{
  4.   var $page;
  5.  
  6.   function Page($template = "index.tpl") {
  7.     if (is_file($template))
  8.       $this->page = implode("", file($template));
  9.     else
  10.       die("Template file $template not found.");
  11.   }
  12.  
  13.   function parse($file) {
  14.     ob_start();
  15.     include($file);
  16.     $buffer = ob_get_contents();
  17.     ob_end_clean();
  18.     return $buffer;
  19.   }
  20.  
  21.   function replace_tags($tags = array()) {
  22.     if (sizeof($tags) > 0)
  23.       foreach ($tags as $tag => $data) {
  24.         $data = (is_file($data)) ? $this->parse($data) : $data;
  25.