Help with PHP

Bakurei

Spy
Joined
Jun 27, 2004
Messages
980
Reaction score
0
For starters my knowledge of PHP is about 1h of reading and 3h of toying around. So now to the bussines, could someone please tell me whats wrong with these codes (trying to get a news posting system working).

PHP:
<html>
<head>
<title>Admin sivun testaus</title>
<link href="style.css"  type="text/css">
</head>
<body>
<?php
$news = file("news.txt");
$news = array_reverse($news);
$amount = count($news);

for ($i = 0; $i < $amount; $i++) {
	$info = explode( "|", $news[$i], 5);
	
	$top = $info[0];
	$time = $info[1];
	$newstitle = $info[2];
	$message = $info[3];
	$bottom = $info[4];
	
echo $top . $newstitle . $message $bottom;

}		
?>
</body>
</html>

And the other one:

PHP:
<?php
if (!isset($_POST['name'])) {
?>
<html>
 <head>
  <title>Viestin lähetys</title>
 </head>
 <body>
  <h1>Viestin lähetys</h1>

<form action="undex.php" method="post">
Name: [br] <input type="text" name="name"> [br]
Title: [br] <input type="text" name="newstitle"> [br]
Message: [br] <textarea name="message"></textarea> [br] [br]
<input type="submit" value="Send">
</form>

</body>
</html>
<?php
} else {
   $file = fopen("news.txt", "a");

   $top = '<table  cellspacing="0" cellpadding="0" width=820 align="center" ><tr><td>[img]img/yla_vasen.gif[/img]</td><td></td><td>[img]img/yla_oikea.gif[/img]</td></tr><tr><td></td><td width=800>'; 
   $newstitle = '<span>[img]img/Avatar_Akurei-.png" align="right" >[i]'. $_POST['name'] . '[/i][b]' . $_POST['newstitle'] . '[/b][br]<hr>';
   $message = '<p>' . $_POST['message'] . '</p></span>';
   $bottom = '</td><td></td></tr><tr><td>[img]img/ala_vasen.gif" width=10 height=10 border=0 ></td><td></td><td>[img]img/ala_oikea.gif" width=10 height=10 border=0 ></td></tr></table>[br]';
     
   $time = time();

   $row= "$top|$time|$newstitle|$message|$bottom \n";

   fwrite($file, $row);

   fclose($file);

   header("Location: undex.php");
}
?>

Thank you.
 
I personally use Mysql for my news posts but I did use a file format before..
if (!isset($_POST['name'])) {
?>
<html>
<head>
<title>Viestin lähetys</title>
</head>
<body>
<h1>Viestin lähetys</h1>

<form action="undex.php" method="post">
Name: [br] <input type="text" name="name"> [br]
Title: [br] <input type="text" name="newstitle"> [br]
Message: [br] <textarea name="message"></textarea> [br] [br]
<input type="submit" value="Send">
</form>

</body>
</html>
<?php
} else {
What are you trying to do with that?
I think you are trying to make that html code appear ONLY if that cookie is not present. Well you need to use the echo command to do that:

<?php
if (!isset($_POST['name'])) {
echo('
<html>
<head>
<title>Viestin lähetys</title>
</head>
<body>
<h1>Viestin lähetys</h1>

<form action="undex.php" method="post">
Name: [br] <input type="text" name="name"> [br]
Title: [br] <input type="text" name="newstitle"> [br]
Message: [br] <textarea name="message"></textarea> [br] [br]
<input type="submit" value="Send">
</form>

</body>
</html>
');
} else {
$file = fopen("news.txt", "a");
For the echo I used ' ' instead of " " because you used " " during the echo(Just for future refrence).

Anyways I am confused on your problem if thats not it or something..you really didn't tell us whats wrong..

If you give us the error it says(for what line and such) we can start there if you are getting an error.
 
Well it doesn't give an error, it just won't work. It won't write anything in the news.txt file, and I can't use MYSQL (the place where I have my homepages doesn't support that).
 
If i get problems such as this where there is no given error but the script does not execute as wanted, i usually put little flag variables at certain lines to see whether crucial stages are being met. In your case i'd check that the If statement in the second one is working and that the script is reaching the fwrite.
 
Back
Top