5 Replies - 624 Views - Last Post: 31 May 2009 - 05:29 AM Rate Topic: -----

#1 saxasm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-May 08

Weird errors in news system

Posted 30 May 2009 - 12:21 PM

I've been trying to make a small system to post news on the front page of my page and not have a stupid "this is a webpage" front page.

I have two errors that i can't find out why they happen.


The first one is:
<?php

if (isset($_GET['news']))
{
	$article = $_GET['news'];
	$file = fopen("news.txt","r") or die("Unable to open file, sorry");
	
	while(!feof($file))
	{
		$tmp = fgets($file);
		
		if ($tmp == $article)
		{
			$tmp = fgets($file);
			echo '<h1>';
			echo $tmp;
			echo '</h1>';
			
			$tmp = fgets($file);
			echo $tmp;
			
			break;
		} else
		{
			fgets($file);
			fgets($file);
		}
	}
} else
{
	echo '<h1>No page specified</h1>You have to specify what news you want to look at.';
}


It doen't output anything at all!

The news.txt file is formatted as:
Short name for links
Header
Text


2:

The next error is:
<?php
if ( isset($_GET['completed']) )
{
	echo '<h1>News submitted</h1>The news will appear on the site now';
} elseif(!(isset($_POST['password'])&&isset($_POST['name'])&&isset($_POST['header'])&&isset($_POST['text'])))
{
	// I:E print the submit page
	echo '
	<form action="addnews.php" method="post">
	Header: <input type="text" name="header" /><br />
	Text (can use html tags, abstain from using <h1>. No newlines in it will be preserved, use <br />):<br />
	<textarea rows="10" cols="30" name="text"> This is an example news. Lorem ipsum dolor sit amet. </textarea> <br />
	Unique name (can only contain the letters a-z): <input type="text" name="name" /><br />
	Password: <input type="password" name="password" /><br />
	<input type="submit" />
	</form>
	';
	
} else
{
	if ($_POST['password'] == '******')
	{
		$tmp = str_replace('\n','<br />',$_POST['text']);
		$file = fopen("news.txt","a") or die("Unable to open file, sorry");
		fputs($file, $_POST['name']);
		fputs($file, "\n");
		fputs($file, $_POST['header']);
		fputs($file, "\n");
		fputs($file, $tmp);
		fputs($file, "\n");
		echo '<script>document.location="http://***********.x10hosting.com/index.php?p=addnews.php&completed=true";</script>';
		
	} else
	{
		echo '<h2>Invalid password</h2>
		<b>Sorry, the password you supplied was invalid! <a href="http://**********.x10hosting.com/index.php?p=addnews.php">Try again.</a></b>';
	}
}


Simply, it doesn't print it to the file in the correct format.

Is This A Good Question/Topic? 0
  • +

Replies To: Weird errors in news system

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 125
  • View blog
  • Posts: 704
  • Joined: 28-April 07

Re: Weird errors in news system

Posted 30 May 2009 - 12:58 PM

hello,
for the first one, the problem is there is some hidden characters included to the $tmp (the carriage return and the new line), thats why comparing $tmp and $article will always fail

you can simply remove those characters to fix this problem
$tmp = str_replace("\n","",fgets($file));
$tmp = str_replace("\r","",$tmp);


edit:
for the second problem:
what is the correct format?
is it the one from the first example (Short name for links,Header,Text)
if so, then it's the same problem, but this time you have to add those characters (\r\n)
fputs($file, "\r\n");



hope it helps

This post has been edited by ahmad_511: 30 May 2009 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: Weird errors in news system

Posted 30 May 2009 - 05:16 PM

View Postahmad_511, on 30 May, 2009 - 01:58 PM, said:

$tmp = str_replace("\r","",$tmp);

You'd be better off doing that with trim() rather than str_replace(). You're trying to remove leading and trailing whitespace, which is the entire purpose of trim().
Was This Post Helpful? 1
  • +
  • -

#4 saxasm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-May 08

Re: Weird errors in news system

Posted 30 May 2009 - 07:55 PM

View PostAdaHacker, on 30 May, 2009 - 04:16 PM, said:

View Postahmad_511, on 30 May, 2009 - 01:58 PM, said:

$tmp = str_replace("\r","",$tmp);

You'd be better off doing that with trim() rather than str_replace(). You're trying to remove leading and trailing whitespace, which is the entire purpose of trim().

Thanks. I think it works now. :)

[edit]

No it doesn't. At least not the viewnews.php, which is the main priority for me. :(

I get no output at all!

[/edit]

This post has been edited by saxasm: 30 May 2009 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: Weird errors in news system

Posted 31 May 2009 - 05:17 AM

View Postsaxasm, on 30 May, 2009 - 08:55 PM, said:

No it doesn't. At least not the viewnews.php, which is the main priority for me. :(

Works fine for me. The only thing I changed was to wrap the very first call to fgets() in a call to trim() like this: $tmp = trim(fgets($file)).

What does your code look like now? And what's in the news.txt file?
Was This Post Helpful? 0
  • +
  • -

#6 saxasm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-May 08

Re: Weird errors in news system

Posted 31 May 2009 - 05:29 AM

View PostAdaHacker, on 31 May, 2009 - 04:17 AM, said:

View Postsaxasm, on 30 May, 2009 - 08:55 PM, said:

No it doesn't. At least not the viewnews.php, which is the main priority for me. :(

Works fine for me. The only thing I changed was to wrap the very first call to fgets() in a call to trim() like this: $tmp = trim(fgets($file)).

What does your code look like now? And what's in the news.txt file?

The code:
<?php
$title = 'News not found - ';
$file = fopen("news.txt","r") or die("Unable to open file");
while(!feof($file))
{
	$tmp = fgets($file);
	trim($tmp);
	if ($tmp == $_GET['news'])
	{
		$title=fgets($file);
		break;
	} else
	{
		fgets($file);
		fgets($file);
	}
}
fclose($file);

function PrintNews()
{
	if (isset($_GET['news']))
	{
		$article = $_GET['news'];
		$file = fopen("news.txt","r") or die("Unable to open file, sorry");
	
		while(!feof($file))
		{
			$tmp = fgets($file);
			trim($tmp);
			if ($tmp == $article)
			{
				$tmp = fgets($file);
				echo '<h1>';
				echo $tmp;
				echo '</h1>';
			
				$tmp = fgets($file);
				echo $tmp;
			
				break;
			} else
			{
				fgets($file);
				fgets($file);
			}
		}
	} else
	{
		echo '<h1>No page specified</h1>You have to specify what news you want to look at.';
	}
}

I changed it bit to make it work with my index.php, but it shouldn't influence it too much.

news.txt:
newssystemlaunched
News system launched
The news system has been launched!<br />We can now show news on the site!
testnews
TEST
This is an example news. Lorem ipsum dolor sit amet.
statusupdatemay27
The game development is progressing nicely
We just wanted to give you a status update.<br /><br />Everything is going well, and we are working as hard as ever.<br /><img src="http://farm1.static.flickr.com/1/2836_31f626c5fe.jpg?v=0" />
problemsnewssystemmay27
We are having some problems with the news system
Sorry for the inconvenience everyone. There is some bug in the system which makes it impossible to follow the links in the sidebar and the news archive.<br />Hope you can wait for it, it shouldn't take long to fix.
betaisbroken
Please note that the beta registration is broken
We've been getting some requests asking why they can't sign up lately, so remember that the beta is broken. If you can't register for the forums either, send a mail to <a href="mailto:saxasm@gmail.com">saxasm@gmail.com</a> and we will try to fix the problem as soon as possible.
feedbacksystemlaunched
Feedback system launched
The system for sending feedback on the game has been launched!<br />You can now send us messages using the form <a href="http://thrinityonline.x10hosting.com/index.php?p=feedback.htm">here.</a><br />It will send an email to everyone on the team, if you want to contact a specific member, use his email, which you can find <a href="http://thrinityonline.x10hosting.com/index.php?p=whois.htm">here.</a>
firstconceptart
First concept art released
We are proud to release the first basic concept art!<br />It is a basic sketch for the game things, and later we will release images of their models.<br /><img width="500" height="500" src="http://thrinityonline.x10hosting.com/img1stuff.bmp" /><img width="500" height="500" src="http://thrinityonline.x10hosting.com/HandCannon.bmp" />

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1