Page 1 of 1

my Delete-News-Post(s) Script: HELP!

Posted: Fri Jun 11, 2004 12:50 am
by mhulse
Hello,

I am trying to incorporate a delete-news script into the admin news section of my site.

I am not using any MYsql - my server does not support that.

The delete-news script I am using (see below) will display my news items, but, when I click a post's delete link nothing happens...

Here is the delete script:

Code: Select all

<?
if($action == "delete") &#123;
        $data = file('news.dat');
        //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.dat','w');
        foreach($data as $element) &#123;
            fwrite($fp, $element);
        &#125;
        fclose($fp);
        echo "Item deleted!<BR><BR>\n";
        echo "<a href="$PHP_SELF">Go Back</a>\n";
        exit;
&#125;

echo "<H1><u>Current News</u></H1>\n";
$data = file('news.dat');
//next line removed to make everything else easier in the admin script
//$data = array_reverse($data);
foreach($data as $key=>$element) &#123;
    $element = trim($element);
    $pieces = explode("|", $element);
    echo $pieces&#1111;2] . "<BR>" . "<b>Posted by " . $pieces&#1111;1] . " on " . $pieces&#1111;0] . "</b>\n";
    echo "&nbsp;<a href="$PHP_SELF?action=delete&id=$key">Delete</a>\n";
    echo "<BR><BR>\n";
  &#125;
  
?>
Here is an example news.dat file(chmod = 777):

Code: Select all

10 Jun 2004%~#This is a title%~#this is the post body.
10 Jun 2004%~#This is a test!%~#Yep, just like the title says!
And here is my add-news script:

Code: Select all

<?
require('config.php');

if($_POST&#1111;'do']=='addnew') &#123;
	$title=$_POST&#1111;'title'];
	$news=$_POST&#1111;'news'];
	$fp=fopen(NEWS_FILE,"a");
	$formatted=str_replace("\r\n","<br>",$news);
	$formatted=str_replace("\n","<br>",$formatted);
	$newsto=date("d M Y")."%~#".$title."%~#".$formatted;
	if(ENCODING=='yes') $newsto=base64_encode($newsto);
	fwrite($fp,StripSlashes($newsto)."\n");
	fclose($fp);
	echo ' news added... ';
&#125;
?>
Can anyone help me with this? How can I get it to delete my entries? Does anyone know of a better delete script? Any help would be greatly appreciated. :)

Cheers
micky

Posted: Fri Jun 11, 2004 1:05 am
by markl999
if($action == "delete") {
$data = file('news.dat');
//this next line will remove the single news item from the array
array_splice($data,$id,1);
Shouldn't that be:

Code: Select all

if($_GET['action'] == "delete") {
        $data = file('news.dat');
        //this next line will remove the single news item from the array
        array_splice($data,$_GET['id'],1);

Posted: Fri Jun 11, 2004 1:41 am
by mhulse
Wow! Thanks for the quick response! I really appreciate the help! :)

Hmmm, that gives me a parse error... "Unexpected T_STRING"... on this line of code (line 11):

Code: Select all

$data = file('news.dat');
I will keep plugging away...

Thanks for the help! :D You gave me some good ideas. I will keep you updated.


Cheers
Micky

Posted: Fri Jun 11, 2004 2:15 am
by mhulse

Code: Select all

if($_POST&#1111;'action'] == "delete") &#123;
        $data = file('news.dat');
        //this next line will remove the single news item from the array
        array_splice($data,$id,1);
The above syntax produces no errors, but the entries still do not delete. I guess this is what happens when you are a PHP noobie and decide to use deprecated syntax from old scripts... (not you, me... hehe...)

I just discovered PHP error_reporting:

http://us4.php.net/error_reporting

Hopefully implementing error_reporting will help me fix my problems... :)

Thanks all! I am still open to suggestions. :D

Posted: Fri Jun 11, 2004 2:18 am
by markl999
As your deleting via a link you need to use $_GET['action'] and $_GET['id'] instead of $_POST['action'] and $id.
My previous example should have worked fine, no idea how you managed to get an Unexpected T_STRING out of it :o

Posted: Fri Jun 11, 2004 2:38 am
by mhulse
WHa!! It works now!!! LOL, strange, it did not work before... browser cache??? Anyway, Thanks!!!!!!! :D

Code: Select all

if($_GET&#1111;'action'] == "delete") &#123;
        $data = file('news.dat');
        //this next line will remove the single news item from the array
        array_splice($data,$_GET&#1111;'id'],1);
PHP rocks!

Thank you!

Cheers!
<m />