We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
PHP Writing; An Article Script
Topic Started: Jun 29 2005, 05:47 PM (1,268 Views)
shockwave-
My other title is a Ferrari
[ *  * ]
I've got a script that I'm writing, and it's supposed to write a news article to a file in the root directory, news.php. You can probably tell I'm new to PHP. I did some looking around, and here's the script I put together:

Code:
 

<?php
include 'versession.php';
include 'style.css';
?>
<br /><br />
<h3>Create a new News Article:</h3><br /><br />
<form method="post" action="
<?php
$name = $_POST['newsname'];
$news = $_POST['article'];
$fp=fopen("../news.php", "w+");
fwrite($fp, '<h2>$name :</h2><br /><br />$news');
$handle = fopen('../news.php', 'w+');
fclose($handle);

?>
">

<strong>Title of News Article</strong>:<input type="text" name="newsname" /><br />
<strong>Article:</strong><br />
<textarea name="article">Type your news article in here!</textarea><br />

<input type="submit" value="Post News Article" />

</form>




The problem is, it won't write to ../news.php! Someone, please help :D

EDIT: Oh, and everything is CHMODDED correctly
Offline Profile Quote Post Goto Top
 
Das
Member Avatar
Smells of rich mahogany
[ *  *  *  *  *  *  * ]
Not 110% sure, but change:



Quote:
 
$fp=fopen("../news.php", "w+");
fwrite($fp, '<h2>$name :</h2><br /><br />$news');
$handle = fopen('../news.php', 'w+');


To (try first):

Quote:
 
$fp=fopen("/news.php", "w+");
fwrite($fp, "<h2>" . $name . ":" . "</h2>" . "<br />" . "<br />" . $news');
$handle = fopen("/news.php", "w+");


Or (try if nessasary):

Quote:
 

$fp=fopen('/news.php', 'w+');
fwrite($fp, '<h2>' . $name . ':' . '</h2>' . '<br />' . '<br />' . $news');
$handle = fopen('/news.php', 'w+');
Offline Profile Quote Post Goto Top
 
shockwave-
My other title is a Ferrari
[ *  * ]
I get errors using both. :(
Offline Profile Quote Post Goto Top
 
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
Code:
 
$fp=fopen("../news.php", "w+");
$text = "<h2>$name :</h2><br /><br />$news";
fwrite($fp, $text);
$handle = fopen("../news.php", "w+");


May work.

However, in the code, all this block of PHP is going into the form action...are you sure you want it to go here, and not somewhere else?
Offline Profile Quote Post Goto Top
 
shockwave-
My other title is a Ferrari
[ *  * ]
Nope, Doesn't work. I put it in the form action, because I think that's what I'm supposed to do, but I wouldn't really know. However, when I was messing with the script, I did manage to create a file called $writefile (I screwed up variables and quotes).

EDIT: I thought I just did it, but it only writes a colon :(
Offline Profile Quote Post Goto Top
 
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
Well, the action of a form is the location of the script that runs when the form is submitted.
Example:

Code:
 
<form method="post" action="search.php">...


Would run the code in search.php when the form is submitted.
Offline Profile Quote Post Goto Top
 
bag
Take Me To Your Larder
[ * ]
ok, just as a question, im confused as how the action html tag is supposed to run the script seeing as all php geniratres before the page even loads, so no data would therefor throw up errors.

the action should be set to a page, wich holds the php prossor sperpetly?

so youd have
Code: HTML
 
<?php
include 'versession.php';
include 'style.css';
?>
<br /><br />
<h3>Create a new News Article:</h3><br /><br />
<form method="post" action="prossor.php">

<strong>Title of News Article</strong>:<input type="text" name="newsname" /><br />
<strong>Article:</strong><br />
<textarea name="article">Type your news article in here!</textarea><br />

<input type="submit" value="Post News Article" />

</form>


then prossor.php where the forms sent to
Code: HTML
 
<?php
$name = $_POST['newsname'];
$news = $_POST['article'];

$fp=fopen("../news.php", "w+");
fwrite($fp, '<h2>$name :</h2><br /><br />$news');
//$handle = fopen('../news.php', 'w+');
fclose($fp);

//then send them back to orgnal page with header redirect
Header("Location: page.php"); // chnage to correct
?>


i commented out teh bit that confused me

(i did it in a bit of a rush, so not sure it will work. or i did everything 100%, but i *think* it may)
Offline Profile Quote Post Goto Top
 
Sani
Member Avatar
Member
[ *  *  *  *  * ]
Code:
 

<?
if ($submit) {
$art = $_POST['article'];
$title = $_POST['title'];
if (!isset ($art) && ($title)){
   $fp = fopen("newzorz.txt","a+");
   fwrite($fp,nl2br("<h2>$title</h2>$art<hr />"));
   fclose($fp);
 }
}
?>
Add News:
<form action="<? $PHP_SELF ?>" method="post">
<br><input type="text" name="title" value="Title"><br>
<textarea type="text" name="article">
Article
</textarea><br>
<input type="submit" name="submit" value="Add">


Try this, it could work, i have not tested it.. :D
Offline Profile Quote Post Goto Top
 
shockwave-
My other title is a Ferrari
[ *  * ]
Ok, everyone! I Managed to do this :P :

Code:
 

<?php
include 'versession.php';
include 'style.css';
?>



<?php
$includecss = "<?php include 'newscss.css';?>";
$name = $_POST['newsname'];
$news = $_POST['article'];
$date = date("l dS, F Y");
$writepage = '../news.php';
$fp=fopen("../news.php", "w+");
$text = "$includecss<p>$date</p><br /><h2>$name:</h2><br /><p>$news</p>";
fwrite($fp, $text);
fclose($fp);
?>


<br /><br />
<h3>Create a new News Article:</h3><br /><br />
<form method="post" action="<? $PHP_SELF ?>">
<strong><?php echo "$date";?></strong><br />
<strong>Title of News Article</strong>:<input type="text" name="newsname" /><br />
<strong>Article:</strong><br />
<textarea name="article">Type your news article in here!</textarea><br />

<input type="submit" value="Post News Article" />

</form>


The includecss part is just for formatting the article(s).

Anyways, I managed to get it to work, and everything, and a few adjustments (like date posting), but I'm still having 2 problems (that I suspect are related to eachother)

1. When I visit the page to add a new article, the old one gets deleted, leaving just a colon!

2. I can only have one at a time, how would I put the next article on top of the last (descending order by date)?

If anyone could help, that'd be great :D
Offline Profile Quote Post Goto Top
 
solinent
Member
[ *  *  * ]
This might not be fully parsed and good, but it should work(I haven't tried it, I using asp principals though)
code
 

<?php
include 'versession.php';
include 'style.css';
?>



<?php
if (isset($_POST['newsname']) {
$includecss = "<?php include 'newscss.css';?>";
$name = $_POST['newsname'];
$news = $_POST['article'];
$date = date("l dS, F Y");
$writepage = '../'.$name.'.php';
$fp=fopen($writepage, "w+");

$text = "$includecss<p>$date</p><br /><h2>$name:</h2><br /><p>$news</p>";
fwrite($fp, $text);
fclose($fp);
}
?>


<br /><br />
<h3>Create a new News Article:</h3><br /><br />
<form method="post" action="<? $PHP_SELF ?>">
<strong><?php echo "$date";?></strong><br />
<strong>Title of News Article</strong>:<input type="text" name="newsname" /><br />
<strong>Article:</strong><br />
<textarea name="article">Type your news article in here!</textarea><br />

<input type="submit" value="Post News Article" />

</form>


I changed what's in red.
Basicly what I changed is that it won't execute unless there is a title, so that it won't replace the old file whenever you access this page, and that it will name the new article depending on the name of the article. You should make it so that it replaces all spaces with underscores(I don't remember which function does this but...)

It *should* work.
Offline Profile Quote Post Goto Top
 
Sani
Member Avatar
Member
[ *  *  *  *  * ]
Why don't you read the contents of the entire file, and then place the entry before it?
eg;

Code from my old guestbook.. I think you can get the idea.. :)
Code:
 

$file = fopen("text/comments.txt", "r");
$read = fread($file, filesize("text/comments.txt"));
fclose($file);
$cs = fopen("text/comments.txt", "w");
flock ($cs, LOCK_EX);
fwrite($cs, "$comments\n$read");
flock ($cs, LOCK_UN);
fclose ($cs);
Offline Profile Quote Post Goto Top
 
shockwave-
My other title is a Ferrari
[ *  * ]
Code:
 

if (isset($_POST['newsname']) {


You forgot to add the ending parenthases
Code:
 

if (isset($_POST['newsname'])) {


Anyways, I was looking for it to write the article to ../news.php, not to seperate files. Although, now that I think of it, I think I should edit the script so on ../news.php, each article has a 200 charachter limit, then it links to the article's full page :r

I'm gonna try it out.

How would I count the characters inputed in a textarea, after submission?
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply