Synook Posted April 3, 2008 Share Posted April 3, 2008 Its simpler with a file, but a database can be more efficiently manipulated. You can use file_put_contents() to output to a file.Databasing is pretty simple, as it all revolves around SQL. Link to comment Share on other sites More sharing options...
jeffman Posted April 3, 2008 Share Posted April 3, 2008 Synook is a php5-ist. If you're stuck with php4, like some of us are, you'll need to use something like this:$F = fopen($filepath, "w");fwrite($F, $datastring);fclose($F);Best to look up any functions we tell you about so you get a complete understanding.Are you aware that the php manual has a shortcut for looking things up? You can type something like this and it will get you the correct page:au.php.net/fwrite Link to comment Share on other sites More sharing options...
Luke4123 Posted April 4, 2008 Author Share Posted April 4, 2008 I use PHP 5. So would I use a file or a database? Link to comment Share on other sites More sharing options...
Synook Posted April 4, 2008 Share Posted April 4, 2008 Your choice. I would suggest a database because it will help you learn more about that part of PHP. Though if you need a quick solution then go with a file. Link to comment Share on other sites More sharing options...
jeffman Posted April 4, 2008 Share Posted April 4, 2008 I'd start with file reading and writing. You'll want to know how to do it anyway, and what you're up to right now is perfect for it. Plus php5 makes it really simple. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 4, 2008 Author Share Posted April 4, 2008 Yeah I will probably start with File Reading and Writing. Is that in the W3School Tutorials? Link to comment Share on other sites More sharing options...
jeffman Posted April 4, 2008 Share Posted April 4, 2008 Yeah, and you should read it, and what the naual says too. But if you haven't seen it done before, it's hard to see what you need to know first. So I'll give you a little lesson right here.First, you need to design the structure you'll be saving. Will you use one file to hold everything or lots of little files, with one post per file? Your server can handle it either way.Let's say you go with lots of little files. Then you just need to figure the structure of each one. I'd use the newline character to separate items. Like: Name\nDate\nFavorite Band\nComment\n Obviously you know what your form is sending, I don't so you decide. You need some separator so you can extract the info and put it into variables. Newline just happens to make the file readable if you need to inspect one individually.2nd, you'll need to build a data string. That'll be like this:$writeDATA = '';$writeDATA .= $_POST['name'] . "\n"; $writeDATA .= $_POST['date'] . "\n"; $writeDATA .= $_POST['band'] . "\n";$writeDATA .= $_POST['comment'] . "\n";3rd, write the data. Easier than you think.$myPATH = "$dirname/$filename";$result = file_put_contents($myPATH, $writeDATA);if ($result === FALSE){//do something eventually, but don't worry about it now} There. You've just created and saved a file.To read it, do it backwards.$readDATA = file_get_contents($myPATH);// check for an errorNow, file_get_contents returns a string. You need to work with it. Several ways might be more appropriate to your situation. Here's one:list ($name, $date, $band, $comment) = explode ("\n", $readDATA); // I can't remember if \n needs escapingHere's the other:$myArray = explode ("\n", $readDATA);Working with array elements might be easier if you had like 30 of them. Either way, they're in variables you already know how to use. So plug them into an HTML structure and send it out.Except, it were me, I'd do all of the above before writing my HTML just to test the filesystem. Echo the variables to yourself to make sure it's working. When you can get them all, in the correct order, then write the HTML part of the php file.This should give you something to chew on. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks so much for that helpful tutorial. Also will I be able to delete the comments? Link to comment Share on other sites More sharing options...
jeffman Posted April 5, 2008 Share Posted April 5, 2008 Oh, yeah, delete away. If it looks like code, it might be useful. Otherwise chuck it. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 5, 2008 Author Share Posted April 5, 2008 Like if some random posts a rude comment i will be able to get rid of it? And I'm sorta confused about the File Writing thing... Link to comment Share on other sites More sharing options...
Luke4123 Posted April 5, 2008 Author Share Posted April 5, 2008 Oh and here is my form so far if you are wondering. http://www.theolsens.com.au/luke/form2.php Link to comment Share on other sites More sharing options...
jeffman Posted April 5, 2008 Share Posted April 5, 2008 Like if some random posts a rude comment i will be able to get rid of it? And I'm sorta confused about the File Writing thing... You can definitely get rid of it of a bad post.Here's how you get un-confused about file writing: go for it. I know, for some reason it seems like writing a file this way could lead some major catastrophe, but it won't. The worst that can happen is you get an error, and that's happened before, right? No big deal. Frustrating but not bad. Seriously, you've got your form, which I tested, and you can obviously grab the correct post variables, which is very cool. Now (If you haven't already) just use the stuff I gave you to write them down. Remember that file_put_contents will create the file if the file doesn't exist, so it's real easy. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 5, 2008 Author Share Posted April 5, 2008 I'm just a bit confused like where to put it all, its PHP isnt it? Also what document do I put it in? The one with the form or the one with all the post? Link to comment Share on other sites More sharing options...
Synook Posted April 6, 2008 Share Posted April 6, 2008 You would put validation and filtering server-side in the processing page. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 6, 2008 Author Share Posted April 6, 2008 So do I just copy and paste it or do I write my own? Link to comment Share on other sites More sharing options...
Synook Posted April 6, 2008 Share Posted April 6, 2008 Write your own, of course! You can try to copy and paste DD's, but I don't think it'll work. Link to comment Share on other sites More sharing options...
jhecht Posted April 6, 2008 Share Posted April 6, 2008 It's always best to attempt to write your own. If you just can't do it, ask for help or get a freeware script and break it down line by line. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 6, 2008 Author Share Posted April 6, 2008 Yeah I think I will try and read some more and try to get a better understanding. Link to comment Share on other sites More sharing options...
jeffman Posted April 6, 2008 Share Posted April 6, 2008 Yeah, everything I showed you is php. It would be in a file a lot like "post.php" which you are already using. In fact, you could duplicate "post.php", rename the duplicate, and use it as a starting place.The way it works now, the data comes in, you grab it, then merge it with some HTML using echo statements, yes?So your guest book will be almost like that. The data comes in, you grab it, write it into a file, and then send something back to the client. At first, while you're working on writing the file, I would just send back a simple thank you, or maybe send back form2.php again.Seriously, file_put_contents() is almost as easy as echo. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 6, 2008 Author Share Posted April 6, 2008 Ok. So this is my new post.php? <html><body><?php$writeDATA .= ";$writeDATA .= $_POST['name']. "/n";$writeDATA .= $_POST['post']. "/n";?></body></html> Is that all I need to do or is there more to it? Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2008 Share Posted April 7, 2008 Very close. Change this ($writeDATA .= " to this ($writeDATA = '' -- no dot, and two single quotes with nothing inside. The point is just to initialize an empty string. Not strictly necessary, but I like to do it when I'm getting ready to write a whole bunch of .= statements.The rest of the data string looks fine.Of course, you're not writing anything to a file yet, soCreate a filename somehow. For now it could be test.txtUse file_put_contents to write $writeDATA into that fileIf file_put_contents returns a true value, echo a happy message, otherwise echo a failure message.If you get nervous before you actually try it, go ahead and post your code here again. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 7, 2008 Author Share Posted April 7, 2008 Ok thanks for that. Is this the code you mean? <html><body><form action="post.php" method="post">Name:<br /><input type="text" name="name" /><br />Post: <br /><textarea rows="10" cols="30" name="post" />YoUr CoMmEnT hErE.</textarea><br><button input type="submit">Post Comment</button></form></body></html> Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2008 Share Posted April 7, 2008 No, I've seen that, it's on the page that already works. Unless you've written new code I don't know about, you still need to add those things to post.php. Link to comment Share on other sites More sharing options...
Luke4123 Posted April 7, 2008 Author Share Posted April 7, 2008 Well I changed post.php and it came up with an error. Try it:http://www.theolsens.com.au/luke/form2.php Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2008 Share Posted April 7, 2008 A parsing error just means something got written wrong. Half the time it's a missing semi-colon. Post the code! Let's have a look. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.