Page 1 of 1

running php codes from db

Posted: Wed Nov 24, 2004 5:18 pm
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);
	}
?>

Posted: Wed Nov 24, 2004 5:21 pm
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..

Posted: Wed Nov 24, 2004 5:31 pm
by rehfeld
eval()

Posted: Wed Nov 24, 2004 5:34 pm
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? ;)

Posted: Wed Nov 24, 2004 10:38 pm
by josh
umm use preg_match to find code between <? and ?> then run eval() on it....

Posted: Thu Nov 25, 2004 2:33 pm
by mudkicker
ok but then.. what about other parts of this content???

Posted: Thu Nov 25, 2004 3:06 pm
by mudkicker
hmm.. i think i can't get this work, i have to make it file-based or something..
what do you think?

Posted: Thu Nov 25, 2004 3:23 pm
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);

Posted: Thu Nov 25, 2004 3:26 pm
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..

Posted: Thu Nov 25, 2004 4:17 pm
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);
?>

Posted: Thu Nov 25, 2004 4:53 pm
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...

Posted: Thu Nov 25, 2004 6:00 pm
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 :)