Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP and echo

Status
Not open for further replies.

Shanksta

Technical User
Jun 28, 2002
96
0
0
US
Hi all -

I am building a custom content management system and needed some quick help.

I echo out the text for the body from my SQL DB. However in that I have a php include. How can I make it so that the echo escapes, executes the include and then continues with the text?

Thanks
 
I don't understand.

Why wouldn't you just end the echo statement by having a close quotes, issue the include() statement, then start a new echo statement?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
tried eval, but doesnt seem to work...

the body text is $db['body']; and inside that is require("test.php");

can you give me an example of what I would code to make the php execute?

thanks for your quick replys
 
All it is is a simple

echo $body;

inserted into a table cell. $body refers to the mySQL data I fetch that is basically a text cell. In there I have the require that needs to exectute. Its litterally just require("test.php"); that I put there.

 
Here's a quick test I wrote.

Main code:
Code:
<?
$str = 'require("testingr.php");';
eval($str);
?>

testingr.php
Code:
<?
$xyz = 'xxx';
echo $xyz."<br>\n";
?>

The output of this is:
Code:
xxx

Proving that the eval of the require worked.

Is your code similar?

Ken
 
I have the following code inside the $body var - what is currently being echoed:

(text... yada yada)
<br>
require("test.php");
<br>
(more text... yada yada yada)

This is the var that I am using eval on
 
Since
Code:
(text... yada yada)
<br>
require("test.php");
<br>
(more text... yada yada yada)
does not look like all PHP code, eval is going to issue an error message. If you're using EVAL, then all the code in the EVALed string must be valid PHP.

Ken
 
Can anyone still help with this? Ill try to get as specific as possible...

For every page on my site I use a MySQL DB to retrieve the page contents. I have a field for title, heading, and body. I use the echo funciton in php to show the text in each one of the fields.

In one of my body fields I typed:
Code:
<?php
require('test.php');
?>

I wanted to have a small php generated list be executed.
However, the PHP is never executed and I can not see the list. Does anyone know how I can go about getting that php code to execute from within the MySQL text field when I echo it?

Thanks
 
So your problem is:

How do you get php to interpret a string as php code not a litteral?

No clue. but maybe that simplifies you question though.

-Jer
 
Yep. Just trying to give the context.
 
if you tried the eval() function like kenrbnsn advised, make sure you don't need any escape characters.

For Example:
Code:
<?php
require(\"test.php\");
?>

-Jer
 
Maybe I'm way off on this one, but here is a thought...

Perhaps you could require $body rather than echo it. Begin body with the echo statement so that you can then end the echo, require test.php, and open a new echo.

Within your table:

Code:
require($body);

And then, $body would look like this...

Code:
echo "text yadda yadda yadda<br>";
require(test.php);
echo "<br> text yadda yadda yadda";

I don't know if this applies to your situation or not as you haven't posted the table code or anything. It's not a pretty solution, but as I understood your problem it might be worth a shot.
 
GiffordS, its not going to parse that as code but as a string litteral.

-Jer
 
I am sure there must be an easier way, but what comes to my mind right now is to find the require("") in the string and then split the string into three parts.

echo first part
eval the second part
echo the third part


I am not a great php programmer, so I have to look for the code to give you. Can someone give him the code.
 
For anyone who had a similar problem, this is how I solved it:


// turn on output buffering
ob_start();
// this parses the php code in out $php variable; this is the heart of our templating logic
eval(" ?>" . $body . "<? ");
// store the final parsed output in a variable $output
$output = ob_get_contents();
// clear buffer and stop buffering
ob_end_clean();

// this line show the parsed final data in the browser
echo $output;

worked beautifully!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top