Results 1 to 2 of 2

Thread: Line deleting using Array_Diff or str_replace();?

  1. #1
    Join Date
    May 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Line deleting using Array_Diff or str_replace();?

    HI,

    I am new here. I need help> I am stuck in my project

    My work, I mean I am trying to do is that first show all the contents of my text file. With each having a Text [Delete!] and after, once any one of them is clicked, It will delete that line from the text file database and imploded(); the lines again and write back to database. Now the code I am using is this ;

    <?php

    $db = "commentsdb.txt";


    if($_GET["delete"])
    {
    $data = file($db);

    foreach($data as $entry)
    {
    $data_remove = array_diff($entry , array($data));
    }
    implode("",$data_remove);
    $handle = fopen($db,'w');
    fwrite($handle,$data_remove);
    fclose($handle);

    }else

    $data = file($db);

    foreach($data as $entry)
    {
    echo $entry . "<a href='moderation.php?delete=" . $entry . "'>[Delete!]</a>";
    }
    ?>
    All goes Ok, It shows as I want. But the problem is once I hit a delete on next page I get these errors

    Code:
    Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\server\htdocs\moderation.php on line 12
    
    Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\server\htdocs\moderation.php on line 12
    
    Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\server\htdocs\moderation.php on line 12
    
    Warning: implode() [function.implode]: Invalid arguments passed in C:\server\htdocs\moderation.php on line 14
    Thanks i Badly need help

    Thanks a lot

    edit,
    I just saw a thread saying that this work can done with str_replace(); and it's more easier So I don't mind. If the array_diff(); Don't work I can easily work with str_replace(); can any one suggest me how can I use that function according to my script needs Thanks a lot

    FAISAL!

  2. #2
    Join Date
    May 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi thanks A new update, I used this code, str_replace(); in my code, Once I hit that, it deletes all the database contents, All the file contents and file becomes Blank Please help me> I just only want to delete the file / the line that is clicked to be deleted

    Thanks here is the code,

    Code:
    <?php
    
    $db = "commentsdb.txt";
    
    
    if($_GET["delete"])
    {
    $data = file($db);
    
    foreach($data as $entry)
    {
    # $data_remove = array_diff($data, array($entry));
    $data_remove = str_replace($data,"",$entry);
    #$data_remove = explode($data,"",$entry);
    }
    
    
    $handle = fopen($db,'w');
    fwrite($handle, $data_remove);
    fclose($handle);
    
        
    }else
        
    $data = file($db);
    
    foreach($data as $entry)
    {
    echo $entry . "<a href='moderation.php?delete=" . $entry . "'>[Delete!]</a>";
    }    
        ?>
    Looking forward for help

    FAISAL!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •