Page 1 of 1

parsing php variables?

Posted: Tue Mar 23, 2004 3:42 pm
by Zay
Hi,

first of all, thank you for taking time reading this!

I was working on a new version of my content management system of my site when I decided I wanted my pages to be stored on database.

it's not like it's more efficient, but it's because I want to be able to do so.

anywayz, I had no problems saving it, nor reading it...

the problem lies here:

Code: Select all

<?php
// database connection already exists

if (ISSET($_GET['pageid'])) {
	$pageid = $_GET['pageid'];
}
else {
	$pageid = 1; // if nothing then home
}
$sql = "select content from pages where id=$pageid";
$result = mysql_query($sql);
$content = mysql_result($result,0);
echo $content;
?>
this will obviously not work, because $content containt php code that will not be parsed this was.

so now we get to the part where I ask all of you:
if there a way to parse that, and if so how?

again, many thanks for the time you take helping me solve it.[/php_man]

Posted: Tue Mar 23, 2004 4:07 pm
by penguinboy

Posted: Tue Mar 23, 2004 5:33 pm
by McGruff
If you find yourself reaching for eval put it back it the box and work out what you're doing wrong.

Can you put the content in the db and the php in php files?

Posted: Wed Mar 24, 2004 12:56 am
by Zay
that's the whole point here... I want to be able to create/edit/delete pages without using anything but my CMS...

This is just a test thing for me, but I'd like to get it to work... I could rebuild my entire site with easier things, but I use the difficult ones so I know how they work.

Posted: Wed Mar 24, 2004 2:56 am
by Zay
woohoo! I got it :)
for those interested, here's the snippet:
$content containts the page. it can contain both PHP code as normal HTML

Code: Select all

$continue = true;
while ($continue) &#123;
	// non php code

	$pos = strpos($content,"<?");
	if (!$pos) &#123;
		$pos = strlen($content);
		$continue = false;
		$nonscript = substr($content,0,$pos);
		echo $nonscript;
	&#125;
	else &#123;
		$nonscript = substr($content,0,$pos);
		echo $nonscript;

		// reset for php code check
		$content = stristr($content,"<?");


		// php code
		$pos = strpos($content,"?>");
		if (!$pos) &#123;
			$continue = false;
		&#125;
		else &#123;
			$withscript = substr($content,2,$pos);
			echo eval($withscript);
			// reset for next loop
			$content = stristr($content,"?>");
			$content = substr($content,2,strlen($content));
		&#125;
	&#125;
&#125;
thank you penguinboy!