php-general Digest 28 Jun 2010 00:29:58 -0000 Issue 6819

Topics (messages 306460 through 306469):

Re: Modifying Existing Text File From PHP Is Not Working
        306460 by: Ashley Sheridan
        306462 by: tedd
        306463 by: Alice Wei
        306464 by: Alice Wei
        306465 by: Ashley Sheridan
        306466 by: Alice Wei
        306467 by: Ashley Sheridan
        306468 by: Alice Wei

Brandon Rampersad wants to chat
        306461 by: Brandon Rampersad

Re: mysql case statement
        306469 by: David McGlone

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Sun, 2010-06-27 at 07:20 -0400, Alice Wei wrote:

> Hi, 
> 
> I have the code as in the following:
> 
> <?php
> 
> $name= "Test";
> $phone= "123-456-7890";
> $email = "myem...@mymail.com";
> $comments = "Test Only";
> 
> $string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone . 
> "</phone>\n<email>". 
>           $email . "</email>\n<comments>" .  $comments . 
> "</comments>\n</message>";
> 
> //If file exists. append, otherwise create
> $file = "messages.xml";
> $fh = fopen($file,"a");
> $lines = file($file);
> 
> //Output a line of the file until the end is reached
> foreach($lines as $line) {
> 
>    if (trim($line) == "<messages>") {
> 
>     $line = "<messages>" . $string; // If string, change it.
>     echo $line . "<br>";
>     fwrite($fh, $line);
>    
>    } 
>    else {
>    
>     // If not don't write anything
>    echo $line . "<br>";
> 
>    }
>   fclose($file); // Close the file.
>   }
> ?>
> 
> For some reason, it is writing out the lines, but it is not f-writing to the 
> file. Here is the file that is meant to be edited:
> 
> <messages>
> <message>
> <name>Me</name>
> <phone>123-456-7890</phone>
> <email>t...@test.com</email>
> <comments>This is my message board!</comments>
> </message>
> </messages>
> 
> Have I missed something here? How come the file cannot perform fwrite actions?
> 
> Thanks for your help.
>                                         
> _________________________________________________________________
> The New Busy is not the too busy. Combine all your e-mail accounts with 
> Hotmail.
> http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4


I assume you're opening the file in a text editor and you've not found
what you expect to be there?

Have you made sure that Apache has permission to write to the file? If
you created the file yourself and uploaded it, chances are it doesn't
have the right permissions.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
At 7:20 AM -0400 6/27/10, Alice Wei wrote:
Hi,

I have the code as in the following:

<?php

$name= "Test";
$phone= "123-456-7890";
$email = "myem...@mymail.com";
$comments = "Test Only";

$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone . "</phone>\n<email>". $email . "</email>\n<comments>" . $comments . "</comments>\n</message>";

//If file exists. append, otherwise create
$file = "messages.xml";
$fh = fopen($file,"a");
$lines = file($file);

//Output a line of the file until the end is reached
foreach($lines as $line) {

   if (trim($line) == "<messages>") {

    $line = "<messages>" . $string; // If string, change it.
    echo $line . "<br>";
    fwrite($fh, $line);
}
   else {
// If not don't write anything
   echo $line . "<br>";

   }
  fclose($file); // Close the file.
  }
?>

For some reason, it is writing out the lines, but it is not f-writing to the file. Here is the file that is meant to be edited:

<messages>
<message>
<name>Me</name>
<phone>123-456-7890</phone>
<email>t...@test.com</email>
<comments>This is my message board!</comments>
</message>
</messages>

Have I missed something here? How come the file cannot perform fwrite actions?

Thanks for your help.

Do you have permission to write to the file?

Check the permissions of the file.

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---
--- Begin Message ---
> Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> From: lexsi...@gmail.com
> Date: Sun, 27 Jun 2010 13:47:53 +0200
> To: aj...@alumni.iu.edu
> 
> You are closing your file too early. Close it outside the foreach.
> An other point : you should not 'edit' an XML file by this way ; use an XML 
> api like dom or simplexml
> 
> Regards
> 
> --
> Alexandre Simon
> http://alex.zybar.net
> 

Hi, 

  I did try changing the fclose to after all the brackets in my php file. On 
another note, I checked the permission of my folder that I was attempting to 
write in, which all users have write, modify, read and execute permissions. I 
don't see any 500 or permission denied errors, and I use IIS, so I don't use 
apache. 

  It didn't work though, the suggestion you provided on simpleXML looks 
promising, and yet I am writing not to the child, and plus I seem to be getting 
a 500 error. Another thing is, do I need to "save" the file after all this 
echoing is done using simpleXML? 

BTW, here is the code, which I am sure that I must have done something wrong 
here. Could anyone please help me out here?

Thanks for your help. 
Is it possible that I could do it the way I did it before by any chance?

<?php

$name= "Test";
$phone= "123-456-7890";
$email = "mym...@mail.com";
$comments = "Test Only";

//If file exists. append, otherwise create
$file = "messages.xml";

$xml = simplexml_load_file($file);
$xml-> messages[0]->addChild("message", "");
$xml -> message[0]->addChild("name",$name);
$xml -> message[0] ->addChild("phone",$phone);
$xml -> message[0] -> addChild("email",$email);
$xml-> message[0] -> addChild("comments",$comments);

echo $xml->asXML();

?>

Alice

> Le 27 juin 2010 à 13:20, Alice Wei <aj...@alumni.iu.edu> a écrit :
> 
> > 
> > Hi, 
> > 
> > I have the code as in the following:
> > 
> > <?php
> > 
> > $name= "Test";
> > $phone= "123-456-7890";
> > $email = "myem...@mymail.com";
> > $comments = "Test Only";
> > 
> > $string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone . 
> > "</phone>\n<email>". 
> >          $email . "</email>\n<comments>" .  $comments . 
> > "</comments>\n</message>";
> > 
> > //If file exists. append, otherwise create
> > $file = "messages.xml";
> > $fh = fopen($file,"a");
> > $lines = file($file);
> > 
> > //Output a line of the file until the end is reached
> > foreach($lines as $line) {
> > 
> >   if (trim($line) == "<messages>") {
> > 
> >    $line = "<messages>" . $string; // If string, change it.
> >    echo $line . "<br>";
> >    fwrite($fh, $line);
> > 
> >   } 
> >   else {
> > 
> >    // If not don't write anything
> >   echo $line . "<br>";
> > 
> >   }
> >  fclose($file); // Close the file.
> >  }
> > ?>
> > 
> > For some reason, it is writing out the lines, but it is not f-writing to 
> > the file. Here is the file that is meant to be edited:
> > 
> > <messages>
> > <message>
> > <name>Me</name>
> > <phone>123-456-7890</phone>
> > <email>t...@test.com</email>
> > <comments>This is my message board!</comments>
> > </message>
> > </messages>
> > 
> > Have I missed something here? How come the file cannot perform fwrite 
> > actions?
> > 
> > Thanks for your help.
> >                         
> > _________________________________________________________________
> > The New Busy is not the too busy. Combine all your e-mail accounts with 
> > Hotmail.
> > http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
                                          
_________________________________________________________________
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4

--- End Message ---
--- Begin Message ---

> Date: Sun, 27 Jun 2010 12:01:50 -0400
> To: aj...@alumni.iu.edu; php-gene...@lists.php.net
> From: tedd.sperl...@gmail.com
> Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> 
> At 7:20 AM -0400 6/27/10, Alice Wei wrote:
> >Hi,
> >
> >I have the code as in the following:
> >
> ><?php
> >
> >$name= "Test";
> >$phone= "123-456-7890";
> >$email = "myem...@mymail.com";
> >$comments = "Test Only";
> >
> >$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone 
> >. "</phone>\n<email>".
> >           $email . "</email>\n<comments>" .  $comments . 
> >"</comments>\n</message>";
> >
> >//If file exists. append, otherwise create
> >$file = "messages.xml";
> >$fh = fopen($file,"a");
> >$lines = file($file);
> >
> >//Output a line of the file until the end is reached
> >foreach($lines as $line) {
> >
> >    if (trim($line) == "<messages>") {
> >
> >     $line = "<messages>" . $string; // If string, change it.
> >     echo $line . "<br>";
> >     fwrite($fh, $line);
> >   
> >    }
> >    else {
> >   
> >     // If not don't write anything
> >    echo $line . "<br>";
> >
> >    }
> >   fclose($file); // Close the file.
> >   }
> >?>
> >
> >For some reason, it is writing out the lines, but it is not 
> >f-writing to the file. Here is the file that is meant to be edited:
> >
> ><messages>
> ><message>
> ><name>Me</name>
> ><phone>123-456-7890</phone>
> ><email>t...@test.com</email>
> ><comments>This is my message board!</comments>
> ></message>
> ></messages>
> >
> >Have I missed something here? How come the file cannot perform fwrite 
> >actions?
> >
> >Thanks for your help.
> 
> Do you have permission to write to the file?
> 
> Check the permissions of the file.
> 
> Cheers,
> 
> tedd
> 

I believe I do, since I set my folder permission of this script with modify, 
read, execute and delete. I don't see any errors regarding this either. I have 
changed my code to having the curly brace before I close the file handler, if 
that helps any. Yet, it is still not modifying the file. Have I missed 
something else here?

Thanks for your help.

Alice
> -- 
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
                                          
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3

--- End Message ---
--- Begin Message ---
On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:

> 
> > Date: Sun, 27 Jun 2010 12:01:50 -0400
> > To: aj...@alumni.iu.edu; php-gene...@lists.php.net
> > From: tedd.sperl...@gmail.com
> > Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> > 
> > At 7:20 AM -0400 6/27/10, Alice Wei wrote:
> > >Hi,
> > >
> > >I have the code as in the following:
> > >
> > ><?php
> > >
> > >$name= "Test";
> > >$phone= "123-456-7890";
> > >$email = "myem...@mymail.com";
> > >$comments = "Test Only";
> > >
> > >$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone 
> > >. "</phone>\n<email>".
> > >           $email . "</email>\n<comments>" .  $comments . 
> > >"</comments>\n</message>";
> > >
> > >//If file exists. append, otherwise create
> > >$file = "messages.xml";
> > >$fh = fopen($file,"a");
> > >$lines = file($file);
> > >
> > >//Output a line of the file until the end is reached
> > >foreach($lines as $line) {
> > >
> > >    if (trim($line) == "<messages>") {
> > >
> > >     $line = "<messages>" . $string; // If string, change it.
> > >     echo $line . "<br>";
> > >     fwrite($fh, $line);
> > >   
> > >    }
> > >    else {
> > >   
> > >     // If not don't write anything
> > >    echo $line . "<br>";
> > >
> > >    }
> > >   fclose($file); // Close the file.
> > >   }
> > >?>
> > >
> > >For some reason, it is writing out the lines, but it is not 
> > >f-writing to the file. Here is the file that is meant to be edited:
> > >
> > ><messages>
> > ><message>
> > ><name>Me</name>
> > ><phone>123-456-7890</phone>
> > ><email>t...@test.com</email>
> > ><comments>This is my message board!</comments>
> > ></message>
> > ></messages>
> > >
> > >Have I missed something here? How come the file cannot perform fwrite 
> > >actions?
> > >
> > >Thanks for your help.
> > 
> > Do you have permission to write to the file?
> > 
> > Check the permissions of the file.
> > 
> > Cheers,
> > 
> > tedd
> > 
> 
> I believe I do, since I set my folder permission of this script with modify, 
> read, execute and delete. I don't see any errors regarding this either. I 
> have changed my code to having the curly brace before I close the file 
> handler, if that helps any. Yet, it is still not modifying the file. Have I 
> missed something else here?
> 
> Thanks for your help.
> 
> Alice
> > -- 
> > -------
> > http://sperling.com  http://ancientstones.com  http://earthstones.com
>                                         
> _________________________________________________________________
> The New Busy is not the old busy. Search, chat and e-mail from your inbox.
> http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3


It might be some weird Windows thing. I know that some of the more
recent versions of Windows, in an attempt to be more secure, make you
jump through hoops to write to a file, even when the permissions are set
correctly. Unfortunately, I don't have access to a system with IIS so
there's no way I can test your code on that.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---

Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
From: a...@ashleysheridan.co.uk
To: aj...@alumni.iu.edu
CC: tedd.sperl...@gmail.com; php-gene...@lists.php.net
Date: Sun, 27 Jun 2010 17:16:04 +0100






  
  


On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:


> Date: Sun, 27 Jun 2010 12:01:50 -0400
> To: aj...@alumni.iu.edu; php-gene...@lists.php.net
> From: tedd.sperl...@gmail.com
> Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> 
> At 7:20 AM -0400 6/27/10, Alice Wei wrote:
> >Hi,
> >
> >I have the code as in the following:
> >
> ><?php
> >
> >$name= "Test";
> >$phone= "123-456-7890";
> >$email = "myem...@mymail.com";
> >$comments = "Test Only";
> >
> >$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone 
> >. "</phone>\n<email>".
> >           $email . "</email>\n<comments>" .  $comments . 
> >"</comments>\n</message>";
> >
> >//If file exists. append, otherwise create
> >$file = "messages.xml";
> >$fh = fopen($file,"a");
> >$lines = file($file);
> >
> >//Output a line of the file until the end is reached
> >foreach($lines as $line) {
> >
> >    if (trim($line) == "<messages>") {
> >
> >     $line = "<messages>" . $string; // If string, change it.
> >     echo $line . "<br>";
> >     fwrite($fh, $line);
> >   
> >    }
> >    else {
> >   
> >     // If not don't write anything
> >    echo $line . "<br>";
> >
> >    }
> >   fclose($file); // Close the file.
> >   }
> >?>
> >
> >For some reason, it is writing out the lines, but it is not 
> >f-writing to the file. Here is the file that is meant to be edited:
> >
> ><messages>
> ><message>
> ><name>Me</name>
> ><phone>123-456-7890</phone>
> ><email>t...@test.com</email>
> ><comments>This is my message board!</comments>
> ></message>
> ></messages>
> >
> >Have I missed something here? How come the file cannot perform fwrite 
> >actions?
> >
> >Thanks for your help.
> 
> Do you have permission to write to the file?
> 
> Check the permissions of the file.
> 
> Cheers,
> 
> tedd
> 

I believe I do, since I set my folder permission of this script with modify, 
read, execute and delete. I don't see any errors regarding this either. I have 
changed my code to having the curly brace before I close the file handler, if 
that helps any. Yet, it is still not modifying the file. Have I missed 
something else here?

Thanks for your help.

Alice
> -- 
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
                                          
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3




It might be some weird Windows thing. I know that some of the more recent 
versions of Windows, in an attempt to be more secure, make you jump through 
hoops to write to a file, even when the permissions are set correctly. 
Unfortunately, I don't have access to a system with IIS so there's no way I can 
test your code on that.


Well, my problem is not entirely solved, but I found that when I changed the 
file name extension from xml to txt, it is now letting me edit the file now. 
Only, the file is not writing in the way it is supposed to be. I used a+ 
originally, and it is writing outside my <messages></messages> root element. 

I tried w, and it wipes the file out. So, this proves that it is now modifying 
and saving it correctly. My question is, what kind of parameter should I use to 
add the things I would like to add after the <messages> without editing 
anything else in the file. 

Thanks for your help.

Alice







Thanks,

Ash

http://www.ashleysheridan.co.uk





                                          
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your 
inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2

--- End Message ---
--- Begin Message ---
On Sun, 2010-06-27 at 12:23 -0400, Alice Wei wrote:

> 
> Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
> From: a...@ashleysheridan.co.uk
> To: aj...@alumni.iu.edu
> CC: tedd.sperl...@gmail.com; php-gene...@lists.php.net
> Date: Sun, 27 Jun 2010 17:16:04 +0100
> 
> 
> 
> 
> 
> 
>   
>   
> 
> 
> On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:
> 
> 
> > Date: Sun, 27 Jun 2010 12:01:50 -0400
> > To: aj...@alumni.iu.edu; php-gene...@lists.php.net
> > From: tedd.sperl...@gmail.com
> > Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> > 
> > At 7:20 AM -0400 6/27/10, Alice Wei wrote:
> > >Hi,
> > >
> > >I have the code as in the following:
> > >
> > ><?php
> > >
> > >$name= "Test";
> > >$phone= "123-456-7890";
> > >$email = "myem...@mymail.com";
> > >$comments = "Test Only";
> > >
> > >$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone 
> > >. "</phone>\n<email>".
> > >           $email . "</email>\n<comments>" .  $comments . 
> > >"</comments>\n</message>";
> > >
> > >//If file exists. append, otherwise create
> > >$file = "messages.xml";
> > >$fh = fopen($file,"a");
> > >$lines = file($file);
> > >
> > >//Output a line of the file until the end is reached
> > >foreach($lines as $line) {
> > >
> > >    if (trim($line) == "<messages>") {
> > >
> > >     $line = "<messages>" . $string; // If string, change it.
> > >     echo $line . "<br>";
> > >     fwrite($fh, $line);
> > >   
> > >    }
> > >    else {
> > >   
> > >     // If not don't write anything
> > >    echo $line . "<br>";
> > >
> > >    }
> > >   fclose($file); // Close the file.
> > >   }
> > >?>
> > >
> > >For some reason, it is writing out the lines, but it is not 
> > >f-writing to the file. Here is the file that is meant to be edited:
> > >
> > ><messages>
> > ><message>
> > ><name>Me</name>
> > ><phone>123-456-7890</phone>
> > ><email>t...@test.com</email>
> > ><comments>This is my message board!</comments>
> > ></message>
> > ></messages>
> > >
> > >Have I missed something here? How come the file cannot perform fwrite 
> > >actions?
> > >
> > >Thanks for your help.
> > 
> > Do you have permission to write to the file?
> > 
> > Check the permissions of the file.
> > 
> > Cheers,
> > 
> > tedd
> > 
> 
> I believe I do, since I set my folder permission of this script with modify, 
> read, execute and delete. I don't see any errors regarding this either. I 
> have changed my code to having the curly brace before I close the file 
> handler, if that helps any. Yet, it is still not modifying the file. Have I 
> missed something else here?
> 
> Thanks for your help.
> 
> Alice
> > -- 
> > -------
> > http://sperling.com  http://ancientstones.com  http://earthstones.com
>                                         
> _________________________________________________________________
> The New Busy is not the old busy. Search, chat and e-mail from your inbox.
> http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3
> 
> 
> 
> 
> It might be some weird Windows thing. I know that some of the more recent 
> versions of Windows, in an attempt to be more secure, make you jump through 
> hoops to write to a file, even when the permissions are set correctly. 
> Unfortunately, I don't have access to a system with IIS so there's no way I 
> can test your code on that.
> 
> 
> Well, my problem is not entirely solved, but I found that when I changed the 
> file name extension from xml to txt, it is now letting me edit the file now. 
> Only, the file is not writing in the way it is supposed to be. I used a+ 
> originally, and it is writing outside my <messages></messages> root element. 
> 
> I tried w, and it wipes the file out. So, this proves that it is now 
> modifying and saving it correctly. My question is, what kind of parameter 
> should I use to add the things I would like to add after the <messages> 
> without editing anything else in the file. 
> 
> Thanks for your help.
> 
> Alice
> 
> 
> 
> 
> 
> 
> 
> Thanks,
> 
> Ash
> 
> http://www.ashleysheridan.co.uk
> 
> 
> 
> 
> 
>                                         
> _________________________________________________________________
> Hotmail is redefining busy with tools for the New Busy. Get more from your 
> inbox.
> http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2


There isn't any switch that suddenly recognises that you want to add
nodes inside of an XML document, you have to write the code for that
yourself.

There are two main ways to go about this:


     1. Read in the entire file with file(), create a new array with
        your new content to insert, and splice() it back into the main
        array you read in at the start, then write the whole of that to
        the original file, overwriting anything in it.
     2. Use XML functions to read in the contents of the file, add in
        the new nodes as you need, and overwrite the whole file again
        with the new DOM output


Either way, you're going to have to overwrite the existing file each
time.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
From: a...@ashleysheridan.co.uk
To: aj...@alumni.iu.edu
CC: tedd.sperl...@gmail.com; php-gene...@lists.php.net
Date: Sun, 27 Jun 2010 17:29:05 +0100






  
  


On Sun, 2010-06-27 at 12:23 -0400, Alice Wei wrote:


Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
From: a...@ashleysheridan.co.uk
To: aj...@alumni.iu.edu
CC: tedd.sperl...@gmail.com; php-gene...@lists.php.net
Date: Sun, 27 Jun 2010 17:16:04 +0100






  
  


On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:


> Date: Sun, 27 Jun 2010 12:01:50 -0400
> To: aj...@alumni.iu.edu; php-gene...@lists.php.net
> From: tedd.sperl...@gmail.com
> Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> 
> At 7:20 AM -0400 6/27/10, Alice Wei wrote:
> >Hi,
> >
> >I have the code as in the following:
> >
> ><?php
> >
> >$name= "Test";
> >$phone= "123-456-7890";
> >$email = "myem...@mymail.com";
> >$comments = "Test Only";
> >
> >$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone 
> >. "</phone>\n<email>".
> >           $email . "</email>\n<comments>" .  $comments . 
> >"</comments>\n</message>";
> >
> >//If file exists. append, otherwise create
> >$file = "messages.xml";
> >$fh = fopen($file,"a");
> >$lines = file($file);
> >
> >//Output a line of the file until the end is reached
> >foreach($lines as $line) {
> >
> >    if (trim($line) == "<messages>") {
> >
> >     $line = "<messages>" . $string; // If string, change it.
> >     echo $line . "<br>";
> >     fwrite($fh, $line);
> >   
> >    }
> >    else {
> >   
> >     // If not don't write anything
> >    echo $line . "<br>";
> >
> >    }
> >   fclose($file); // Close the file.
> >   }
> >?>
> >
> >For some reason, it is writing out the lines, but it is not 
> >f-writing to the file. Here is the file that is meant to be edited:
> >
> ><messages>
> ><message>
> ><name>Me</name>
> ><phone>123-456-7890</phone>
> ><email>t...@test.com</email>
> ><comments>This is my message board!</comments>
> ></message>
> ></messages>
> >
> >Have I missed something here? How come the file cannot perform fwrite 
> >actions?
> >
> >Thanks for your help.
> 
> Do you have permission to write to the file?
> 
> Check the permissions of the file.
> 
> Cheers,
> 
> tedd
> 

I believe I do, since I set my folder permission of this script with modify, 
read, execute and delete. I don't see any errors regarding this either. I have 
changed my code to having the curly brace before I close the file handler, if 
that helps any. Yet, it is still not modifying the file. Have I missed 
something else here?

Thanks for your help.

Alice
> -- 
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
                                          
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3




It might be some weird Windows thing. I know that some of the more recent 
versions of Windows, in an attempt to be more secure, make you jump through 
hoops to write to a file, even when the permissions are set correctly. 
Unfortunately, I don't have access to a system with IIS so there's no way I can 
test your code on that.


Well, my problem is not entirely solved, but I found that when I changed the 
file name extension from xml to txt, it is now letting me edit the file now. 
Only, the file is not writing in the way it is supposed to be. I used a+ 
originally, and it is writing outside my <messages></messages> root element. 

I tried w, and it wipes the file out. So, this proves that it is now modifying 
and saving it correctly. My question is, what kind of parameter should I use to 
add the things I would like to add after the <messages> without editing 
anything else in the file. 

Thanks for your help.

Alice







Thanks,

Ash

http://www.ashleysheridan.co.uk





                                          
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your 
inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2




There isn't any switch that suddenly recognises that you want to add nodes 
inside of an XML document, you have to write the code for that yourself.



There are two main ways to go about this:




    Read in the entire file with file(), create a new array with your new 
content to insert, and splice() it back into the main array you read in at the 
start, then write the whole of that to the original file, overwriting anything 
in it.
    Use XML functions to read in the contents of the file, add in the new nodes 
as you need, and overwrite the whole file again with the new DOM output



Either way, you're going to have to overwrite the existing file each time.

I ended up modifying the code by using r+ instead of using a, and modified your 
first option by reading the file line by line and writing out the lines over 
again plus the edits. Finally got it solved now, and I truly appreciate it. I 
will probably look at simpleXML when I get a chance. 

Thanks for your help.

Alice








Thanks,

Ash

http://www.ashleysheridan.co.uk







                                          
_________________________________________________________________
Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_1

--- End Message ---
--- Begin Message ---
<div dir="ltr">

-----------------------------------------------------------------------

Brandon Rampersad wants to stay in better touch using some of Google's
coolest new
products.

If you already have Gmail or Google Talk, visit:
http://mail.google.com/mail/b-b89a7a894d-a89de444ea-qNA2NPWwf-0ll8Gz7bfGZh8MgfI
You'll need to click this link to be able to chat with Brandon Rampersad.

To get Gmail - a free email account from Google with over 2,800 megabytes of
storage - and chat with Brandon Rampersad, visit:
http://mail.google.com/mail/a-b89a7a894d-a89de444ea-qNA2NPWwf-0ll8Gz7bfGZh8MgfI

Gmail offers:
- Instant messaging right inside Gmail
- Powerful spam protection
- Built-in search for finding your messages and a helpful way of organizing
  emails into "conversations"
- No pop-up ads or untargeted banners - just text ads and related information
  that are relevant to the content of your messages

All this, and its yours for free. But wait, there's more! By opening a Gmail
account, you also get access to Google Talk, Google's instant messaging
service:

http://www.google.com/talk/

Google Talk offers:
- Web-based chat that you can use anywhere, without a download
- A contact list that's synchronized with your Gmail account
- Free, high quality PC-to-PC voice calls when you download the Google Talk
  client

We're working hard to add new features and make improvements, so we might also
ask for your comments and suggestions periodically. We appreciate your help in
making our products even better!

Thanks,
The Google Team

To learn more about Gmail and Google Talk, visit:
http://mail.google.com/mail/help/about.html
http://www.google.com/talk/about.html

(If clicking the URLs in this message does not work, copy and paste them into
the address bar of your browser).

</div>

--- End Message ---
--- Begin Message ---
On Sunday 27 June 2010 04:08:24 Tanel Tammik wrote:
> Hello,
> 
> how to select only if value is present?
> 
>     $query = $db->query("select menus.id, menus.name,
>       case
>         when panels.id is not null then '1'
>         end as hiddenpanel
> 
>     from " . \DB_MENUS . " as menus
>       left join " . \DB_HIDDENPANELS . " as panels on (menus.id =
> panels.menu_id)
>     where menus.id='" . (int)$id . "'
>     ");
> 
> i would like to select hiddenpanel only if there is a corresponding value
>  in DB_HIDDENPANELS. At the moment i get NULL if there is no corresponding
>  value in HIDDENPANELS table!

I would use an if statement since you only need to determine true or false. 
Something like:

$query = $db->query("select menus.id, menus.name,
     from " . \DB_MENUS . " as menus
       left join " . \DB_HIDDENPANELS . " as panels on (menus.id =
 panels.menu_id)
     where menus.id='" . (int)$id . "'
     ");

if (empty(DB_HIDDENPANELS)) {
        echo "";

}

else {
        echo "hiddenpanel";

}

But I would wait for others to chime in on this one, because I'm very far from 
an expert, there's also got to be a much better efficient way to write the if 
statement above, but it's what I would do in a case like this until I found a 
better way.

-- 
Blessings,
David M.

--- End Message ---

Reply via email to