chwebdesigns Posted May 29, 2007 Share Posted May 29, 2007 Hello, I am designing a new website for a taekwondo club in my area. Currently we have a news page, but with no facility for instructors to post. I have written a short php code which works fine, and saves the news item into a *.txt file. As I do not have any access to mySQL. I also have written the code for showing the news on a page. But I want the latest news title to appear in a box on the index. I can get just the title but I am struggling just to get the latest. Here is the php code for submitting the news article <?php $pass = md5($HTTP_POST_VARS['password'] ) ; if($HTTP_POST_VARS['submit']) { if($pass == PASSWORD) { if(strstr($HTTP_POST_VARS['name'],"Select your name")) { echo "<h1 align=\"center\">You must select your name <a href=\"javascript:history.back(-1)\"> Go back</a></h1>"; exit; } if(!$HTTP_POST_VARS['title']) { echo "<h1 align=\"center\">You must enter a title <a href=\"javascript:history.back(-1)\"> Go back</a></h1>" ; exit; } if(!$HTTP_POST_VARS['news']) { echo "<h1 align=\"center\">You must enter some news <a href=\"javascript:history.back(-1)\"> Go back</a></h1>"; exit; } if(strstr($HTTP_POST_VARS['name'],"|")) { echo "<h1 align=\"center\">Name cannot contain the pipe symbol - |<a href=\"javascript:history.back(-1)\"> Go back </a></h1>"; exit; } if(strstr($HTTP_POST_VARS['title'],"|")) { echo "<h1 align=\"center\">Title cannot contain the pipe symbol - |<a href=\"javascript:history.back(-1)\"> Go back </a></h1>"; exit; } if(strstr($HTTP_POST_VARS['news'],"|")) { echo "<h1 align=\"center\">News cannot contain the pipe symbol - |<a href=\"javascript:history.back(-1)\"> Go back</a></h1>"; exit; } $fp = fopen('newsadd.txt','a'); if(!$fp) { echo "<h1 align=\"center\">Error opening file! Please try again. If the error continues please contact the webmaster <a href=\"javascript:history.back(-1)\" Go back</a><br><a href=\"mailto:webmaster@ultimatetkd.co.uk?subject=php error UTKD code 101\"> E-mail the webmaster</a></h1>"; exit; } $line = date("d/m/y") . "|" . $HTTP_POST_VARS['name']; $line .= "|" . $HTTP_POST_VARS['title']; $line .= "|" . $HTTP_POST_VARS['news']; $line = str_replace("\r\n","<BR>",$line); $line .= "\r\n"; fwrite($fp, $line); if(!fclose($fp)) { echo "Error closing file! Please try again. If the error continues please contact the webmaster <a href=\"javascript:history.back(-1)\" Go back</a><br><a href=\"mailto:webmaster@ultimatetkd.co.uk?subject=php error UTKD code 102\"> E-mail the webmaster</a>"; exit; } echo "<h1>News item added</h1>" ; } else { echo "<h1>Bad Password</h1>"; } } ?> Here is the php code for showing it on the news page: <?php $data = file('newsadd.txt'); $data = array_reverse($data); foreach($data as $element) { $element = trim($element); $pieces = explode("|", $element); echo "<h3 align=\"center\">" . $pieces[2] . "</h3>" . "" . "<i><h5 align=\"center\">Posted by " . $pieces[1] . " on " . $pieces[0] . "</b></i></h5><p align=\"center\">" . $pieces[3] . "HTML CODE" ; } ?> Some details: $pieces[0] = Date $pieces[1] = Name of poster $pieces[2] = Title of the news $pieces[3] = Main news article So basically .... all i want to do is show the latest $pieces[2] on a different php file. If you require any extra information I would try my best to write back. Also I do not have much experience in coding with PHP. Any replys / ideas will be extremley gratefull Thanks From CH Webdesigns Quote Link to comment https://forums.phpfreaks.com/topic/53450-solved-php-news-page/ Share on other sites More sharing options...
chigley Posted May 29, 2007 Share Posted May 29, 2007 <?php function latesttitle() { $lines = file("newsadd.txt"); // Get an array, with each element containing a line of a file. $lines = array_reverse($lines); // Now we have the most recent entry first $recentline = $lines[0]; // $recentline contains only the most recent line of the file $pieces = explode("|", $recentline); // Break it down... return $pieces[2]; // ... and give back the value } $newstitle = latesttitle(); // $newstitle now has the latest title name stored in it... echo "View our latest news article on <b>$newstitle</b> in the News section!"; // ... and you can output it like this! ?> Not tested at all, let me know how it goes Quote Link to comment https://forums.phpfreaks.com/topic/53450-solved-php-news-page/#findComment-264234 Share on other sites More sharing options...
chwebdesigns Posted May 29, 2007 Author Share Posted May 29, 2007 Thanks loads, this has worked perfectly! Just what I wanted, thanks lots from cal Quote Link to comment https://forums.phpfreaks.com/topic/53450-solved-php-news-page/#findComment-264240 Share on other sites More sharing options...
chigley Posted May 29, 2007 Share Posted May 29, 2007 No problemo Quote Link to comment https://forums.phpfreaks.com/topic/53450-solved-php-news-page/#findComment-264247 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.