Page 1 of 2
Need some guidance about this program
Posted: Mon Jun 19, 2006 3:07 pm
by akimm
ok my idea is to make an html file that will allow me to write a program that outputs an html form(easy) I know, then allows me to print the info to a textfile, however, I want to use it as a content mananagement system. Like article 1,article 2, and so on, and I want each article to be on their own file, I'm sure this is rather easy, however, as of now I am a bit confused.
functions.php
Code: Select all
<?php
function emailval() {
if ($submit) {
$good = preg_match(
'/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{2,4}$/',
$emailfield
);
if ($good) {
echo "";
} else {
echo "Please enter a proper email form like string@someplace.com";
}
function enterarticle() {
// recognize what i want from user
$user = $_POST['name'] . $_POST['email'] . $_POST['article'];
// start an iteration that will add 1,2,3,4,5,6 and so on to each article added
for ($i=0;$i<count($i);$i++){
if ($user) {
fwrite("/article/article[$i].txt")
}
}
?>
One point of major confusion is how to assign each article to a different textfile in anyone directory, like say I want my articles to go to /articles/ then I want the first article entered to be 1 second to be 2 third to be 3 and so on. I am trying right now to write a loop that iterates a new number for a unique file name for each new article, I'm not sure how to do that at this point tho I have my efforts written down however.
entry_page
Code: Select all
<?php
include "functions.php";
echo "<html>\n";
echo"<head>\n";
echo "<STYLE TYPE='text/css'>"\n;
echo " @import url(style.css);"\n";
echo "</STYLE>"\n";
echo "<body>\n";
$date = date('l n/d/y g:i a');
echo $date
echo "Todays date is" $date
echo "<form action="" method="POST">\n";
echo "<p><b>Your name</b>\n";
echo "<input type="text" name="name" size="30"></p>\n";
echo "<p><b>Your email</b>\n";
echo "<input type="text" name="email" size="30"></p>\n";
echo "<p><b>article</b>\n";
echo "<TEXTAREA NAME="article" COLS="120" ROWS="20" WRAP="virtual"></TEXTAREA></P>\n";
echo "<p><input type="submit" name="submit" value="submit your article"></p>\n";
if ($_POST['name'] = "" &&
$_POST['email'] = "" &&
$_POST['article'] ="")
{
echo "Please fill out all form fields";
}
function enterarticle()
}
?>
Any suggestions would be great.
Posted: Mon Jun 19, 2006 10:18 pm
by akimm
Thanks:-)
Posted: Mon Jun 19, 2006 10:20 pm
by Luke
Is there any reason you can't use mysql or any database for this?
Thanks for the response
Posted: Mon Jun 19, 2006 10:52 pm
by akimm
And no I cannot, none available to me at this moment, Sorry, I wish I wasn't such a pain. Althought, for now I must use what tools are available to me.
Re: Need some guidance about this program
Posted: Tue Jun 20, 2006 2:12 am
by Luke
akimm wrote:
Code: Select all
<?php
function enterarticle() {
// recognize what i want from user
$user = $_POST['name'] . $_POST['email'] . $_POST['article'];
// start an iteration that will add 1,2,3,4,5,6 and so on to each article added
for ($i=0;$i<count($i);$i++){
if ($user) {
fwrite("/article/article[$i].txt")
}
}
?>
OK, one thing at a time... first of all you need to open the file before you can fwrite() to it.
fopen() accepts the filename as its first argument and 'w' (for write if you are creating a file or overwriting it) as the second argument (also accepts 'r' for read, 'a' for append) and returns a file handle (which is needed for working with the file)
Then use fwrite to write to the file. fwrite returns the amount of bytes written.
If you have any questions about any of the functions, you should be able to just click on them and it will pop up their documentation in another window
Code: Select all
$fileName = "/article/article1.txt"; // This can be anything (including file names generated with a loop like the one above)
$fileHandle = fopen($fileName, 'w') or die("can't open file"); // open the file or kill the script and show error
$numBytes = fwrite($fileHandle, "Information you want written to the file, such as name, email and article"); // Write to the file with fwrite now
fclose($fileHandle); // Close the file handle
I didn't know I had to
Posted: Tue Jun 20, 2006 6:46 am
by akimm
fopen() if the file didn't exist like a new article by say bob entitled why bob is cool.
Code: Select all
<?php
// open file
if this doesn't exist i figured it just had to be written, but thank you for the assistance, now to understand you correctly could I:
$fileName = "/article/article[$i].txt"; // This can be anything (including file names generated with a loop like the one above)
$fileHandle = fopen($fileName, 'w') or die("can't open file"); // open the file or kill the script and show error
$numBytes = fwrite($fileHandle, "Information you want written to the file, such as name, email and article"); // Write to the file with fwrite now
fclose($fileHandle); // Close the file handle
?>
Re: I didn't know I had to
Posted: Wed Jun 21, 2006 12:12 am
by Luke
akimm wrote:fopen() if the file didn't exist like a new article by say bob entitled why bob is cool.
Code: Select all
<?php
// open file
if this doesn't exist i figured it just had to be written, but thank you for the assistance, now to understand you correctly could I:
$fileName = "/article/article[$i].txt"; // This can be anything (including file names generated with a loop like the one above)
$fileHandle = fopen($fileName, 'w') or die("can't open file"); // open the file or kill the script and show error
$numBytes = fwrite($fileHandle, "Information you want written to the file, such as name, email and article"); // Write to the file with fwrite now
fclose($fileHandle); // Close the file handle
?>
I'm not sure what you are asking here
Ok, fine I will use this.
Posted: Fri Jun 30, 2006 4:03 am
by akimm
Weirdan, that section you cleaned up didn't help the program
I had these errors:
Friday 6/30/06 5:00 am
Warning: fopen(/articles/article8.txt): failed to open stream: No such file or directory in /nfs/cust/8/25/05/650528/web/articles/1.php on line 51
Warning: fwrite(): supplied argument is not a valid stream resource in /nfs/cust/8/25/05/650528/web/articles/1.php on line 54
Warning: fclose(): supplied argument is not a valid stream resource in /nfs/cust/8/25/05/650528/web/articles/1.php on line 55
Posted: Fri Jun 30, 2006 4:08 am
by Weirdan
That means you're trying to write to non-existant directory.
what I would propose to start with is using relative path to articles:
Code: Select all
// $file = '/articles/article' . $visits . '.txt';
$file = basename(__FILE__). '/article' . $visits . '.txt';
well
Posted: Fri Jun 30, 2006 4:13 am
by akimm
I definetly made a directory called /articles/
I don't know exactly what to do with your code, __FILE__ what do I put there?
Re: well
Posted: Fri Jun 30, 2006 4:15 am
by JayBird
akimm wrote:I definetly made a directory called /articles/
I don't know exactly what to do with your code, __FILE__ what do I put there?
Just put EXACTLY what Weirdan told you
__FILE__ is a magic constant -
http://uk.php.net/manual/en/language.co ... efined.php
ok
Posted: Fri Jun 30, 2006 4:23 am
by akimm
I'm not that deep into programming to understand magic constants
Here is how I have it
Posted: Fri Jun 30, 2006 4:30 am
by akimm
is this correct
// $file = '/articles/article' . $visits . '.txt';
$file = basename(__FILE__). '/articles' . $visits . '.txt';
update
Posted: Fri Jun 30, 2006 4:40 am
by akimm
it still has the same errors even with the magic constant.
Ok
Posted: Fri Jul 07, 2006 2:52 am
by akimm
If I use a magic constant will that allow for the creation of a file.
Code: Select all
$name = $_POST['name'];
// $file = '/articles/article' . $name . '.txt'
$file = basename(__FILE__). '/articles/' . $name . '.txt';
For example if I wanted a file named after a user who submitted his name, we'll say bob for arguments sakes, however, if i wanted this file bob.txt created in /articles/ would this be the properway of doing so?