Need some guidance about this program

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Need some guidance about this program

Post 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() 
}
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Any suggestions would be great.

Post by akimm »

Thanks:-)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Is there any reason you can't use mysql or any database for this?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks for the response

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need some guidance about this program

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

I didn't know I had to

Post 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 
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: I didn't know I had to

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok, fine I will use this.

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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';
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

well

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: well

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post by akimm »

I'm not that deep into programming to understand magic constants
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Here is how I have it

Post by akimm »

is this correct

// $file = '/articles/article' . $visits . '.txt';
$file = basename(__FILE__). '/articles' . $visits . '.txt';
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

update

Post by akimm »

it still has the same errors even with the magic constant.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok

Post 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?
Post Reply