This is my Task!!
When ever user comment and clicks add, i need to open an HTML file from a directory(i need to open an existing file which i know), i need to locate a string </head> and i need to put the value of the text field there like this:
Comment is the name of the text field the user has to enter into to enter the text into the file!!
<meta name="comment" content="value in the text field ie ($_REQUEST['comment']">
now i have this
if($_REQUEST['comment'])
{
$file_contents = file_get_contents(REPORTS_DIR."/$FILENAME");
$find_string = '</head>';
now how to add the new string <meta name="comment" content="value in the text field ie ($_REQUEST['comment']"> above the </head>
Any help would be greatly appreciated!! If you need anyomore info, i can explain you in detail
// Function adds meta tag to file. // function addMetaTag($file, $separator, $metaTag) { $file_parts = explode($separator, file_get_contents($file)); $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1]; file_put_contents($file, $newFile, LOCK_EX); }
// Setup variables. // $tagArray = array("comment","Content-Type","description","language","keywords","ROBOTS"); // Add meta tag names to this array seperated by commas. // $file = REPORTS_DIR."/$FILENAME"; // Enter name of file to add meta tags. //
// Do not edit these variables. // $errorMsg = ""; $separator = "</head>"; $metaName = $_SESSION["tagName"]; //////////////////////////////////
// Was the form submitted? // if(!empty($_POST["submit"])) { // Was a meta tag selected? // if($_POST["tagName"]) { $metaName = $_POST["tagName"]; // Was any content entered? // if($_POST['content']) { $_SESSION["content"] .= $metaName == "comment" ? $_POST['content'] . "\r\n" : $_POST['content'] . ", "; echo nl2br(htmlspecialchars("<meta name=\"".$metaName."\" content=\"".substr($_SESSION["content"],0,strlen($_SESSION["content"])-2))."\">"). "<br /> <form action='#' method='post'> <input type='hidden' name='tagName' value='".$_SESSION["tagName"]."' /> Add more content? <input type='submit' name='yes' value='Yes' /> <input type='submit' name='no' value='No' ></form>"; die(); } else{$errorMsg = "ERROR! - You must enter the <u>".$_SESSION["tagName"]."</u> content.";} // Error message, no content entered. // } else{$errorMsg = "ERROR! - You must select a meta tag.";} // Error message, no meta tag selected. // }
// Finished creating meta tag and add it to the file. // if(!empty($_POST['no'])) { $content = $_SESSION["content"] ? substr($_SESSION["content"],0,strlen($_SESSION["content"])-2) : ""; $metaTag = "<meta name=\"".$metaName."\" content=\"".$content."\">\r\n"; addMetaTag($file, $separator, $metaTag); unset($_SESSION["content"]); unset($_SESSION["tagName"]); // See the results. // echo "<div style='font-size:24px;font-weight:bold;'><u>New file</u> :</div> <br /> <div style='border:1px solid #000;padding:10px;'>".nl2br(htmlspecialchars(file_get_contents($file)))."</div> <br /> <form action='#' method='post'> Add another Meta tag? <input type='submit' value='Yes' /> <input type='submit' name='quit' value='No' /> </form>"; die(); }
// Finished using Meta tag creator. // if(!empty($_POST["quit"])) { echo "Thank you for using <b>Meta tag creator.</b>"; die(); } ?> <div> <p style="font-size:24px;color:#00f;"><u>Meta tag creator.</u></p> <?php // Echo any error messages. // echo "<p style='color:#f00;font-size:18px;'><b>$errorMsg</b></p>"; ?> <form action="#" method='POST'> <div class='normal_header'>Tag Name:</div> <select name="tagName"> <option value="">Select a meta tag name</option> <?php for($i=0;$i<count($tagArray);$i++) { $selected = $tagArray[$i] == $_SESSION["tagName"] ? "selected" : ""; echo "<option value='".$tagArray[$i]."' $selected>".$tagArray[$i]."</option>"; } ?> </select> <br /> <div class='normal_header'>Content:</div> <textarea name = 'content' id = 'txtarea1' rows = '3' cols = '30' ><?php echo $_POST['content']; ?></textarea> <br /> <input type='submit' name='submit' value='Post' /> </form> </div> </body> </html>
If the metatag comment is not present in the file or this is the first time user is entering a comment, then there should not be any value in the text area, it should be blank.
so when now user enters something then the metatag is created, this is working fine for me!!
suppose
if there is a meta tag present already in the file, user has already entered a comment just before or long back, the value of the comment ie the content in the metatag should be displayed at the intial value of the text box.
<meta name="comment" content="This is the first">
so in this case when page loads, the value in the text area should be This is the first comment and just a post button.
for this i have written a metatag parser:
Code:
$file_contents = file_get_contents(REPORTS_DIR."/$FILENAME");
//detect encoding
if(!mb_detect_encoding($file_contents)) {
//If encoding cannot be detected, assume UTF-16 and convert to UTF-8
$CONV_TEXT = @iconv("UTF-16", "UTF-8", $file_contents);
//Replace UTF-16 encoding parameter in report header with UTF-8
$find_string = '<META http-equiv="Content-Type" content="text/html; charset=UTF-16">';
$replace_string = '<META http-equiv="Content-Type" content="text/html; charset=UTF-8">';
$TEXT = str_replace($find_string, $replace_string, $CONV_TEXT);
}
else {
$TEXT = $file_contents;
}
function parse_metadata($TEXT)
{
//grab data from each metatag
$metadata = array();
if ($TEXT)
{
if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uim', $TEXT, $matches))
{
for($i = 0; $i < count($matches[1]); $i++)
$metadata[$matches[1][$i]] = $matches[2][$i]; // as far as i know here, when the function parses the metatag <meta name="" content="This is the first comment">, ie $metadata['comment'] = This is the first comment
}
}
return $metadata;
}
if(isset($metadata['comment']))
{
now i want the value of $metadata['commen't] to be stored as the value of text area, this happens when page loads first and when the metatag is present already, but i am confused when to call this if(isset($metadata['comment'])) and how to assign this php variable value to textarea value!!
}
so now textarea has the initial value as the content or previous user entered comment or by default the textarea shows the comments that are present for the reports.
Now if user adds a new comment, it should append to the existing comment and should display whole updated comment (content value from metatag) in the textarea!!
i think i have pretty much done with it, but confused in calling the functions. i need your help.
If you haven't understood please lemme know. I will try to xplain further.
Just a brief summary.:
$metadata['comment']) is having the value of the comment that is present in the file.
$_REQUEST['comment'] is having the value of the text area after user addas and before clicking the post button
As of now, my first task would be completed if i was able to implement, showing the value of metatag content in the textarea as soon as page displays!!
This code was really helpful!!
i added up a little extra!!
Now my code looks like this!!
Actually, everything works fine now, the only problem is (I have noted this line in the code below as " --------------->> This is where i am talking about"):
When user adds a second comment, a new meta tag is getting created, but i want the new comment to get appended to the existing one. I know you gave it in your last code, but the tagname confuses me a lot and i was not able to see how you are appending to the existing comment. I tried making chnages according to my situation but its making my result worse.
Right now, lets take an example:
User enters 3 comments at different times namely: hai how are you i am fine great its working good
after executing for all these different times, now the file looks like this:
<meta name="comment" content="hai how are you">
<meta name="comment" content="hai how are you
i am fine">
<meta name="comment" content="hai how are you
i am fine
great its working good">
[CODE]<?php
function addMetaTag($file, $separator, $metaTag)
But i want these three different comemnts to create only one metatag:
<meta name="comment" content="hai how are you
i am fine
great its working good">, just update the existing one, it sould not craete a new one.
Any help please, can you help me by adding changes to exisitng code, if i do this, my task will be done!!
I need another help: In the same file on which i have been working, where i have updated the comments until now i have to pus the same comments in another place!!
I am aware of doing this: But the place where i have to enter is a string that looks like this:
The place marked as ------------------>> at this function:
i want echo $DisplayReport; to be run whenever te new comment is posted, everything works fine now: I want to refresh the page when ever the addcommentintohtml() function gets executed.
I tried so many online resources nothing is of no help, you can also give your own suggestion may be like php refresh function etc
Thanks!!
I sthere anyway i can do that, i dont want to refresh page after every n seconds, i want it to refresh only when comment is added or addcommentintohtml() is executed
The place marked as ------------------>> at this function:
i want echo $DisplayReport; to be run whenever te new comment is posted, everything works fine now: I want to refresh the page when ever the addcommentintohtml() function gets executed.
I tried so many online resources nothing is of no help, you can also give your own suggestion may be like php refresh function etc
Thanks!!
I sthere anyway i can do that, i dont want to refresh page after every n seconds, i want it to refresh only when comment is added or addcommentintohtml() is executed
Well, I am glad you figured it out.
But I am going to be very blunt with you.
I see you didn't learn very much.
You have a lot of redundancy in you code.
Why are you getting the contents of the file three times?
Why don't you get the contents of the file once in the beginning of the program and get it over with?
You need to rethink your logic and rewrite your code so it makes logical sense.
Then and only then, will you be able to see a way, how to do what you want.
I am not going to keep rewriting your code all the time unless you are willing to compensate me for my time.
But I think sense you should be learning something you should try and figure it out yourself, otherwise I charge $40 per hour for my time.