[ borneo @ 09.09.2005. 16:49 ] @
Ima li neko blagu ideju, zasto prilozeni samplate2.php samo inkluduje template.class i tu stane (nece da pozove funkcije) ? samplate2.php Code: <? include("template.class"); $page_title = "Address Book"; // The default page will retrieve persons having last name beginning with 'a' if (! isset($letter) ) : $letter = "a"; endif; $tpl = new template; $tpl->register_file("book", "book.php"); $tpl->register_variables("book", "page_title,letter"); $tpl->address_sql("book", "rows.addresses","$letter"); $tpl->file_parser("book"); $tpl->print_file("book"); ?> template.class Code: 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"; 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($fh, filesize("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($fh, filesize($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 book.php Code: <html> <head> <title>:::::{page_title}:::::</title> </head> <body bgcolor="white"> <table cellpadding=2 cellspacing=2 width=600> <h1>Address Book: {letter}</h1> <tr><td> <a href="index.php?letter=a">A</a> | <a href="index.php?letter=b">B</a> | <a href="index.php?letter=c">C</a> | <a href="index.php?letter=d">D</a> | <a href="index.php?letter=e">E</a> | <a href="index.php?letter=f">F</a> | <a href="index.php?letter=g">G</a> | <a href="index.php?letter=h">H</a> | <a href="index.php?letter=i">I</a> | <a href="index.php?letter=j">J</a> | <a href="index.php?letter=k">K</a> | <a href="index.php?letter=l">L</a> | <a href="index.php?letter=m">M</a> | <a href="index.php?letter=n">N</a> | <a href="index.php?letter=o">O</a> | <a href="index.php?letter=p">P</a> | <a href="index.php?letter=q">Q</a> | <a href="index.php?letter=r">R</a> | <a href="index.php?letter=s">S</a> | <a href="index.php?letter=t">T</a> | <a href="index.php?letter=u">U</a> | <a href="index.php?letter=v">V</a> | <a href="index.php?letter=w">W</a> | <a href="index.php?letter=x">X</a> | <a href="index.php?letter=y">Y</a> | <a href="index.php?letter=z">Z</a> </td></tr> {rows.addresses} </table> </body> </html> rows.addresses Code: <tr><td bgcolor="#c0c0c0"> <b>{last_name},{first_name}</b> </td></tr> <tr><td> <b>{telephone}</b> </td></tr> <tr><td> <b><a href = "mailto:{email}">{email}</a></b> </td></tr> Svaka pomoc dobrodosla Hvala |