Web Hosting Talk







View Full Version : Database stored templates problem


joprout
09-22-2004, 01:18 PM
Hi everyone, i'm currently updating my website and i try to do as much professionnal as possible. So what i do is i store all the major templates into my database. But i have a problem.

I have a function that call my template codes which is in my function.php file:

class templates
{
function show_templates($name)
{
$result = mysql_query( "SELECT * FROM templates WHERE name='$name'");
$t = mysql_fetch_array($result);
$templa = $t[template]; /// $t[template]; is where the template code is store.
print $templa;
}
}


Now on the page that i need the template, here's what i basicly write:

include('function.php');

$temp = new templates;

$str = "hello";
$temp->show_templates("admin_printer_tablehead");



Everything's fine, all the HTML codes works. The problem is, the PHP Variables that are in my templates. They don't show! The website read them as an HTML code as well. So instead of showing what the variable equal, it shows the variable itself. (e.g. $str = "hello". Instead of showing Hello, it will show $str. )

Is there a function that would help me with my problem ?

joprout
09-22-2004, 01:48 PM
I forgot to mentionned that even if i put the variable that is in the template like this:

<? echo $str; ?>

It doesn't work

joprout
09-22-2004, 09:49 PM
I realy need help... If you don't understand what i mean, please let me know.

starfish0226
09-22-2004, 10:11 PM
It is the equivalent as if you had typed

echo '$variable';

when you really meant

echo "$variable";

Use the function eval(). Look into how it works.

joprout
09-22-2004, 10:25 PM
No it doesn't work

starfish0226
09-22-2004, 10:34 PM
Read the documentation again. You must be using it wrong.

Try enclosing the variables in

<?php echo $variable ?>

tags instead of

<?=$variable?>

eval() is essentially your only option here.

joprout
09-23-2004, 09:33 AM
No it doesn't work. Here's what i did, please correct me if i'm wrong.

/// this is my show_templates($name); function
$result = mysql_query( "SELECT * FROM templates WHERE name='$name'");
$t = mysql_fetch_array($result);
$templa = $t[template];
eval( "\$templa = \"$templa\";" );
echo $templa;


i'll explain my problem more in detail.

In the database name "templates", i have this template name "prod_detail" and with the folowing codes:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>$str</td>
</tr>
</table>

With a function that is located in the function.php file, i call the template codes that are located in the "templates" database. Here's the function again:


class templates
{
function show_templates($name) /// $name = "prod_detail";
{
$result = mysql_query( "SELECT * FROM templates WHERE name='$name'");
$t = mysql_fetch_array($result);
$templa = $t[template]; /// $t[template]; is where the template code is stored.
echo $templa; //// this prints the code on the page
}
}


Now, in my other page that actualy shows on the web, here's what i have:


include( function.php );

$temp = new templates;

$str = "hello";
$temp->show_templates("admin_printer_tablehead");/// this call the templates code that are store in the DB with the function that is located in the function.php page.


Result:
$str

Expected result:
hello

I tried eval, but doesn't work. Maybe i don't put it right but i followed what the exemple were showing me.

starfish0226
09-23-2004, 03:30 PM
I see your problem. Your PHP code is not being placed in your template as PHP code. How in the world is the interpreter supposed to know $str is actually PHP code, and not just you actually meaning to type "$str."

Normally, this is done by encasing such strings with <?php and ?>. If you want your script to work, encase them with PHP tags. Eval() or anything else you ever try will fail unless you put in some proper PHP. Think about it. If I pasted that "php" into a txt file, uploaded it, and ran it, "$str" would come out as "$str."

Otherwise I suggest you rethink your templating engine. It is very ineffective. Try using {STR} tags inside your HTML and use a str_replace.