SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multiple line deletion

    Hello, i've got a simple problem i can't figure out. I've got a text file database, i need to delete lines with a certain detail.

    My file looks like this:

    Code:
    <?php die(); ?>
    a|1|September 3rd 07|1|1
    a|2|September 3rd 07|2|1
    a|2|September 3rd 07|2|1
    a|3|September 3rd 07|3|1
    and my code:

    Code:
    $filearray = file($commentfile);
    unset($filearray[0]);
    
    foreach($filearray as $lnum => $thisline){
    $break = "|";
    list($name_comm, $comment, $date, $id, $status)=explode($break,$thisline);
    
    if($_GET['delete'] == $id){
    unset($filearray[$lnum]);
    }
    }
    
    $fd = fopen ($commentfile, "w");
    fwrite ($fd, "<?php die(); ?>\n");
    
    $all = count($filearray);
    for($i = 1; $i < $all; $i++) {
    fwrite ($fd, $filearray[$i]);
    }
    
    fclose($fd);
    When the script is used to, for example delete lines with 2 in, it deletes those *and* lines with 1 in. I think this is because when the line is unset, it doesn't match the $lnum. But i'm not sure.

    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would read each line of the file and put it into an array. Then loop the array and whatever doesn't match put it in another array, then write to file.

  3. #3
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thought it'd be that simple. Thanks

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a reason for using a Text based DB? Would it not be better to use XML ?

    If you must use text based, then i'd load each line into an array, and then write the file back but use an if to only write back if it doesn't match the id.

    ie..

    Code:
    <?php
    
        $lines = array(); // this would contain all the lines from the file.
    
        foreach($lines as $line) {
            $lineData = explode('|', $line);
            if(lineData[0] != $_GET['delete'])
                fwrite($fd, $line);
        }
    
        fclose($fd);
    
    ?>


  5. #5
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try it.

    I don't have/ need an SQL database.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script removes from teh array, the one I posted does not. It simply goes through the array and writes to the file but ignores any lines that match your criteria


  7. #7
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It still has the same problem, although it seems impossible. I must have an error somewhere else.

  8. #8
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be an error with your data then. As long as your data is seperated by new lines, and each line is then in turn seperated by pipes (|) then the script will work.


  9. #9
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $filearray file($commentfile);
    echo 
    "<pre>";
    print_r ($filearray);
    echo 
    "</pre>"
    That should show on screen what PHP is seeing as the file data in the first array. Might help in debugging.
    Ian Anderson
    www.siteguru.co.uk

  10. #10
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think i've found the problem. $_GET['delete'] wasn't the id, it was the line number.

  11. #11
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Text File DBs are so inefficient. Not to mention old very very old.
    No wonder man invent better DB system then plain old text files.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #12
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry but i'm not the type of person who buys things because they're trendy I'm not paying 1/3 more for my hosting until i *actually* need a database. I'm handling 600+ members, 2500 links and 40 tutorials very well for now.

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What? buy what things? MySQL is free, SQLite is free, XML is free. What do you mean by "buy" you are not buying a thing. They are trendy for a reason they work really really damn well.

    You need to get your facts straight. If they were trendy they would not be around for as long as they are. So they are not trendy they are here until someone finds something better. And plain text is not that.

    Oh also Orcal has afree developer edition.
    Microsoft has a free version of SQL Server 2005.

    There are so many free Relational database systems you have nothing to buy.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not everyone on this planet has a server. I have to rent space on someone elses. This means paying more for features.

  15. #15
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If your server does not come with PHP and MySQL you are being ripped off.

    The cheapest web host on the plant package PHP and MySQL together. If your host doesn't give that to you find a new host because seriously I've not seen a hosting package that doesn't have those two item in it.

    I was paying less then $80 bucks a year and I got that I could probably pay less and still get it.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  16. #16
    SitePoint Enthusiast pea's Avatar
    Join Date
    Jul 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i'm paying about $40 (&#163;20). This is english, i'm sure i can pay dirt for a host in america. I can upgrade to a package with mysql for &#163;30 ($60) but as i said, i DO NOT NEED IT (please read twice).


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
  •