Jump to content

Creating a custom CMS...


Rifts
 Share

Recommended Posts

Is there a way to use PHP to edit html without having to store it in my DB?

 

For example I would like someone to be able to edit a paragraph on the page, its wrapped in a <div class="editable"> now is there a way to have PHP detect that div and have an textarea to edit it directly without having to store anything in a DB?

 

I hope that makes sense if it doesn't let me know!

 

Thanks

 

 

 

 

 

 

Link to comment
Share on other sites

You'll have to elaborate a little more. If it isn't stored in a database, would be stored elsewhere? If not, what would be the point in editing? It will only update source for that page view.

 

To be honest, this is one of those times I would use javascript (jquery). With a couple of lines of code you can easily create a textarea containing the sourcode within that div.

 

Something like:

 


$(".editable").each(function(){
  oldContent = $(this).html();
  $(this).html('<textarea name="something">'+oldContent+'</textarea>');
});

 

That will do what you want, if I have read you right.

Link to comment
Share on other sites

So that will actually update the source html for the page and save it?

 

to elaborate more...

 

I have a ton of pages and the client wants to be able to update them whenever he wants. So I created a login so when he logs in all the stuff he can edit will have a littel "edit" tab on them (the "editable" divs).

 

I would like him to be able to click edit and it would open a textarea with the current text in the div and allow him to change it and hit save. I'm guessing hitting save would have to like reupload the page to ftp or something to get the new updates source there?

Link to comment
Share on other sites

In your form processing code you would read, modify, write the actual page that is being edited. I recommend that you make a .bak backup copy of the original page before your perform the modify/write step (or even save x back up copies .bk1, .bk2, ...) so that if anything goes wrong you can recover a previous version.)

Link to comment
Share on other sites

The following code demonstrates how you might accomplish the basic edit logic -

<?php
// locate <div class="editable"></div> on a page and allow it to be edited and then saved back to the page

// echo or return a string that may contain html
function echo_html($string,$return=false){
if(!$return){
	echo htmlentities($string,ENT_QUOTES);
} else {
	return htmlentities($string,ENT_QUOTES);
}
}

// ref: http://forums.devnetwork.net/viewtopic.php?f=38&t=102670 for the following
// regex to extract contents from <div class="editable">contents</div>
$pattern_long = '{           # recursive regex to capture contents of specific DIV
(<div\s+[^>]*?class=["|\']editable["|\'][^>]*>)   # match the DIV opening tag
	(                                   # capture DIV contents into $1
		(?:                               # non-cap group for nesting * quantifier
			(?: (?!<div[^>]*>|</div>). )++  # possessively match all non-DIV tag chars
			|                                 # or
			<div[^>]*>(?1)</div>            # recursively match nested <div>xyz</div>
		)*                                # loop however deep as necessary
	)                                   # end group 1 capture
(</div>)                               # match the DIV closing tag
}six';  // single-line (dot matches all), ignore case and free spacing modes ON

session_start();

$_SESSION['admin'] = true; // ********* fake for testing *********

// check if visitor is logged in as an admin... Replace with your actual log in checking logic
if(!isset($_SESSION['admin'])){
header('login.php'); // send them somewhere else...
exit;
}
// the current visitor can access this page

// get/make a list of files that can be edited - either via array, glob, database, or by indexing the site
$files = array('index.html','news.html'); // dummy list of files for testing purposes

// set default value(s) as needed
$_SESSION['current_file'] = isset($_SESSION['current_file']) ? $_SESSION['current_file'] : '';

// 'select file' form processing code
if(isset($_POST['select_file'])){
$_SESSION['current_file'] = $_POST['file'];
}

// 'div edit' form processing code
if(isset($_POST['div_edit'])){
// check if there is a filename (if the form was used to submit to here there will be)
if(!empty($_POST['filename'])){
	// check if file actually/still exists
	if(!file_exists($_POST['filename'])){
		echo "The file: {$_POST['filename']} does not exist!";
	} else {
		// file exists, get the current content
		$content = file_get_contents($_POST['filename']);
		// check that the <div> exists
		$matchcount = preg_match_all($pattern_long, $content, $matches);
		if ($matchcount > 0) {
			// an editable <div> exists, replace the content in the <div>
			$content = preg_replace($pattern_long, "$1{$_POST['textarea']}$3", $content, 1);
			if($content !== NULL){
				// write changes to file
				if(file_put_contents($_POST['filename'],$content) === false){
					echo "Could not save changes to the file!";
				} else {
					echo "The file: {$_POST['filename']}, was successfully updated!";
				}
			} else {
				echo "preg_replace failed due to an error!<br />";
			}
		} else {
			echo_html('No editable <div> found!');
		}
	}
}
} // end div_edit form processing

// make a select list of files
$select = "<select name='file' onchange='this.form.submit();'>\n<option value=''>Select a file to edit!</option>\n";
foreach($files as $file){
$selected = ($_SESSION['current_file'] == $file) ? " selected = 'selected'" : '';
$select .= "<option value='$file'$selected>$file</option>\n";
}
$select .= "</select>\n";
// output the select file form
?>
<form action='' method='post'>
Current File:
<input type='hidden' name='select_file'>
<?php echo $select; ?><br />
</form>
<?php
// get and display the editable div from the currently selected file
if(!empty($_SESSION['current_file'])){
if(!file_exists($_SESSION['current_file'])){
	echo "The file: {$_SESSION['current_file']} does not exist!";
} else {
	$content = file_get_contents($_SESSION['current_file']);
	$matchcount = preg_match_all($pattern_long, $content, $matches);
	if ($matchcount > 0) {
		echo echo_html("The following editable <div> was found:",true) . "<br />\n";
		echo "<form action='' method='post'>";
		echo "<input type='hidden' name='div_edit'>";
		echo "<input type='hidden' name=filename value='{$_SESSION['current_file']}'";
		echo "<textarea name='textarea' rows='10' cols='80'>". echo_html($matches[2][0],true) . "</textarea><br />\n";
		echo "<input type='submit' name='submit' value='Save Changes'";
		echo "<form>";
	} else {
		echo_html('No editable <div> found!');
	}
}
}
?>

Link to comment
Share on other sites

Thanks for helping me about with this post

 

I looked over the code and i'm trying it now its amazing the only thing is when its creating the textarea i'ts not editable. It shows up and says

 

The following editable <div> was found:

bla bla bla

 

but I can not change it. Looking at the code it looks fine so i'm kinda stuck.

 

what do you think?

 

thanks

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.