parsing php variables?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Zay
Forum Newbie
Posts: 22
Joined: Tue Mar 23, 2004 3:42 pm

parsing php variables?

Post 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]
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
Zay
Forum Newbie
Posts: 22
Joined: Tue Mar 23, 2004 3:42 pm

Post 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.
Zay
Forum Newbie
Posts: 22
Joined: Tue Mar 23, 2004 3:42 pm

Post 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!
Post Reply