Inserting data at marker point

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
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Inserting data at marker point

Post by mattcooper »

Hi all,

Does anyone know of a way of inserting code at a "marker", such as a special HTML comment, in order to write new code to the middle of an existing document?

I have some HTML code for page content stored in a database, part of a CMS I'm about to finish that creates ezines. Because of problems with incorporating PHP code in a Javascript-based editor environment that would only render the PHP-generated code to HTML, I would like to add the PHP at the very last minute, when the user publishes the ezine. At that point, I would like to create a new document on the server, get the code
from the database, write it to the new file, find the "marker", add the new code, rewrite the file's new contents back to the database and then delete the temporary file.

Any thoughts? There's bound to be an easier way to do this, but I'm struggling to find it...

Thanks in advance :)
Last edited by mattcooper on Thu May 11, 2006 1:09 pm, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Can you elaborate a little please?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

hawleyjr wrote:Can you elaborate a little please?
I thought somebody would ask that, so have edited the post!
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Additionally, I have tried to use eregi on the string <!--MENU--> with no success - eregi is not finding the string for some reason. I thought that if I could replace this string with the contents of the PHP file I want to include, that would solve my problem.

Anyone got any thoughs on what's going wrong with this script??::

Code: Select all

$ezine_id = $_GET['ezine_id'];
	
	$sql = "SELECT page_content 
	FROM {$ezine_id}_content 
	WHERE page_id = '1'";
		
	$result = mysql_query($sql) or die("Could not perform the query - ".mysql_error());
	$contents = mysql_fetch_assoc($result) or die("Could not get row information - ".mysql_error());
	$string = '<!--MENU-->';

	if(!ereg("$string","$contents")){
		echo "Couldn't find the menu marker using string $string<p>";
	//	exit();
	}

	$split = explode("<!--MENU-->",$contents) or die("Problem with exploding string");
	
	$part1 = $split[0];
	$part2 = $split[1];
	
	$contents_of_array = array_values($split);
	
	$makemenu = file_get_contents("mengen.php");
	$addmenu = $part1.$makemenu.$part2;
All help greatly appreciated!
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Try replace

Code: Select all

if(!ereg("$string","$contents")){
with

Code: Select all

if ( !ereg($string, $contents['page_content'])){
Have a nice day!

- Nathaniel
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Oh, you'll want to replace $contents in

Code: Select all

$split = explode("<!--MENU-->",$contents) or die("Problem with exploding string");
with $contents['page_content'] as well.

- Nathaniel
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Right, I have the replace working now - thanks Nathaniel.

Problem is, instead of parsing, the new code simply displays in the source code of the php page but only if the page was generated dunamically - i.e, the temp.php page runs correctly. Any ideas why it's not being parsed? Here's the new code...

Code: Select all

$ezine_id = $_GET['ezine_id'];
	
	$sql = "SELECT page_content 
			FROM {$ezine_id}_content 
			WHERE page_id = '1'";
		
	$result = mysql_query($sql) or die("Could not perform the query - ".mysql_error());
	$contents = mysql_fetch_row($result) or die("Could not get row information - ".mysql_error());
	$string = '::MENU::';
	
// Make a temporary file to store the content
	$filename = "/path/to/file/$ezine_id/temp.php";
	if (!$handle = fopen($filename, 'w+')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   // Write $contents to our opened file.
   $contents = $contents[0];
   if (fwrite($handle, $contents) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   
   
	$contents = file_get_contents("/path/to/file/$ezine_id/temp.php");

	if(!ereg("$string","$contents")){
		echo "Couldn't find the menu marker using string $string<p>";
	//	exit();
	}
	
	$split = explode("::MENU::",$contents) or die("Problem with exploding string");
	
	$part1 = $split[0];
	$part2 = $split[1];
	
	// Write the new data to the temp file...
	
	$menu = '<? eval($menu_code); ?>';
	$addmenu = $part1.$menu.$part2;
	
	if (!$handle = fopen($filename, 'w+')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   // Write new content to our opened file.
   $contents = $addmenu;
   if (fwrite($handle, $contents) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
	
	//echo $addmenu;
	//echo $contents_of_array;
	//echo $contents."<br>";
	
	$replace = str_replace($string,$makemenu,$contents);
	
	$replace = str_replace("'","\'",$contents);
		
	$sql = "UPDATE {$ezine_id}_content 
			SET page_content = '$replace' 
			WHERE page_id = '1' ";
		
	$result = mysql_query($sql) or die("Could not perform the update query - ".mysql_error());
	
	$sql = "SELECT page_content 
			FROM {$ezine_id}_content 
			WHERE page_id = '1'";
	
	$result = mysql_query($sql) or die("Couldn't perform the final query - ". mysql_error());
	
	$row = mysql_fetch_row($result);
	
	$contents = $row[0];
	
	echo $contents;
	
	fclose($handle);
Any ideas, anyone? thanks!
Post Reply