View Full Version : help with php in included files?

10-02-2002, 10:59 AM

I am currently writing a large news database.

It allows us to fill in a form on a website and then it saves the info to the database.

Anyway - when we actually try and put php tags into the news items we store in the database, they dont show up on the page that displays it.

But, if you go to view source, they are written in there? Can anybody help.


10-02-2002, 11:59 AM
I think i wont work that way... Storing PHP code mixed with free text in a database, then extract the text in a string variable and get PHP code parsed...
I've done something similar, but used custom tags like [ArticleID] included in text, then replaced them with the appropriate value... But there was a limited set of recognized tags and there was specific code that maked the replacement for each tag...


10-02-2002, 12:11 PM
Hi - I'm a newbie..

Bit confused now :confused:

Any more help?

10-02-2002, 12:47 PM
Well, that was just a shot in the dark...
Can You post some sample or more detailed explanation of what You want to do and what goes wrong?


10-02-2002, 12:50 PM
Well - I have a page

display.php3 - this file includes a file news.php3

- which gets stuff from the database - that works fine.

But when i store php tags into the database, they dont show up in news.php3. if you view the source of the page, they are there in full. they dont show up on the broswer window..

i have tried everything and i just cant get it right!

10-02-2002, 12:58 PM
PHP tags stored in a database will not be executed by the server, because they are not part of any script - they are just string constants. Browser will not show them, because by default it will skip any tag it does not understand, as <?...?> for example.
Now, are You sure storing PHP code in the database can not be avoided? I just can not image any reason for the code to be there...


10-02-2002, 01:00 PM
is there anyway it can be done?

10-02-2002, 01:23 PM
Sure, but that does not make sense...
1. Extract the text from database in a variable
2. Use a regular expression to match all <?...?> pairs.
3. Parse the text inside and replace all that is a syntactically valid PHP variable with the appropriate value.
4. Output the result to the browser.


10-02-2002, 01:24 PM
umm sorry to do this - but would u mind just givin me an example of those steps?

i dont quite know what u mean. sorry

10-02-2002, 01:30 PM
Can You post the PHP code, and at least one of the texts as stored in the database?


10-02-2002, 01:34 PM

This is the code on the news.php3 page.


$db = mysql_connect("www.handclaireonline.com", "connect

mysql_select_db("news",$db)or die(mysql_error());

$sql = "SELECT * FROM newsdb WHERE $id=id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

$id = $myrow["id"];

$displaytitle = $myrow["displaytitle"];

$news = $myrow["news"];

$displaydate = $myrow["displaydate"];

$time = $myrow["time"];

$by = $myrow["by"];

$realtitle = $myrow["realtitle"];

$dateyear = $myrow["dateyear"];

$newsshort = substr($news, 0, 250);


<body topmargin="0" leftmargin="0">
<p style="margin-top: 0; margin-bottom: 0">
<font face="Verdana" size="1" color="#404D55">


if ($archive) {

echo "<br><font face='Verdana' size=2 color=#FDCD0F>[ <a href=http://www.handclaireonline.com/archive/archive.php3?news=yes><font face='Verdana' size=2 color=#FDCD0F>return to archive</a> ]</font><p>";


<br><font face=Verdana size=1 color=#404D55>
//<?php echo $displaydate ?>-<?php echo $dateyear ?><br>
<font face="Verdana" size="2" color="#FFFFFF"><?php echo $realtitle ?><br>
<font face="Verdana" size="1" color="#FDCD0F"><?php echo $news ?><br>
&nbsp;</font><p style="margin-top: 0; margin-bottom: 0">
<img border="0" src="linehoriz.jpg" width="522" height="5"><br>
<font color="#FFFFFF" face="Verdana" size="2"><a href="display.php3">
<font color="#FFFFFF">[click here to go back]</font></a></font></b></p>

and this is the text from one of the database - it only has one small amount of php - so i just post that area.

This seems like a pretty much all ballad cd - but there will be some upbeat
tracks as well! More news when we get it!<br>
You can check out some more news about the album <a href="display.php3?id=50&ref=<?php echo $id ?>">here </a>

hope this helps


10-02-2002, 01:56 PM
Ok, I see...
Try just to change the part of code that stores all this in the database so that it sotores $id instead of <?php echo $id ?>.
PHP will replace it with its value authomatically...


10-02-2002, 01:57 PM
i have done that

it just comes up with $id then!

10-02-2002, 02:12 PM
It should not...
What version of PHP are You using?
There's a piece of PHP code. Can You start it in Your environement and tell me what's the output?
$id = 5;
$Str = "<br>This seems like a pretty much all ballad cd - but there will be some upbeat tracks as well! More news when we get it!<br>You can check out some more news about the album <a href=\"display.php3?id=50&ref=$id\">here </a>";




10-02-2002, 02:15 PM
this is what i get.


10-02-2002, 03:15 PM
If PHP variables inserted in Youtr DB are a limited number You can forge a block of code to extract the right values...
Something like this:

$PHP_IN_DB1 = "<?php echo \$id ?>";
$REPLACE1 = "$id";
$Str = str_replace($PHP_IN_DB1,$REPLACE1,$Str);



$Str is a string extracted from the database.
$PHP_IN_DB1 is the Piece of PHP in the databse. Note the backslash before the variable name....