PHP Question: eval() function maybe / include () function?

Superwormy

Golden Member
Feb 7, 2001
1,637
0
0
OK, basically what I want to do, is store an entire page in a database.

So say my page is this:

<html>
<title>arg</title>
<head></head>
<body>


This is <?php print ("$fname $lname"); ?>'s Website!

Content

</body>
</html>



I want to store that whole bit in a database, which is fine I can do that, BUT, then I want to pull it back out and build the page with it. Problem being, if I say this:

$fname = Keith
$lname = Palmer

SELECT * FROM $tablename etc etc etc to get the content

print ($select_result);


PHP won't parse that, and thus won't print out my firstname and lastname. How can I get PHP to parse the string? The ' eval() ' function sounds promising but I don't understand how its used and the documentation is rather sparse on it. Suggestions? The only other idea I could come up wiht was to write it to a file, then include () it. Is there a way I can make include () take a string instead of a file?

 

kt

Diamond Member
Apr 1, 2000
6,027
1,342
136
First of all, how does your database table structure look like? Let's say your table contain the following columns: fname, lname, page_name and content.

Then your code will look something like this:

<?
$db_query = "SELECT * FROM $tablename WHERE page_name='$yourpagename'";
$db_result = mysql_query($db_query);
$db_row = mysql_fetch_array($db_result);
?>

<html>
<title><? echo $db_row['page_name']; ?></title>
<head></head>
<body>

This is <? echo $db_row['fname']." ".$db_row['lname']; ?>'s Website!


<? echo $db_row['content']; ?>

</body>
</html>
 

Superwormy

Golden Member
Feb 7, 2001
1,637
0
0
I think you missed the poitn or I wasn't very clear, probably the latter... :-(

Anyway, thep roblem is that I have PHP code stored in the database. ie. the page content STORED IN THE DATABASE looks like this:

-------------------

<html>
<title>title of page</title>
<head>
</head>

<table>
<tr>
<td>

Welcome.

<?php print ("This is $fname $lname 's"); ?> Website.


</td>
</tr>
</table>


----------------------

There, all that is stored in the database.


Now, I need all the pulled out of the database.

So I do my query:

$query = SELECT page_content FROM pages WHERE page_id = '1';
$result = mysql_query ($query);

bla bla bla etc.


So, I pull the above said page content out of the database, well that page contains PHP code, so if I say:

print ($result[page_content]);


It will NOT PARSE THE PHP CODE, and the actual code on that page will get treated as plain HTML and printed out to the browser. Never evaluated, never parsed. How can I get it to parse that code?

 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Here's one thing you can do:

$fp = fopen("temp directory/temp file","w");
fputs($fp,$result[page_content]);
fclose ($fp);
include "temp directory/temp file";


eval would work as well, but ONLY if you only want variables filled in, I believe. I have not tried that one myself...

so, echo eval($result[page_content]);

should work if you only need variable names replace, yes.
 

Superwormy

Golden Member
Feb 7, 2001
1,637
0
0
Yeah thats what I was doing for now, but I was hoping there would be a more efficient way of doing it.

Afraid that that might put kinda a heavy load on the server, always writing files out for every page... :-(
 

Superwormy

Golden Member
Feb 7, 2001
1,637
0
0
Other suggestions? Theres gotta be a way to take a string and treat is as PHP code...

Or to make an include() function take a string instead of a file...
 

kt

Diamond Member
Apr 1, 2000
6,027
1,342
136
I see what you are trying to do.. I think I misunderstood you the first time. This would be easiest way for you to do what you are trying to accomplish.. not the most efficient way, but the easiest way.

<?php
$fname = Keith;
$lname = Palmer;

$db_query = "SELECT * FROM $tablename WHERE page='$yourpagename'";
$db_result = mysql_query($db_query);
$db_row = mysql_fetch_array($db_result);

ob_start();
eval("?>" . $db_row['content'] . "<?php ");
$output = ob_get_contents();
ob_end_clean();

print($output);
?>


Does that make sense?

--edit--
BTW, the above code will only work with PHP 4 or greater. There's no output buffering function in PHP 3.