Problems writing news to a file. Help?

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

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Problems writing news to a file. Help?

Post by Jim »

I'm selecting posted news information from a MySQL database and trying to put it on a text file. There are often several news posts, and they are constantly updated/added/deleted.

What's I'm trying to do is write this news into a text file for each news post there is.

I want the news.txt file (the file to which all news is written to) to be completely written over with new information each time new news is posted, just to ensure no old news is left on the page.

This is what I have so far:

Code: Select all

if($query) {


//open file for writing and place pointer at end of file
$fp = fopen("/home/www/nhgn/news.txt","r+");

		while ($data = mysql_fetch_assoc($query)) {
			
			$id = $data['id'];
			$poster = $data['poster'];
			$headline = $data['header'];
			$news = $data['news'];
			$date = date("l, F j Y", $data['postdate']);
			//$date = formatDate($datestamp);
			
 
		
		//This is your news template.  Edit the stuff inside the echo " ";
		//Use these variables:  
		
		 $newsitem = "<table width='100%' cellspacing='0' cellpadding='0' bordercolor='#0000FF'>
  <tr background='http://www.nhgn.us/images/topcell2.gif'>
    <td colspan='2' height='28'>
      <table width='100%' border='0' cellspacing='0' cellpadding='0' background='http://www.nhgn.us/images/topcell2.gif' bordercolor='#FFFFFF' bgcolor='#000099'>
        <tr>
          <td width='2%' valign='middle'><font size='-1' color='FFFFFF' face='arial, verdana, tahoma'><b><img
src='http://www.nhgn.us/images/news/general.gif' width='20' height='22'
alt='General News' align='baseline' vspace='0'>
            </b></font></td>
          <td width='95%' valign='middle'><font size='-1' color='FFFFFF' face='arial, verdana, tahoma'><b>
            <a name='$id'></a>&nbsp;$headline</b></font></td>
          <td align='right' width='3%'><img src='http://www.nhgn.us/images/topcell2end.gif' width='14' height='28'></td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td colspan='2'> </td>
  </tr>
  <tr>
    <td><i><font color='#0000CC' size='-2'
face='arial'>&nbsp;$date</font></i></td>
    <td>
      <div align='right'><font face='arial, verdana, tahoma'
color='#333333'><i><font size='-2' color='#0000CC'>Posted
        by </font><font size='-2'><a
href='http://www.nhgn.us/staff.shtml#$newsemail'>$poster</a>
        </font></i></font></div>
    </td>
  </tr>
  <tr>
    <td colspan='2'>
      <blockquote>
        <p><font size='-1' color='#000000' face='arial'> <br>
          $news</font></p>
      </blockquote>
    </td>
  </tr>
</table>";			

							
// write data to file
fputs($fp,$newsitem,strlen($newsitem));
// flush the output to the file
fflush($fp);
// close the file
fclose($fp);

}

			

}

}

This writes the first news post I add to the text file. News posts after that are ignored.

See the results at http://www.nhgn.us/news.txt

Thanks for help!
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

put the fclose function outside the while loop
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Still doesn't work. :P
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Instead of

Code: Select all

// write data to file
fputs($fp,$newsitem,strlen($newsitem));
try

Code: Select all

fputs($fp,$newsitem)
without the length-parameter. This is just a guess, I can't see anything wrong with your code...
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

Jim, probably should have posted this earlier but him and I worked this out so the issue is resolved.
Post Reply