View Full Version : PHP help
MrPinkPanther
05-07-2009, 02:31 PM
I've got some PHP which essentially writes out some of the details about my objects in an external app into a plist which is basically XML. The only problem is that it isnt writing! It could be a problem with the app but its more than likely PHP because I'm a PHP noob, can anyone see any errors?
<?php
$Object1 = $_POST['Object1'];
$Object1x = $_POST['Object1x'];
$Object1y = $_POST['Object1y'];
$Object2 = $_POST['Object2'];
$Object2x = $_POST['Object2x'];
$Object2y = $_POST['Object2y'];
$Object3 = $_POST['Object3'];
$Object3x = $_POST['Object3x'];
$Object3y = $_POST['Object3y'];
$Object4 = $_POST['Object4'];
$Object4x = $_POST['Object4x'];
$Object4y = $_POST['Object4y'];
$Object5 = $_POST['Object5'];
$Object5x = $_POST['Object5x'];
$Object5y = $_POST['Object5y'];
$file = 'Values.plist';
$f_op = fopen($file);
$string = '<?xml version="1.0" encoding="UTF-8"?>';
fwrite($f_op, $string);
$string = '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
fwrite($f_op, $string);
$string = '<plist version="1.0">';
fwrite($f_op, $string);
$string = '<dict>';
fwrite($f_op, $string);
$string = '<key>Item1</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object1 . '</string>';
fwrite($f_op, $string);
$string = '<key>Item1x</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object1x . '</string>';
fwrite($f_op, $string);
$string = '<key>Item1y</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object1y . '</string>';
fwrite($f_op, $string);
$string = '<key>Item2</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object2 . '</string>';
fwrite($f_op, $string);
$string = '<key>Item2x</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object2x . '</string>';
fwrite($f_op, $string);
$string = '<key>Item2y</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object2y . '</string>';
fwrite($f_op, $string);
$string = '<key>Item3</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object3 . '</string>';
fwrite($f_op, $string);
$string = '<key>Item3x</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object3x . '</string>';
fwrite($f_op, $string);
$string = '<key>Item3y</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object3y . '</string>';
fwrite($f_op, $string);
$string = '<key>Item4</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object4 . '</string>';
fwrite($f_op, $string);
$string = '<key>Item4x</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object4x . '</string>';
fwrite($f_op, $string);
$string = '<key>Item4y</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object4y . '</string>';
fwrite($f_op, $string);
$string = '<key>Item5</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object5 . '</string>';
fwrite($f_op, $string);
$string = '<key>Item5x</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object5x . '</string>';
fwrite($f_op, $string);
$string = '<key>Item5y</key>';
fwrite($f_op, $string);
$string = '<string>' . $Object5y . '</string>';
fwrite($f_op, $string);
fclose($f_op);
?>
Jam-ez
05-07-2009, 02:35 PM
I'm not sure if you can $string = '<?xml version="1.0" encoding="UTF-8"?>'; as wouldn't it close the php connection in the whole file? Not sure...
Edit: THIS IS USELESS:
Edit: Just tested this:
<?php
echo 'yo';
$string = '<?xml version="1.0" encoding="UTF-8"?>';
echo 'rawr';
echo $string;
echo 'meh';
?>
The data "string" contains nothing, and the output of the echo is nothing (result is: yorawrmeh) which would suggest that line does nothing?
I'll try a simple fopen/fclose/fwrite and test it now.
Actually, I just noticed, your fopen is wrong. You forgot to specify a mode http://uk2.php.net/manual/en/function.fopen.php such as
$f_op = fopen( $file, 'w' );
where as you just use:
$f_op = fopen($file);
Honestly why don't you just do
<?php
$Object1 = $_POST['Object1'];
$Object1x = $_POST['Object1x'];
$Object1y = $_POST['Object1y'];
$Object2 = $_POST['Object2'];
$Object2x = $_POST['Object2x'];
$Object2y = $_POST['Object2y'];
$Object3 = $_POST['Object3'];
$Object3x = $_POST['Object3x'];
$Object3y = $_POST['Object3y'];
$Object4 = $_POST['Object4'];
$Object4x = $_POST['Object4x'];
$Object4y = $_POST['Object4y'];
$Object5 = $_POST['Object5'];
$Object5x = $_POST['Object5x'];
$Object5y = $_POST['Object5y'];
$file = 'Values.plist';
$f_op = fopen($file);
$string = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Item1</key>
<string>' . $Object1 . '</string>
<key>Item1x</key>
<string>' . $Object1x . '</string>
<key>Item1y</key>
<string>' . $Object1y . '</string>
<key>Item2</key>
<string>' . $Object2 . '</string>
<key>Item2x</key>
<string>' . $Object2x . '</string>
<key>Item2y</key>
<string>' . $Object2y . '</string>
<key>Item3</key>
<string>' . $Object3 . '</string>
<key>Item3x</key>
<string>' . $Object3x . '</string>
<key>Item3y</key>
<string>' . $Object3y . '</string>
<key>Item4</key>
<string>' . $Object4 . '</string>
<key>Item4x</key>
<string>' . $Object4x . '</string>
<key>Item4y</key>
<string>' . $Object4y . '</string>
<key>Item5</key>
<string>' . $Object5 . '</string>
<key>Item5x</key>
<string>' . $Object5x . '</string>
<key>Item5y</key>
<string>' . $Object5y . '</string>';
fwrite($f_op, $string);
fclose($f_op);
?>
MrPinkPanther
05-07-2009, 02:37 PM
I'm not sure if you can $string = '<?xml version="1.0" encoding="UTF-8"?>'; as wouldn't it close the php connection in the whole file? Not sure...
Ok, cheers, +Rep. Can anyone shed some more light onto this and tell me how I might go about fixing it.
EDIT: CHEERS BLOB!
Jam-ez
05-07-2009, 02:41 PM
Ok, cheers, +Rep. Can anyone shed some more light onto this and tell me how I might go about fixing it.
EDIT: CHEERS BLOB!
Did what Blob gave work? If not, check my new new edit. :)
MrPinkPanther
05-07-2009, 02:45 PM
Did what Blob gave work? If not, check my new new edit. :)
It didn't but now it does thanks to you! I love you both lol.
Jam-ez
05-07-2009, 02:45 PM
It didn't but now it does thanks to you! I love you both lol.
No problem, good luck with whatever you're doing. :)
MrPinkPanther
05-07-2009, 03:00 PM
Cheers! Actually, while I'm at it and rather than creating a new thread in the future I have one more question. How can I add or remove users?
E.G. In the application below up to 3 users can join. Currently User1 and User3 are occupied but not User2.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>User1</key>
<string>George</string>
<key>User2</key>
<string></string>
<key>User3</key>
<string>Ryan</string>
</dict>
</plist>
How could I do it so when someone new joins then their name is sent via POST to fill the User2 String and then when they leave a message is sent via POST so they are removed again.
Jam-ez
05-07-2009, 03:13 PM
Well I'm not sure what they're leaving but when they're logging in do something like:
$username = $_POST['username'];
// then if we use that to say..
echo '<key>User1</key>
<string>George</string>
<key>User2</key>
<string>' . $username . '</string>
<key>User3</key>
<string>Ryan</string>';
You'd have to clarify more what you mean... if you wanted me to help more - or just have a go and post what you get. :D
Protege
05-07-2009, 04:34 PM
Havent tested it, but try it.
<?php
$objNum = 5;
$file = 'Values.plist';
$string = '<?xml version="1.0" encoding="UTF-8"?>';
$string .= '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
$string .= '<plist version="1.0">';
$string .= '<dict>';
for( $i = 1; $i < $objNum; $i++ )
{
$obj = $_POST[ 'Object' . $i ];
$objX = $_POST[ 'Object' . $i . 'x' ];
$objY = $_POST[ 'Object' . $i . 'y' ];
$string = '<key>Item' . $i . '</key>';
$string .= '<string>' . $obj . '</string>';
$string .= '<key>Item' . $i . 'x</key>';
$string .= '<string>' . $objX . '</string>';
$string .= '<key>Item' . $i . 'y</key>';
$string .= '<string>' . $objY . '</string>';
}
$string .= '</dict>';
$handle = fopen( $file );
fwrite( $handle, $string );
fclose( $handle );
?>
Protege
05-07-2009, 04:34 PM
Havent tested it, but try it.
<?php
$objNum = 5;
$file = 'Values.plist';
$string = '<?xml version="1.0" encoding="UTF-8"?>';
$string .= '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
$string .= '<plist version="1.0">';
$string .= '<dict>';
for( $i = 1; $i < $objNum; $i++ )
{
$obj = $_POST[ 'Object' . $i ];
$objX = $_POST[ 'Object' . $i . 'x' ];
$objY = $_POST[ 'Object' . $i . 'y' ];
$string = '<key>Item' . $i . '</key>';
$string .= '<string>' . $obj . '</string>';
$string .= '<key>Item' . $i . 'x</key>';
$string .= '<string>' . $objX . '</string>';
$string .= '<key>Item' . $i . 'y</key>';
$string .= '<string>' . $objY . '</string>';
}
$string .= '</dict>';
$handle = fopen( $file );
fwrite( $handle, $string );
fclose( $handle );
?>
Havent tested it, but try it.
<?php
$objNum = 5;
$file = 'Values.plist';
$string = '<?xml version="1.0" encoding="UTF-8"?>';
$string .= '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
$string .= '<plist version="1.0">';
$string .= '<dict>';
for( $i = 1; $i < $objNum; $i++ )
{
$obj = $_POST[ 'Object' . $i ];
$objX = $_POST[ 'Object' . $i . 'x' ];
$objY = $_POST[ 'Object' . $i . 'y' ];
$string = '<key>Item' . $i . '</key>';
$string .= '<string>' . $obj . '</string>';
$string .= '<key>Item' . $i . 'x</key>';
$string .= '<string>' . $objX . '</string>';
$string .= '<key>Item' . $i . 'y</key>';
$string .= '<string>' . $objY . '</string>';
}
$string .= '</dict>';
$handle = fopen( $file );
fwrite( $handle, $string );
fclose( $handle );
?>
Ya forgot a .
<?php
$objNum = 5;
$file = 'Values.plist';
$string = '<?xml version="1.0" encoding="UTF-8"?>';
$string .= '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
$string .= '<plist version="1.0">';
$string .= '<dict>';
for( $i = 1; $i < $objNum; $i++ )
{
$obj = $_POST[ 'Object' . $i ];
$objX = $_POST[ 'Object' . $i . 'x' ];
$objY = $_POST[ 'Object' . $i . 'y' ];
$string .= '<key>Item' . $i . '</key>';
$string .= '<string>' . $obj . '</string>';
$string .= '<key>Item' . $i . 'x</key>';
$string .= '<string>' . $objX . '</string>';
$string .= '<key>Item' . $i . 'y</key>';
$string .= '<string>' . $objY . '</string>';
}
$string .= '</dict>';
$handle = fopen( $file );
fwrite( $handle, $string );
fclose( $handle );
?>
Protege
05-07-2009, 06:16 PM
Woops. Well pointed out.
MrPinkPanther
07-07-2009, 09:51 AM
Cheers guys, +REP to all. One more thing is removal. How would I go about doing that?
MrPinkPanther
07-07-2009, 12:42 PM
I have been given this a potential solution but it doesn't seem to be working. Any help?
<?php
$file = file_get_contents('Names.plist');
$file = explode("\n", $file);
$name = $_POST['Name'];
foreach($file as $key => $value)
{
if(strstr($value, '<string>'.$name.'</string>'))
unset($file[$key]);
}
echo implode("\n", $file);
?>
MrPinkPanther
08-07-2009, 11:32 AM
Ignore the above post but I'm having trouble getting the code you guys created to work:
<?php
$objNum = 5;
$file = 'Names.plist';
$string = '<?xml version="1.0" encoding="UTF-8"?>';
$string .= '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
$string .= '<plist version="1.0">';
$string .= '<dict>';
for( $i = 1; $i < $objNum; $i++ )
{
$obj = $_POST[ 'Name' . $i ];
$string = '<key>Name' . $i . '</key>';
$string .= '<string>' . $obj . '</string>';
}
$string .= '</dict>';
$handle = fopen( $file );
fwrite( $handle, $string );
fclose( $handle );
?>
I'm "POST"ing this to it:
Name1 = George;
Name2 = Alan;
Name3 = Bob;
Name4 = Zach;
Name5 = Ian;
But it doesn't work. I added "w" after the FileOpen stuff so it knew to Write to it but then it just overwrites it all. If the "W" isnt there then it doesn't write anything and when you visit its location it says PHP on line 22.
BoyBetterKnow
08-07-2009, 11:54 AM
Ignore the above post but I'm having trouble getting the code you guys created to work:
I'm "POST"ing this to it:
But it doesn't work. I added "w" after the FileOpen stuff so it knew to Write to it but then it just overwrites it all. If the "W" isnt there then it doesn't write anything and when you visit its location it says PHP on line 22.
Er. Sorry I aint been paying attention to what you want. When you say overwrites it all, do you mean the current stuff in the file? You want to add to the file?
Source
08-07-2009, 12:05 PM
Didn't read the post fully, but if you want to add it on the the end of a file, I think you need to fopen it with the 'a' flag - for append.
Jam-ez
08-07-2009, 02:39 PM
Didn't read the post fully, but if you want to add it on the the end of a file, I think you need to fopen it with the 'a' flag - for append.
This is correct:
taken from http://uk3.php.net/manual/en/function.fopen.php
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
This should achieve what you are trying to do.
EDIT: example:
$handle = fopen( $file, 'a' );
fwrite( $handle, $string );
fclose( $handle );
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.