Click to See Complete Forum and Search --> : echoing php from database
JDM71488
03-20-2005, 02:10 PM
echo "<h1>Module => $row[upper]</h1>\n";
echo $module[content]."\n";
and even tried:
echo "<h1>Module => $row[upper]</h1>\n";
echo "$module[content]\n";
whats in $module[content] is: <?php echo "it worked!"; ?>
and what i get: http://accessjdm.com/index.php?module=archive
96turnerri
03-20-2005, 02:32 PM
eval() (http://uk2.php.net/eval) should do what you need however i cant remember you may need to strip the PHP tags from start & end
JDM71488
03-20-2005, 02:40 PM
hey thanks a lot... it works, but the only downfall is that i have to recode all the info in my database to php syntax... :( well at least it will work i guess..
ShrineDesigns
03-20-2005, 03:19 PM
you can use {} inside statements that tell php to parse the variables, ex. <?php
$str = 'hello word';
$array = array('blah', 'indice' => 'value');
echo "i say to you, {$str}"; // outputs: i say to you, hello world
echo "{$array[0]}, {$array[0]}, {$array[0]}..."; // outputs: blah, blah, blah...
echo "indice => $array['indice']"; // outputs: indice =>
echo "indice => {$array['indice']}"; // outputs: indice => value
$pre = "pre";
echo "$prevent"; // outputs: error
echo "{$pre}vent" // outputs: prevent
?>
JDM71488
03-20-2005, 05:04 PM
seems to be a better idea, but i can't get it to work...
index.php
echo $module[content];
what's in $module[content]
<?php
...
while($row=mysql_fetch_array($query))
{
echo "<p><b>{$row[title]} [{$row[date]}]</b>\n";
echo "{$row[content]}</p>\n";
}
...
?>
it's doing this: http://accessjdm.com/index.php?module=archive
NogDog
03-20-2005, 05:58 PM
echo() sends the text to the browser, it does not send it to the PHP parser on the server. Thus if you want to retrieve PHP statements from your DB and have them parsed and executed, you need to use eval() as suggestged by 96turnerri. Quick fix to work around the php tags:
if(preg_match("/<\\?php(.*)\\?>/", $row['content'], $matches) == 1);
{
eval($matches[1]);
}
else
{
eval($row['content']);
}
JDM71488
03-20-2005, 06:12 PM
now, eval() works, but not all of the modules in the db are in php... only the archived news for they are called from another table... i don't want to use eval()... i want to be able to store html uls in my database... but with eval, i will have to do this:
echo "<ul>\n";
...
but i thought i could just put in some {} to make them display the variables... and for some reason, it puts the php code into the source code as well. how can i dispaly the variables, and hide the php in the source? without using eval?
ShrineDesigns
03-20-2005, 07:44 PM
you shouldn't put php code blocks in a database, this could lead to security vunerabilities
thisecho $module[content];should beecho $module['content'];
JDM71488
03-20-2005, 08:03 PM
ok, i changed it... but is there no way to force the display of the php code besides eval()?
NogDog
03-20-2005, 08:19 PM
OK, I'm confused now. Do you want the contents of the data field to simply be displayed on the web page, or do you want the contents of the data field to be executed as though it were part of the rest of your PHP code (sort of like using include() to run PHP code from an external file)? Or are you trying to do option C which I haven't figured out yet? ;)
JDM71488
03-20-2005, 08:26 PM
modules in the database:
1) links: just some ul li a...
2) archive: php code that i want included in the page
3) resume: more html...
i figured i could echo 1 & 3, but when i echo 2 its blank with the php code in the source...
i tried wrapping the contents of 2 in the database with <?php and ?> too... but still nothing...
now, if i use eval, all of the modules will have to be converted to echoes just to have 2 displayed correctly... i liked the idea of parsing using {} around the variables i wanted proccessed... but i understand that echo skips the server and goes to the browser...
is there any other way?
NogDog
03-20-2005, 08:43 PM
Probably what I'd do is add a column to the table that tells you what to do with the data. Some of the possible values might be "PHP", "HTML", and "TEXT". Let's suppose we call this column "type" in the DB table:
# do your query and fetch results to array $row, then:
switch(strtoupper($row['type']))
{
case "PHP": # send the PHP content to the PHP interpreter:
eval($row['content']);
break;
case "HTML": # display the HTML content as is:
echo $row['content'] . "\n";
break;
case "TEXT": # if type is TEXT or no/another type is specified, then
default: # display in a paragraph, converting \n to <br>
echo "<p>" . nl2br($row['content']) . "</p>\n";
}