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:
  #1  
Old July 7th, 2004, 06:17 PM
Jehuty77 Jehuty77 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 1 Jehuty77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
news system edit/delete problems PLZ HELP

hi there im working on the news system provided by matt wades tutorial on this site.

im ahving a bit of trouble getting it 2 work properly. after getting the action to work i can now click edit or delete on my posts, but when i do it jsut takes me 2 the last entry in the file and wants to edit/delete that entry instead of the one i selected.

im having a hard time explaining this so maybe u guys wanna jsut take a look at the code. here is a link 2 a example i have uploaded to my website.

http://www.animenexus.gotdns.com/php-tests/news/addnews-beta.php

php Code:
Original - php Code
  1.  
  2.  
  3. <HTML>
  4. <HEAD></HEAD>
  5. <BODY>
  6. <?
  7.  
  8. error_reporting ('E_NONE');
  9. if($_GET['action'] == "edit" && isset($HTTP_POST_VARS['password'])) {
  10.     //obviously you should change this password on the next line
  11.     if($HTTP_POST_VARS['password'] == "editpass") {
  12.         //First let's recompile that line with the pipe symbols so we can reinsert it
  13.         $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name'];
  14.         $line .= "|" . $HTTP_POST_VARS['news'];
  15.         $line = str_replace("rn","<BR>",$line);
  16.         $line .= "rn";
  17.         $data = file('news.txt');
  18.         $data[$id] = $line;
  19.         //the next line makes sure the $data array starts at the beginning
  20.         reset($data);
  21.         //now we open the file with mode 'w' which truncates the file
  22.         $fp = fopen('news.txt','w');
  23.         foreach($data as $element) {
  24.             fwrite($fp, $element);
  25.         }
  26.         fclose($fp);
  27.         echo "Item Edited!<BR><BR>n";
  28.         echo "<a href="$PHP_SELF">Go Back</a>n";
  29.         exit;
  30.     } else {
  31.         echo "Bad password!n";
  32.         exit;
  33.     }
  34. }
  35. if($_GET['action'] == "edit") {
  36.     $data = file('news.txt');
  37.     $element = trim($data[$id]);
  38.     $pieces = explode("|", $element);
  39.     //the next line is to reverse the process of turning the end of lines into breaking returns
  40.     $news = str_replace("<BR>","rn",$pieces[2]);
  41.     echo "Make the changes you would like and press save.<BR>n";
  42.     echo "<FORM ACTION="$PHP_SELF?action=edit" METHOD="POST" NAME="editform">n";
  43.     echo "Name:<BR>n";
  44.     echo "<INPUT TYPE="text" SIZE="30" NAME="name" value="".$pieces[1].""><BR>n";
  45.     echo "The News:<BR>n";
  46.     echo "<TEXTAREA NAME="news" COLS="40" ROWS="5">".$news."</TEXTAREA><BR><BR>n";
  47.     echo "Password:<BR>n";
  48.     echo "<INPUT TYPE="password" SIZE="30" NAME="password"><BR>n";
  49.     echo "<INPUT TYPE="hidden" NAME="date" VALUE="".$pieces[0]."">n";
  50.     echo "<INPUT TYPE="hidden" NAME="id" VALUE="$id">n";
  51.     echo "<INPUT TYPE="submit" NAME="submit" VALUE="Save"><BR>n";
  52.     echo "</FORM>n";
  53.     exit;
  54. }
  55. if($_GET['action'] == "delete" && isset($HTTP_POST_VARS['password'])) {
  56.     //obviously you should change this password on the next line
  57.     if($HTTP_POST_VARS['password'] == "deletepass") {
  58.         $data = file('news.txt');
  59.         //this next line will remove the single news item from the array
  60.         array_splice($data,$id,1);
  61.         //now we open the file with mode 'w' which truncates the file
  62.         $fp = fopen('news.txt','w');
  63.         foreach($data as $element) {
  64.             fwrite($fp, $element);
  65.         }
  66.         fclose($fp);   
  67.         echo "Item deleted!<BR><BR>n";   
  68.         echo "<a href="$PHP_SELF">Go Back</a>n";
  69.         exit;
  70.     } else {
  71.         echo "Bad password!n";
  72.         exit;
  73.     }
  74. }
  75. if($_GET['action'] == "delete") {
  76.     echo "<H2>You are about to delete the following news item.</H2>n";
  77.     $data = file('news.txt');
  78.     $element = trim($data[$id]);
  79.     $pieces = explode("|", $element);
  80.     echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>n";
  81.     echo "<BR><BR>n";
  82.     echo "Are you sure you want to delete this news item? If so, enter the password and click on Delete.<BR>n";
  83.     echo "<FORM ACTION="$PHP_SELF?action=delete" METHOD="POST" NAME="deleteform">n";
  84.     echo "Password:<BR>n";
  85.     echo "<INPUT TYPE="password" SIZE="30" NAME="password"><BR>n";
  86.     echo "<INPUT TYPE="hidden" NAME="id" VALUE="$id">n";
  87.     echo "<INPUT TYPE="submit" NAME="submit" VALUE="Delete"><BR>n";
  88.     echo "</FORM>n";
  89.     exit;
  90. }
  91.  
  92.  
  93. echo "<H1><u>Current News</u></H1>n";
  94. $data = file('news.txt');
  95. //next line removed to make everything else easier in the admin script
  96. //$data = array_reverse($data);
  97. foreach($data as $key=>$element) {
  98.     $element = trim($element);
  99.     $pieces = explode("|", $element);
  100.     echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>n";
  101.     echo "&nbsp;<a href="$PHP_SELF?action=delete&id=$key">Delete</a>n";
  102.     echo "&nbsp;<a href="$PHP_SELF?action=edit&id=$key">Edit</a>n";
  103.     echo "<BR><BR>n";
  104. }
  105. echo "<HR>n";
  106. echo "<H1><u>Add News</u></H1>n";
  107. if($HTTP_POST_VARS['submit']) {
  108.     if($HTTP_POST_VARS['password'] == 'pass') {
  109.         if(!$HTTP_POST_VARS['name']) {
  110.             echo "You must enter a name";
  111.             exit;
  112.         }
  113.         if(!$HTTP_POST_VARS['news']) {
  114.             echo "You must enter some news";
  115.             exit;
  116.         }
  117.         if(strstr($HTTP_POST_VARS['name'],"|")) {
  118.             echo "Name cannot contain the pipe symbol - |";
  119.             exit;
  120.         }
  121.         if(strstr($HTTP_POST_VARS['news'],"|")) {
  122.             echo "News cannot contain the pipe symbol - |";
  123.             exit;
  124.         }
  125.         $fp = fopen('news.txt','a');
  126.         if(!$fp) {
  127.             echo "Error opening file!";
  128.             exit;
  129.         }
  130.         $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
  131.         $line .= "|" . $HTTP_POST_VARS['news'];
  132.         $line = str_replace("rn","<BR>",$line);
  133.         $line .= "rn";
  134.         fwrite($fp, $line);
  135.         if(!fclose($fp)) {
  136.             echo "Error closing file!";
  137.             exit;
  138.         }
  139.         echo "<b>News added!</b>n";   
  140.     } else {
  141.         echo "Bad Password";
  142.     }
  143. }
  144.  
  145. ?>
  146. <FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
  147. Your name:<BR>
  148. <INPUT TYPE="text" SIZE="30" NAME="name"><BR>
  149. The News:<BR>
  150. <TEXTAREA NAME="news" COLS="40" ROWS="5"></TEXTAREA><BR><BR>
  151. News Password:<BR>
  152. <INPUT TYPE="password" SIZE="30" NAME="password"><BR>
  153. <INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR>
  154. </FORM>
  155. </BODY>
  156. </HTML>
  157.  




Reply With Quote
  #2  
Old July 7th, 2004, 06:21 PM
System System is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Posts: 665 System User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Message Moved

Thread moved from 'PHP Coding' to 'Tutorials' by notepad.

Reason: wrong place

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > news system edit/delete problems PLZ 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 |