• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

SImple News Script...

Lokannon

New Member
Okay, i need help with this... i just cant seem to figure it out. Its a simple news script. I want to be able to have a title shown on the news I add. I am using 3 scripts.

The first one is the addnews.php this is the problem child... Heres the code:

PHP:
<?
if($action == "edit" && isset($HTTP_POST_VARS['password'])) {
    //obviously you should change this password on the next line
    if($HTTP_POST_VARS['password'] == "karate7") {
        //First let's recompile that line with the pipe symbols so we can reinsert it
        $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name'];
        $line .= "|" . $HTTP_POST_VARS['news'];
        $line = str_replace("\r\n","<BR>",$line);
        $line .= "\r\n";
        $data = file('news.txt');
        $data[$id] = $line;
        //the next line makes sure the $data array starts at the beginning
        reset($data);
        //now we open the file with mode 'w' which truncates the file
        $fp = fopen('news.txt','w');
        foreach($data as $element) {
            fwrite($fp, $element);
        }
        fclose($fp);
        echo "Item Edited!<BR><BR>\n";
        echo "<a href=\"$PHP_SELF\">Go Back</a>\n";
        exit;
    } else {
        echo "Bad password!\n";
        exit;
    }
}
if($action == "edit") {
    $data = file('news.txt');
    $element = trim($data[$id]);
    $pieces = explode("|", $element);
    //the next line is to reverse the process of turning the end of lines into breaking returns
    $news = str_replace("<BR>","\r\n",$pieces[3]);
    echo "Make the changes you would like and press save.<BR>\n";
    echo "<FORM ACTION=\"$PHP_SELF?action=edit\" METHOD=\"POST\" NAME=\"editform\">\n";
    echo "Name:<BR>\n";
    echo "<INPUT TYPE=\"text\" SIZE=\"30\" NAME=\"name\" value=\"".$pieces[1]."\"><BR>\n";
	echo "Title:<BR>\n";
    echo "<INPUT TYPE=\"text\" SIZE=\"30\" NAME=\"title\" value=\"".$pieces[3]."\"><BR>\n";
    echo "The News:<BR>\n";
    echo "<TEXTAREA NAME=\"news\" COLS=\"40\" ROWS=\"5\">".$pieces[2]."</TEXTAREA><BR><BR>\n";
    echo "Password:<BR>\n";
    echo "<INPUT TYPE=\"password\" SIZE=\"30\" NAME=\"password\"><BR>\n";
    echo "<INPUT TYPE=\"hidden\" NAME=\"date\" VALUE=\"".$pieces[0]."\">\n";
    echo "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
    echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Save\"><BR>\n";
    echo "</FORM>\n";
    exit;
}
if($action == "delete" && isset($HTTP_POST_VARS['password'])) {
    //obviously you should change this password on the next line
    if($HTTP_POST_VARS['password'] == "karate7") {
        $data = file('news.txt');
        //this next line will remove the single news item from the array
        array_splice($data,$id,1);
        //now we open the file with mode 'w' which truncates the file
        $fp = fopen('news.txt','w');
        foreach($data as $element) {
            fwrite($fp, $element);
        }
        fclose($fp);    
        echo "Item deleted!<BR><BR>\n";    
        echo "<a href=\"$PHP_SELF\">Go Back</a>\n";
        exit;
    } else {
        echo "Bad password!\n";
        exit;
    }
}
if($action == "delete") {
    echo "<H2>You are about to delete the following news item.</H2>\n";
    $data = file('news.txt');
    $element = trim($data[$id]);
    $pieces = explode("|", $element);
    echo $peices[3] . "<br>" . $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>\n";
    echo "<BR><BR>\n";
    echo "Are you sure you want to delete this news item? If so, enter the password and click on Delete.<BR>\n";
    echo "<FORM ACTION=\"$PHP_SELF?action=delete\" METHOD=\"POST\" NAME=\"deleteform\">\n";
    echo "Password:<BR>\n";
    echo "<INPUT TYPE=\"password\" SIZE=\"30\" NAME=\"password\"><BR>\n";
    echo "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
    echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Delete\"><BR>\n";
    echo "</FORM>\n";
    exit;
}


echo "<H1><u>Current News</u></H1>\n";
$data = file('news.txt');
//next line removed to make everything else easier in the admin script
//$data = array_reverse($data);
foreach($data as $key=>$element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo $pieces[3] . "<BR>" . $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>\n";
    echo "&nbsp;<a href=\"$PHP_SELF?action=delete&id=$key\">Delete</a>\n";
    echo "&nbsp;<a href=\"$PHP_SELF?action=edit&id=$key\">Edit</a>\n";
    echo "<BR><BR>\n";
}
echo "<HR>\n";
echo "<H1><u>Add News</u></H1>\n";
if($HTTP_POST_VARS['submit']) {
    if($HTTP_POST_VARS['password'] == 'karate7') {
        if(!$HTTP_POST_VARS['name']) {
            echo "You must enter a name";
            exit;
        }
        if(!$HTTP_POST_VARS['news']) {
            echo "You must enter some news";
            exit;
        }
		if(!$HTTP_POST_VARS['title']) {
            echo "You must enter a title";
            exit;
		}
        if(strstr($HTTP_POST_VARS['name'],"|")) {
            echo "Name cannot contain the pipe symbol - |";
            exit;
        }
        if(strstr($HTTP_POST_VARS['news'],"|")) {
            echo "News cannot contain the pipe symbol - |";
            exit;
        }
        $fp = fopen('news.txt','a');
        if(!$fp) {
            echo "Error opening file!";
            exit;
        }
        $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
        $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!";
            exit;
        }
        echo "<b>News added!</b>\n";    
    } else {
        echo "Bad Password";
    }
}

?>
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
Your name:<BR>
<INPUT TYPE="text" SIZE="30" NAME="name"><BR>
Title:<BR>
<INPUT TYPE="text" SIZE="30" NAME="title"><BR>
The News:<BR>
<TEXTAREA NAME="news" COLS="40" ROWS="5"></TEXTAREA><BR><BR>
News Password:<BR>
<INPUT TYPE="password" SIZE="30" NAME="password"><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR>
</FORM>

Hope it isnt too long. :confused2 :confused2 . The other code is the place where it is actually shown. It calls the news from news.txt.

PHP:
<?PHP
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo "<table valign='top' border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td align=left><b>" . $pieces[3] . "</b></td></tr><tr><td align='right'>" . $pieces[0] . "</td></tr><tr><td align='left'>" . $pieces[2] . "<br></td></tr><tr><td align='left'>:: " . $pieces[1] . "</td></tr></table><br><div align='center'><font color='c0c0c0'>_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _</font></div><br>";
}
?> 




</body>
</html>

</body>
</html>

They ARE inside HTML document... it took up too much room with the head parts there, so i removed them. Please, if you can figure it out, tell me why it doesnt print a title! THNX!!!!

:: Lokannon
 
actually...

It was just those 2 files... copy and past if you REALLY want it.

I was just trying to further my php skills by following a tutorial for the news script.

If you want a news script i suggest you use nphp (www.nphp.net)

I use it, and it rocks!
 
hey use this script you are good enough but i think you make some changes for example divide into pages its working. Not fully function nes scripts is better i think...
 
Back
Top