Page 1 of 1

Problems writing news to a file. Help?

Posted: Tue Dec 03, 2002 6:16 am
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!

Posted: Tue Dec 03, 2002 7:37 am
by mydimension
put the fclose function outside the while loop

Posted: Tue Dec 03, 2002 6:01 pm
by Jim
Still doesn't work. :P

Posted: Thu Dec 05, 2002 6:12 am
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...

Posted: Thu Dec 05, 2002 6:27 am
by mydimension
Jim, probably should have posted this earlier but him and I worked this out so the issue is resolved.