Current location: Hot Scripts Forums » Programming Languages » PHP » Updating/Writing into File using PHP


Updating/Writing into File using PHP

Reply
  #1 (permalink)  
Old 06-04-09, 09:08 PM
samara samara is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Updating/Writing into File using PHP

Hello Sir,

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

Thanks!!
Reply With Quote
  #2 (permalink)  
Old 06-05-09, 12:49 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
PHP Code:

<?php
function addMetaTag($file$separator$metaTag)
{
 
$file_contents file_get_contents($file);
 
$file_parts explode($separator$file_contents);
 
$newFile $file_parts[0].$metaTag.$separator.$file_parts[1];
 
file_put_contents($file$newFileLOCK_EX);
 }

if(
$_REQUEST['comment'])
{
 
$file REPORTS_DIR."/$FILENAME";
 
$separator "</head>";
 
$metaTag "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n";
 
addMetaTag($file$separator$metaTag);
 }

// See the results. //
echo "<span style='font-size:24px;font-weight:bold;'><u>New file</u> :</span><br />".nl2br(htmlspecialchars(file_get_contents($file)));
?>
__________________
Jerry Broughton

Last edited by job0107; 06-05-09 at 01:18 AM.
Reply With Quote
  #3 (permalink)  
Old 06-05-09, 08:50 AM
samara samara is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot for your reply sir, it was very helpful!!

My Next step is:
Now user has enetered the comment and he posted, its going into the file:

Now the files meta Tag is like this:
<meta name="comment" content="This is the first">

If user enters a new comment(this is the second comment), it should append to the first comment in a new line like this:

<meta name="comment" content="This is the first
This is the second comment">

Any help please

Code:
function addMetaTag($file, $separator, $metaTag)
{
 $file_contents = file_get_contents($file);
 $file_parts = explode($separator, $file_contents);
 $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
 file_put_contents($file, $newFile, LOCK_EX);
 }

if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
 $file = REPORTS_DIR."/$FILENAME";
 $separator = "</head>";
 $metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n";
 addMetaTag($file, $separator, $metaTag);
 }

// See the results. //
//echo "<span style='font-size:24px;font-weight:bold;'><u>New file</u> :</span><br />".nl2br(htmlspecialchars(file_get_contents($file)));

  print "<html>";
  print "<form method='POST'>";
  print "<div class='normal_header'>Comment:</div><textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30' ></textarea/></br><input type='submit' name='submit' value='Post' />";
  print "</form>";
  print "</html>";
Reply With Quote
  #4 (permalink)  
Old 06-06-09, 01:56 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by samara View Post
Thanks a lot for your reply sir, it was very helpful!!

My Next step is:
Now user has enetered the comment and he posted, its going into the file:

Now the files meta Tag is like this:
<meta name="comment" content="This is the first">

If user enters a new comment(this is the second comment), it should append to the first comment in a new line like this:

<meta name="comment" content="This is the first
This is the second comment">

Any help please

Code:
function addMetaTag($file, $separator, $metaTag)
{
 $file_contents = file_get_contents($file);
 $file_parts = explode($separator, $file_contents);
 $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
 file_put_contents($file, $newFile, LOCK_EX);
 }

if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
 $file = REPORTS_DIR."/$FILENAME";
 $separator = "</head>";
 $metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n";
 addMetaTag($file, $separator, $metaTag);
 }

// See the results. //
//echo "<span style='font-size:24px;font-weight:bold;'><u>New file</u> :</span><br />".nl2br(htmlspecialchars(file_get_contents($file)));

  print "<html>";
  print "<form method='POST'>";
  print "<div class='normal_header'>Comment:</div><textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30' ></textarea/></br><input type='submit' name='submit' value='Post' />";
  print "</form>";
  print "</html>";
PHP Code:

<?php session_start(); ?>
<html>
<head>
<title>Meta tag creator</title>
</head>
<body>
<?php
$_SESSION
["tagName"] = empty($_POST["tagName"]) ? "" $_POST["tagName"];

// Function adds meta tag to file. //
function addMetaTag($file$separator$metaTag)
{
 
$file_parts explode($separatorfile_get_contents($file));
 
$newFile $file_parts[0].$metaTag.$separator.$file_parts[1];
 
file_put_contents($file$newFileLOCK_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>
__________________
Jerry Broughton

Last edited by job0107; 06-06-09 at 02:08 AM.
Reply With Quote
  #5 (permalink)  
Old 06-07-09, 03:59 PM
samara samara is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot for your reply, but you have totally changed everything, this is of no help for me,

i was not even able to understand which function is actually appending to the existing content, the $metatag name is so confusing!!

This is my code now!!

Code:
function addMetaTag($file, $separator, $metaTag)
{
 $file_contents = file_get_contents($file);
 $file_parts = explode($separator, $file_contents);
 $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
 file_put_contents($file, $newFile, LOCK_EX);
 }

if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
 $file = REPORTS_DIR."/$FILENAME";
 $separator = "</head>";
 $metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n";
 addMetaTag($file, $separator, $metaTag);
 }

// See the results. //
//echo "<span style='font-size:24px;font-weight:bold;'><u>New file</u> :</span><br />".nl2br(htmlspecialchars(file_get_contents($file)));

  print "<html>";
  print "<form method='POST'>";
  print "<div class='normal_header'>Comment:</div><textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30' ></textarea/></br><input type='submit' name='submit' value='Post' />";
  print "</form>";
  print "</html>";
Now my task is!! when ever the page loads:

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!!
Reply With Quote
  #6 (permalink)  
Old 06-08-09, 12:27 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If you just want to get the meta data from $metadata['comment'], then this will work.
PHP Code:

<?php
//detect encoding
function convert_encoding($file,$file_contents)
{
 
$CONV_TEXT = @iconv("UTF-16""UTF-8"$file_contents);   //If encoding cannot be detected, assume UTF-16 and convert to UTF-8
 
$find_string '<META http-equiv="Content-Type" content="text/html; charset=UTF-16">';     //Replace UTF-16 encoding parameter in report header with UTF-8
 
$replace_string '<META http-equiv="Content-Type" content="text/html; charset=UTF-8">';
 return 
str_replace($find_string$replace_string$CONV_TEXT);
 }

function 
parse_metadata($file)
{
 
$file_contents = @file_get_contents($file);
 
$TEXT mb_detect_encoding($file_contents) ? $file_contents convert_encoding($file,$file_contents);
 
//grab data from each metatag
 
$metadata = array();
 if(
$TEXT)
 {
  if(
preg_match_all('/<[\s]*meta[\s]*name="?' '([^>"]*)"?[\s]*' 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si'$TEXT$matchesPREG_SET_ORDER))
  {
   for(
$i=0;$i<count($matches[0]);$i++){$metadata[$matches[$i][1]] = $matches[$i][2];}
   }
  }
 return 
$metadata;
 }

function 
no_metadata()
{
 GLOBAL 
$weight;
 
// Set formatting for error message
 
$weight "font-weight:bold;font-size:16px;color:#f00;padding:10px;";
 return array(
'comment' => "Alert!!!\r\nNo metadata available.");
 }
 
// Initialize error formatting variable
$weight "";

// Get the metadata and display it in a textarea element
$metadata parse_metadata(REPORTS_DIR."/$FILENAME") ? parse_metadata(REPORTS_DIR."/$FILENAME") : no_metadata();
echo 
"<html>
      <head>
      <title></title>
      </head>
      <body>
       <form method='POST'>
        <div class='normal_header'>Comment:</div>
        <textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30' style='"
.$weight."'>"
         
.$metadata['comment'].
       
"</textarea/>
        </br>
        <input type='submit' name='submit' value='Post' />
       </form>
       </body>
       </html>"
;
?>
__________________
Jerry Broughton

Last edited by job0107; 06-08-09 at 12:43 AM.
Reply With Quote
  #7 (permalink)  
Old 06-08-09, 12:05 PM
samara samara is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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!!

Code:
<?php
function addMetaTag($file, $separator, $metaTag)
{
 $file_contents = file_get_contents($file);
 $file_parts = explode($separator, $file_contents);
 $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
 file_put_contents($file, $newFile, LOCK_EX); 
 }

if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
 $metadata = array();
 $file = REPORTS_DIR."/$FILENAME";
 $file_contents = file_get_contents($file);
 $separator = "</head>";

        if ($file_contents)
        {
            if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uims', $file_contents, $matches))
            {
                for($i = 0; $i < count($matches[1]); $i++)
                    $metadata[$matches[1][$i]] = $matches[2][$i];
                    if(isset($metadata['comment']))
                    {    
                    //$metadata['comment'] = $metadata['comment'].$_REQUEST['comment'];
                    echo $_REQUEST['comment'];
                    echo $metadata['comment'];
                    $metaTag = "<meta name=\"comment\" content=\"".$metadata['comment']."\n".$_REQUEST['comment']."\">\r\n"; ---------------------------------------->> This is where i am talking about!!
                    addMetaTag($file, $separator, $metaTag); 
                    }
                     else
                    {
                    $metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n"; 
                    addMetaTag($file, $separator, $metaTag);   
                    }
            }
        }
}

$metadata = array();
$file = REPORTS_DIR."/$FILENAME";
$file_contents = file_get_contents($file);

        if ($file_contents)
        {
            if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uims', $file_contents, $matches))
            {
                for($i = 0; $i < count($matches[1]); $i++)
                    $metadata[$matches[1][$i]] = $matches[2][$i];
                    if(isset($metadata['comment']))
                    {
                        echo "<html>
                              <head>
                              <title></title>
                              </head>
                              <body>
                              <form method='POST'>
                              <div class='normal_header'>Comment:</div>
                              <textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30'>" .$metadata['comment']. "</textarea/>
                              </br>
                              <input type='submit' name='submit' value='Post' />
                              </form>
                              </body>
                              </html>"; 
                    }
                    else
                    {
                     echo "<html>
                           <head>
                           <title></title>
                           </head>
                           <body>
                           <form method='POST'>
                           <div class='normal_header'>Comment:</div>
                           <textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30'>You are Entering the first comment</textarea/>
                           </br>
                           <input type='submit' name='submit' value='Post' />
                           </form>
                           </body>
                           </html>";    
                    }                   
            }
        }   
  ?>
pls lemme knw if u need more xplanation
Reply With Quote
  #8 (permalink)  
Old 06-08-09, 03:04 PM
samara samara is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot for your help sir, i was able to do it!!

$find_string = $metadata['comment'];
$replace_string = $metadata['comment']."\n".$_REQUEST['comment'];
//echo $replace_string;
$holdcontents2 = str_replace($find_string, $replace_string, $file_contents);
//echo $file_contents;
file_put_contents($file, $holdcontents2);

Now my task is 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:

Code:
<span style="color:#0000FF; font-family:Arial; font-size:10pt; font-weight:bold; ">Date/Time: </span><span style="font-family:Arial; font-size:10pt; font-weight:bold; ">Date05-27-2009-Time15-13-31</span><br>
this is the only distinct way i can search for the place: i have to insert the comments exactly after </span><br> at the end.

I have to open file, search through the text for Date/Time and walk through the text until i reach the first </span><br>.

And then insert the comment may be by putting a strpos there or something like that etc.

Can you please help me on this!!

the comment might look like this:
Code:
<span style="color:#0000FF; font-family:Arial; font-size:10pt; font-weight:bold; ">Comment: </span><span style="font-family:Arial; font-size:10pt; font-weight:bold; ">$metadata['comment'] [or] $replace_string</span><br>
if you have some other suggestions or ways of doing it, lemme knw!!

Thanks!!
Reply With Quote
  #9 (permalink)  
Old 06-09-09, 12:23 PM
samara samara is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
I have done it!!

now i have only one problem, in the code below:

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

Code:
function addMetaTag($file, $separator, $metaTag)
{
 $file_contents = file_get_contents($file);
 $file_parts = explode($separator, $file_contents);
 $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
 file_put_contents($file, $newFile, LOCK_EX); 
}

function addcommentintohtml($replace_string, $FILENAME)
{
 $file1 = REPORTS_DIR."/$FILENAME";
 $file_contents = file_get_contents($file1);
 $seperator1 = "Date/Time:";
 $DTstring = explode($seperator1, $file_contents);
 $seperator2 = "<br>";
 $seperator3 = "<span style='color:#0000FF; font-family:Arial; font-size:10pt; font-weight:bold;'>Comment: </span>";
 $SPBstring = explode($seperator2, $DTstring[1]);
 $newfile = $DTstring[0].$seperator1.$SPBstring[0].$seperator2.$seperator3.$replace_string.$seperator2.$seperator2.$SPBstring[1].$seperator2.$seperator2.$SPBstring[2].$SPBstring[3].$SPBstring[4];
     return $newfile; 
}

if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
 $metadata = array();
 $file = REPORTS_DIR."/$FILENAME";
 $file_contents = file_get_contents($file);
 $separator = "</head>";

        if ($file_contents)
        {
            if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uims', $file_contents, $matches))
            {
                for($i = 0; $i < count($matches[1]); $i++)
                    $metadata[$matches[1][$i]] = $matches[2][$i];
                    if(isset($metadata['comment']))
                    {                   
                    $find_string = $metadata['comment'];
                    //$replace_string = $metadata['comment']."\n".$_REQUEST['comment'];
                    $replace_string = $_REQUEST['comment'];
                    $holdcontents2 = str_replace($find_string, $replace_string, $file_contents);            
                    file_put_contents($file, $holdcontents2);                   
                    $add = addcommentintohtml($replace_string, $FILENAME);                          
                    }
                    else
                    {
                    $metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n"; 
                    addMetaTag($file, $separator, $metaTag);
                    $replace_string = $_REQUEST['comment'];
                    $add = addcommentintohtml($replace_string, $FILENAME);
                    //echo $add;
                    $file1 = REPORTS_DIR."/$FILENAME";
                    file_put_contents($file1, $add, LOCK_EX);   
                    }
            }
        }
} 

$metadata = array();
$file = REPORTS_DIR."/$FILENAME";
$file_contents = file_get_contents($file);

        if ($file_contents)
        {
            if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uims', $file_contents, $matches))
            {
                for($i = 0; $i < count($matches[1]); $i++)
                    $metadata[$matches[1][$i]] = $matches[2][$i];
                    if(isset($metadata['comment']))
                    {
                        echo "<html>
                              <head>
                              <title></title>
                              </head>
                              <body>
                              <form method='POST'>
                              <div class='normal_header'>Comment:</div>
                              <textarea name = 'comment' id = 'txtarea1' rows = '5' cols = '50'>" .$metadata['comment']. "</textarea/>
                              </br>
                              <input type='submit' name='submit' value='Post' />
                              </br>
                                                                                                                                                                                                                                             
                              </form>                             
                              </body>
                              </html>";
                              echo $DisplayReport;       ---------------------------------->> This function                      
                    }
                    else
                    {
                        echo "<html>
                              <head>
                              <title></title>
                              </head>
                              <body>
                              <form method='POST'>
                              <div class='normal_header'>Comment:</div>
                              <textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30'>You are Entering the first comment</textarea/>
                              </br>
                              <input type='submit' name='submit' value='Post' />
                              </br>
                             
                              </form>
                              </body>
                              </html>";                            
                    }                    
            }
        }   
  ?>
Reply With Quote
  #10 (permalink)  
Old 06-09-09, 11:02 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by samara View Post
I have done it!!

now i have only one problem, in the code below:

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

Code:
function addMetaTag($file, $separator, $metaTag)
{
 $file_contents = file_get_contents($file);
 $file_parts = explode($separator, $file_contents);
 $newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
 file_put_contents($file, $newFile, LOCK_EX); 
}

function addcommentintohtml($replace_string, $FILENAME)
{
 $file1 = REPORTS_DIR."/$FILENAME";
 $file_contents = file_get_contents($file1);
 $seperator1 = "Date/Time:";
 $DTstring = explode($seperator1, $file_contents);
 $seperator2 = "<br>";
 $seperator3 = "<span style='color:#0000FF; font-family:Arial; font-size:10pt; font-weight:bold;'>Comment: </span>";
 $SPBstring = explode($seperator2, $DTstring[1]);
 $newfile = $DTstring[0].$seperator1.$SPBstring[0].$seperator2.$seperator3.$replace_string.$seperator2.$seperator2.$SPBstring[1].$seperator2.$seperator2.$SPBstring[2].$SPBstring[3].$SPBstring[4];
     return $newfile; 
}

if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
 $metadata = array();
 $file = REPORTS_DIR."/$FILENAME";
 $file_contents = file_get_contents($file);
 $separator = "</head>";

        if ($file_contents)
        {
            if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uims', $file_contents, $matches))
            {
                for($i = 0; $i < count($matches[1]); $i++)
                    $metadata[$matches[1][$i]] = $matches[2][$i];
                    if(isset($metadata['comment']))
                    {                   
                    $find_string = $metadata['comment'];
                    //$replace_string = $metadata['comment']."\n".$_REQUEST['comment'];
                    $replace_string = $_REQUEST['comment'];
                    $holdcontents2 = str_replace($find_string, $replace_string, $file_contents);            
                    file_put_contents($file, $holdcontents2);                   
                    $add = addcommentintohtml($replace_string, $FILENAME);                          
                    }
                    else
                    {
                    $metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n"; 
                    addMetaTag($file, $separator, $metaTag);
                    $replace_string = $_REQUEST['comment'];
                    $add = addcommentintohtml($replace_string, $FILENAME);
                    //echo $add;
                    $file1 = REPORTS_DIR."/$FILENAME";
                    file_put_contents($file1, $add, LOCK_EX);   
                    }
            }
        }
} 

$metadata = array();
$file = REPORTS_DIR."/$FILENAME";
$file_contents = file_get_contents($file);

        if ($file_contents)
        {
            if(preg_match_all('/<meta\s*name\s*=\s*"(.*)"\s*content\s*=\s*"(.*)".*>/Uims', $file_contents, $matches))
            {
                for($i = 0; $i < count($matches[1]); $i++)
                    $metadata[$matches[1][$i]] = $matches[2][$i];
                    if(isset($metadata['comment']))
                    {
                        echo "<html>
                              <head>
                              <title></title>
                              </head>
                              <body>
                              <form method='POST'>
                              <div class='normal_header'>Comment:</div>
                              <textarea name = 'comment' id = 'txtarea1' rows = '5' cols = '50'>" .$metadata['comment']. "</textarea/>
                              </br>
                              <input type='submit' name='submit' value='Post' />
                              </br>
                                                                                                                                                                                                                                             
                              </form>                             
                              </body>
                              </html>";
                              echo $DisplayReport;       ---------------------------------->> This function                      
                    }
                    else
                    {
                        echo "<html>
                              <head>
                              <title></title>
                              </head>
                              <body>
                              <form method='POST'>
                              <div class='normal_header'>Comment:</div>
                              <textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30'>You are Entering the first comment</textarea/>
                              </br>
                              <input type='submit' name='submit' value='Post' />
                              </br>
                             
                              </form>
                              </body>
                              </html>";                            
                    }                    
            }
        }   
  ?>
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.
__________________
Jerry Broughton

Last edited by job0107; 06-09-09 at 11:06 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the file at php? tharsoe PHP 3 07-22-08 02:42 AM
PHP File Upload/User Manager Script Needed Computra Script Requests 2 11-09-05 02:39 PM
PHP based File Tracker project valtea Script Requests 0 09-08-05 02:04 PM
move files around an ftp server, with php file upload script? wapchimp PHP 2 12-19-04 07:27 AM
PHP code on a remote file tonniar .rm PHP 2 05-24-04 02:32 AM


All times are GMT -5. The time now is 08:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.