Rifts Posted December 10, 2010 Share Posted December 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/ Share on other sites More sharing options...
Anti-Moronic Posted December 10, 2010 Share Posted December 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1145355 Share on other sites More sharing options...
Rifts Posted December 10, 2010 Author Share Posted December 10, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1145365 Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2010 Share Posted December 10, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1145368 Share on other sites More sharing options...
Rifts Posted December 10, 2010 Author Share Posted December 10, 2010 ok awesome thanks for the input could you give me a little push coding wise? Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1145370 Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2010 Share Posted December 11, 2010 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!'); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1145794 Share on other sites More sharing options...
Rifts Posted December 13, 2010 Author Share Posted December 13, 2010 wow thanks for the long amazing reply! I'm gunna try and get this working today Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1146666 Share on other sites More sharing options...
Rifts Posted December 13, 2010 Author Share Posted December 13, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221216-creating-a-custom-cms/#findComment-1146673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.