Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old June 27th, 2006, 07:46 PM
dwainehead dwainehead is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 dwainehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Matt Wades News Tutorial part 2 - Need Help

Ok I'm trying to ge tthis working, but I'm having some problems, I realize this is an old tutorial but the basics of the system fit my needs.

Heres the problems:
1. Will not edit an article
2. Will not delete an article

My Testing system
win xp pro
apache 2.x
php 5

I've modified the basics of the code to run and it will post news but that's it. I get php errors from apache for undefined variable, fixed most of those, but even with that no edit/delete, here's my code as it stands now:
Code:
<?php
$PHP_SELF = $_SERVER['PHP_SELF'];
if(isset($_GET["action"]))
	{
	  $action = $_GET["action"]; }
	else
	{
	$action = "";
}
if($action == "edit" && isset($_POST['password'])) {
    //obviously you should change this password on the next line
    if($_POST['password'] == "editpass") {
        //First let's recompile that line with the pipe symbols so we can reinsert it
        $line = $_POST['date'] . "|" . $_POST['name'];
        $line .= "|" . $_POST['news'];
        $line = str_replace("rn","<BR>",$line);
        $line .= "rn";
        $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');
    $id = 0;
    $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>","rn",$pieces[2]);
    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 "The News:<BR>n";
    echo "<TEXTAREA NAME="news" COLS="40" ROWS="5">".$news."</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";
    $id++;
    exit;
}
if($action == "delete" && isset($_POST['password'])) {
    //obviously you should change this password on the next line
    if($_POST['password'] == "deletepass") {
        $data = file('news.txt');
        //this next line will remove the single news item from the array
		array_splice($data,$_GET['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');
    $id = $_GET['id'];
	$element = trim($data[$id]);
    $pieces = explode("|", $element);
    echo $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&id=$id" METHOD="POST" NAME="deleteform">n";
    echo "Password:<BR>n";
    echo "<INPUT TYPE="password" SIZE="30" NAME="password" value="ok"><BR>n";
    echo "<INPUT TYPE="hidden" NAME="id" VALUE="$id">n";
    echo "<INPUT TYPE="submit" NAME="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);
$id = 0;
foreach($data as $id=>$element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>n";
    echo "&nbsp;<a href="$PHP_SELF?action=delete&id=$id">Delete</a>n";
    echo "&nbsp;<a href="$PHP_SELF?action=edit&id=$id">Edit</a>n";
    echo "<BR><BR>n";
    $id++;
}

echo "<HR>n";
echo "<H1><u>Add News</u></H1>n";
if(@$_POST['submit']) {
    if($_POST['password'] == 'pass') {
        if(!$_POST['name']) {
            echo "You must enter a name";
            exit;
        }
        if(!$_POST['news']) {
            echo "You must enter some news";
            exit;
        }
        if(strstr($_POST['name'],"|")) {
            echo "Name cannot contain the pipe symbol - |";
            exit;
        }
        if(strstr($_POST['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") . "|" . $_POST['name'];
        $line .= "|" . $_POST['news'];
        $line = str_replace("rn","<BR>",$line);
        $line .= "rn";
        fwrite($fp, $line);
        if(!fclose($fp)) {
            echo "Error closing file!";
            exit;
        }
        echo "<b>News added!</b>n";
    } else {
        echo "Bad Password";
    }
}

?>
<FORM ACTION="<?php $PHP_SELF?>" METHOD="POST" NAME="newsentry">
Your name:<BR>
<INPUT TYPE="text" SIZE="30" NAME="name"><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>


I was getting undefined id, but the change's i've made fix that, I checked souce before I post and I can see that I am passing the post id # to the form(or next point in the script), but I get a response back of a complete sucessfull action, when infact the post was not deteted or edited.

Any help with this would be appreciated highly.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Matt Wades News Tutorial part 2 - Need Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway