Jump to content

[SOLVED] PHP News Page


chwebdesigns

Recommended Posts

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:[email protected]?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:[email protected]?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

Link to comment
https://forums.phpfreaks.com/topic/53450-solved-php-news-page/
Share on other sites

<?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 :)

Link to comment
https://forums.phpfreaks.com/topic/53450-solved-php-news-page/#findComment-264234
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.