running php codes from db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

running php codes from db

Post by mudkicker »

hi people,

i have a module script which takes infos for modules from db. i have dynamic content in some modules so i have written it with php codes so these codes are saved in db. how can i run them after i include it like this..

Code: Select all

<?php
	function load_modules() {
		$this->moduleblock = file_get_contents('module.htm');
		$blocks = '';
		foreach ($this->modules as $module) {
			$srch = array("{{MODULE_ID}}","{{MODULE_NAME}}", "{{MODULE_CONTENT}}");
			$rplc = array($module['mod_id'], $module['mod_name'], $module['mod_content']);
			$tmp = str_replace($srch, $rplc, $this->moduleblock);
			$blocks .= $tmp;
		}
		$this->file = str_replace("{{PAGE_MODULES}}", $blocks, $this->file);
	}
?>
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

oh by the way, i forgot to mention that the content is $module['mod_content'] and for example it has dynamic infos for some modules like :

Code: Select all

Hello today is : <?php echo date("d-m-Y");
so what should i do to make it shown as

Code: Select all

Hello today is : 23.11.2004
thank you..
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

eval()
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

ok, i thought the same but. how to eval? it's the problem. iit's too late and i'm confused so my brain doesn't work anymore? any ideas? ;)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

umm use preg_match to find code between <? and ?> then run eval() on it....
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

ok but then.. what about other parts of this content???
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

hmm.. i think i can't get this work, i have to make it file-based or something..
what do you think?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

im not very familiar w/ eval, i never use it. but i dont see an easy way to drop in and out of php using eval

maybe this will work?

its not very efficient, but i cant think of a better way at the moment

Code: Select all

$tmpfname = tempnam('/tmp', 'tmp_code');

$fp = fopen($tmpfname, 'w');
fwrite($fp, $php_db_code);
fclose($fp);

include ($tmpfname);

unlink($tmpfname);
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

i tried it but if you look at my code i will replace the content with {{MODULE_CONTENT}}... so i cannot include it..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Something like

Code: Select all

<?php
function modify_stuff($code) {
    $code=str_replace("stuff", "{{MODULE_CONTENT}}", $code);
    return($code);
}

$tmpfname = tempnam('/tmp', 'tmp_code');

$fp = fopen($tmpfname, 'w');
modify_stuff($php_db_code);
fwrite($fp, $php_db_code);
fclose($fp);

include ($tmpfname);

unlink($tmpfname);
?>
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

no way, this is not a good idea and inefficient. i have to find another way to create dynamic modules...

btw, thanx for your examples people...
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

mudkicker wrote:no way, this is not a good idea and inefficient. i have to find another way to create dynamic modules...

btw, thanx for your examples people...
thats what i was about to suggest :)
Post Reply