Jump to content

[SOLVED] Replacing text in a template with defined values?


Salis

Recommended Posts

All right. I'm not sure how to explain this but.... I am working on a web site that allows members to "custom" design their own profile template. I pulled this idea from phpbb forums where there are values in the database and in the template you have, for an example, {POSTER_NAME}. When some one reads the page, the server (some how, and this is were i need help) read the template and replaces {POSTER_NAME} with the corresponding value then sends it to whom ever requested it.

Link to comment
Share on other sites

I was thinking that, but my concern with that is load time and bandwidth. I would have to open the file, read the text (clean it for any hacking code), replace it then echo it. I've seen some fast phpbb boards with a lot of text but I'm not sure if this is how the phpbb boards work or not.

 

But I'll give it a shot thanks.

Link to comment
Share on other sites

assuming you know how to do a select in mysql, I will skip that to actually displaying content... Very simple.

 

<?php
$sql = mysqli_query($db,"SELECT * FROM table WHERE id='$someuser'");
$row = mysqli_fetch_array($sql);
echo $row['userName'];  //This is one field in the table that contains the users name
echo $row['pageContent']; //This is another filed in the table that contains the content
?>

Link to comment
Share on other sites

I hope to clear a little up. Below is an example of what will go on.

 

<?
// connect to the database
define("My_SITE",true);
require("includes/sql_connection.php");
include("includes/functions.php");

$Clean = new CleanStr;

$Mem_Query = mysql_query("SELECT * FROM members WHERE mname='".$Clean->MQur($_GET['member'])."'");
$Mem_Array = mysql_fetch_array($Mem_Query);

if( empty($Mem_Array['mname']) ) {
	// Want to include a "No Member Found" notice
}
else {
	$tpl_replace = array('{MEMBER_NAME}'=>$Mem_Array['mname'],
		'{MEMBER_AVATAR}'=>$Mem_Array['avatar'],
		'{MEMBER_BIO}'=>$Mem_Array['bio']);

	//want to include the template here then replace array_keys with array_values as above
}

?>

 

 

OK, you see $tpl_replace right. Well what I want to do is read the contents of the template ant replace the keys with the values. I don't want the template to include any php for security reasons, however I want to use this approach so that my members will be able to create a template for them selves. Does this help explain it a bit better?

 

My goal is to not only offer a web site that it's members can customize, but also a site that I design with no 3rd party programs.

Link to comment
Share on other sites

Ive tried that too, as long as you do not need to loop through data etc it is straight forward, or if you do it should be done before the parse.

 

<?php
// connect to the database
define("My_SITE",true);
require("includes/sql_connection.php");
include("includes/functions.php");

$Clean = new CleanStr;

$Mem_Query = mysql_query("SELECT * FROM members WHERE mname='".$Clean->MQur($_GET['member'])."'");
$Mem_Array = mysql_fetch_array($Mem_Query);

if( empty($Mem_Array['mname']) ) {
	// Want to include a "No Member Found" notice
}
else {
	$tpl_replace = array('{MEMBER_NAME}', '{MEMBER_AVATAR}', '{MEMBER_BIO}');
                 $tpl_replace_with = array($Mem_Array['mname'], $Mem_Array['avatar'], $Mem_Array['bio']);

                $template = file_get_contents('template.tpl');
                $template = str_replace($tpl_replace, $tpl_replace_with, $template;

                echo $template;

                // the above OR you could do it like this.
	$tpl_replace = array('{MEMBER_NAME}'=>$Mem_Array['mname'],
		'{MEMBER_AVATAR}'=>$Mem_Array['avatar'],
		'{MEMBER_BIO}'=>$Mem_Array['bio']);
                $template = file_get_contents('template.tpl');
                foreach ($tpl_replace as $replace => $with) {
                       $template = str_replace($replace, $with, $template);
               }

               echo $template;
}

?>

 

Both should be fairly quick, I doubt you would see a difference in performance, I my self would prefer the foreach but yea.

I think that would work..

Link to comment
Share on other sites

Ummm.. Hum... wow that easy?! Thanks everyone for your help and support! frost110, file_get_contents() is exactly what I needed!

 

Man, you'd think that with hundreds of php functions, you'd figure the php development team would combine like functions and reduce the function head ache!

 

Again thanks every one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.