Current location: Hot Scripts Forums » Programming Languages » PHP » templating problem (how to forward file name as a variable)


templating problem (how to forward file name as a variable)

Reply
  #1 (permalink)  
Old 09-11-05, 08:27 PM
skyrat skyrat is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Question templating problem (how to forward file name as a variable)

Hi all,

I have some templating system (Address Book) that only works partially.

It contains 4 files:
samplate2.php - executive file/attached/
book.php - template 1/attached/
rows.addresses - template 2/attached/
template.class

The first and the second template variable ($page_title and $letter) are set in
samplate2.php (executive file), but not the third(rows.addresses), because that variable cannot be predefined.
It get its value, after processing in address_sql function (WHILE loop).

So, first 2 variables are replaced, and third is not.

I think the defect of this code is that the $complete_table variable (from address_sql) is not forwarded to WHILE loop in
file_parser function (all from template.class) for replacement of
3.template variable {$rows.addresses} (which is the name of corresponding template file).

I need to make rows.addresses global, something like:
PHP Code:

VAR $rows.addresses ''
in the global part of template.class
and subsequently:
PHP Code:

. . .

$complete_table .= $new_row;
endwhile; 
 
$rows.addresses $complete_table
but this doesn't work (blank page), so I'm wondering, is the rows.addresses a valid variable name ?

Well, what can I do to forward $complete_table to replacement loop (file_parser function) but with the name 'rows.addresses' because this is the name of template variable (also of corresponding template file).

template.class
PHP Code:

<?php

class template {
VAR 
$files = array();
VAR 
$variables = array();
VAR 
$sql = array();
VAR 
$opening_escape '{';
VAR 
$closing_escape '}';

VAR 
$host "localhost";
VAR 
$user "root";
VAR 
$pswd "123456";
VAR 
$db "book";
VAR 
$address_table "addressbook";

VAR 
$rows.addresses '';


function 
address_sql($file_id$variable_name$letter) {

// Connect to MySQL server and select database
mysql_connect($this->host$this->user$this->pswd)
or die(
"Couldn't connect to MySQL server!");

mysql_select_db($this->db) or die("Couldn't select MySQL database!");

// Query database
$query "SELECT last_name, first_name, tel, email
FROM 
$this->address_table WHERE last_name LIKE '$letter%'";
$result mysql_query($query);

// Open "rows.addresses" file and read contents into variable.
$fh fopen("$variable_name""r");
$file_contents fread($fhfilesize("rows.addresses") );

// Perform replacements of delimited variable names with table data
while ($row mysql_fetch_array($result)) :
    
$new_row $file_contents;

    
$new_row str_replace(
    
$this->opening_escape."last_name".$this->closing_escape,
    
$row["last_name"],
    
$new_row);

    
$new_row str_replace(
    
$this->opening_escape."first_name".$this->closing_escape,
    
$row["first_name"],
    
$new_row);

    
$new_row str_replace(
    
$this->opening_escape."telephone".$this->closing_escape,
    
$row["tel"],
    
$new_row);

    
$new_row str_replace(
    
$this->opening_escape."email".$this->closing_escape,
    
$row["email"],
    
$new_row);

// Append new table row onto complete substitution string
$complete_table .= $new_row;
endwhile;

// Assign table substitution string to SQL array key
$sql_array_key $variable_name;
$this->sql[$sql_array_key] = $complete_table;

// add the key to the variables array for later lookup
$this->variables[$file_id][] = $variable_name;

// Close the filehandle
fclose($fh);
// end address_sql


// Function: register_file()
    // Purpose: Store contents of file specified by $file_id
function register_file($file_id$file_name) {

        
// Open $file_name for reading, or exit and print an error message.
    
$fh fopen($file_name"r") or die("Couldn't open $file_name!");
        
// Read in the entire contents of $file_name.
    
$file_contents fread($fhfilesize($file_name));
        
// Assign these contents to a position in the array.
        // This position is denoted by the key $file_id
    
$this->files[$file_id] = $file_contents;
        
// We're finished with the file, so close it.
    
fclose($fh);
// end register_file;


// Function: register_variables()
// Purpose: Store variables passed in via $variable_name under the corresponding
// array key, specified by $file_id
function register_variables($file_id$variable_name) {
        
// attempt to create array from passed in variable names
    
$input_variables explode(","$variable_name);
        
// assign variable name to next position in $file_id array
    
while (list(,$value) = each($input_variables)) :
            
// assign the value to a new position in the $this->variables array
        
$this->variables[$file_id][] = $value;
    endwhile;
// end register_variables


// Function: file_parser()
// Purpose: Parse all registered variables in file contents
// specified by input parameter $file_id
function file_parser($file_id) {
        
// How many variables are registered for this particular file?
    
$varcount count($this->variables[$file_id]);
        
// How many files are registered?
    
$keys array_keys($this->files);
        
// If the $file_id exists in the $this->files array and it
        // has some registered variables…
    
if ( (in_array($file_id$keys)) && ($varcount 0) ) :
            
// reset $x
        
$x 0;
            
// while there are still variables to parse…
            
while ($x sizeof($this->variables[$file_id])) :
                    
// retrieve the next variable
                
$string $this->variables[$file_id][$x];
// retrieve this variable value! Notice that I'm using a
// variable variable to retrieve this value. This value
// will be substituted into the file contents, taking the place
// of the corresponding variable
// name.
            
GLOBAL $$string;
// What exactly is to be replaced in the file contents?
            
$needle $this->opening_escape.$string.$this->closing_escape;
// Perform the string replacement.
            
$this->files[$file_id] = str_replace(
            
$needle// needle
            
$$string// string
            
$this->files[$file_id]); // haystack
    // increment $x
            
$x++;
            endwhile;
    endif;
// end file_parser


// Function: print_file()
// Purpose: Print out the file contents specified by input parameter $file_Id
function print_file($file_id) {
        
// print out the contents pointed to by $file_id
    
print $this->files[$file_id];
}
// end template.class
?>



Thanks a lot
Attached Files
File Type: txt samplate2.php.txt (425 Bytes, 437 views)
File Type: txt book.php.txt (1.2 KB, 435 views)
File Type: txt rows.addresses.txt (182 Bytes, 410 views)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload Script Problem!!! seanknighton Perl 0 03-21-04 09:54 PM
Function and variable problem perleo PHP 1 02-11-04 08:52 PM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
file download problem ukyankee Perl 6 10-04-03 10:39 PM
New Web Host, New Problem! justchat PHP 2 09-29-03 02:39 PM


All times are GMT -5. The time now is 06:12 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.