Jump to content

Deleting lines from flat file problem


Topsy Turvey

Recommended Posts

Hi!

 

I'm having some trouble with the below script.

 

I am trying to display all the news items in my flat file with 'delete' links (<a href="managedeletenews.php?action=delete&id=$key">Delete</a>) that call the same page

to strip out the line and rewrite the file.

 

However, when the delete link is clicked nothing happens (except the page's address changing from "managedeletenews.php" to the above.

 

Am I using the references to the $id or $key variables incorrectly?

Is it something to do with the undefined $action variable?

Or is it something else??

BTW, the embedded styles for the delete link - shown below - don't seem to be throwing up any errors.

 

I'm quite new to php so I hope I haven't missed anthing too obvious...thank you!  :)

 


<?PHP
if($action == "delete" && isset($_POST['submit'])) {
        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 '<p class="info">Item deleted!<br/><br/></p>';
    }
if($action == "delete") {
    echo '<p class="info">You are about to delete the following news item.</p>';
    $data = file('news.txt');
    $element = trim($data[$id]);
    $pieces = explode("||", $element);
    echo '<div class="newshead">'.$pieces[0].'<div class="newstime">'.$pieces[3].'</div></div>';
echo '<p class="newsmessage">'.$pieces[2].'</p>';
echo '<h2>posted by '.$pieces[1].'</h2>';
    echo '<br/><br/>';
    echo '<p class="info">Are you sure you want to delete this news item?</p>';
    echo '<form action="managedeletenews.php?action=delete" method="post" name="deleteform">';
    echo '<input type="hidden" name="id" value="$id"><br/><br/>';
    echo '<input type="submit" name="submit" value="Delete">';
    echo '</form>';
    exit;
}
?>

<?php
$data = file('news.txt');
foreach($data as $key=>$element) {
    $element = trim($element);
    $pieces = explode("||", $element);
$pieces[2] = str_replace("<br/>"," ",$pieces[2]);
$pieces[2] = substr ($pieces[2], 0, 40);
echo '<div class="newshead">'.$pieces[0].'<div class="newstime">'.$pieces[3].'</div></div>';
echo '<p class="newsmessage" style="background:#565656;"><a style="display:block; padding:2px; border:1px solid #000; color:#fff; float:right; width:37px; height:17px; line-height:17px;" href="managedeletenews.php?action=delete&id=$key">Delete</a> '.$pieces[2].'...</p>';
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/49176-deleting-lines-from-flat-file-problem/
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.