Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: PHP Delete Function

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Delete Function

    Hello, I am making a script that ads links to a .txt file when the form is submitted. I have the ad link part working fine, but I need to know how to go about making a delete function to remove the link from the .txt file. I am not 100% sure this can be done with a .txt file, but let me know either way. Thanks

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Clearing the text file is really easy.
    $textfile = "";
    Assuming you have used fopen() to set $textfile to the file, and such.
    [Also, depending on the method used to open the file (w+/a/etc., I can't remember what's what at the moment), you might be just adding "" to it, not setting it to that, but it's possible.]

    If you wish to remove a specific line from the text file, then you would do basically the same, but do this instead:
    Assume $textfile is the contents of the .txt, again.
    str_replace($link,"",$textfile);

    Here's how it works: str_replace(FIND, REPLACE, IN_THIS);

    You may also want to use something like
    str_replace($link."\n","",$textfile);

    That would replace the link, then the line break after it.


    Hope this helps.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33
    Clearing the text file is really easy.
    $textfile = "";
    Assuming you have used fopen() to set $textfile to the file, and such.
    [Also, depending on the method used to open the file (w+/a/etc., I can't remember what's what at the moment), you might be just adding "" to it, not setting it to that, but it's possible.]

    If you wish to remove a specific line from the text file, then you would do basically the same, but do this instead:
    Assume $textfile is the contents of the .txt, again.
    str_replace($link,"",$textfile);

    Here's how it works: str_replace(FIND, REPLACE, IN_THIS);

    You may also want to use something like
    str_replace($link."\n","",$textfile);

    That would replace the link, then the line break after it.


    Hope this helps.
    Ok, I get what you are saying about replacing the link with a blank, but how would I make it so that when a link is added, it also ads a delete option to it. I am thinking that I could use a link ID that autoincrements and then use the ID to reference the link. If that will work, the only thing that i don't know how to do is to make the delete function get the corresponding ID.

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    We need to see the code if you want anything but general answers. However, I think the ID plan is a bad idea. Store the text of line to delete in your form, and use that to find the line.

  5. #5
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, here is the code for you
    Processing
    PHP Code:
    <?php
    $filename 
    "featured_links.txt"// file to write to

    $date date("F j, Y"); //date format

    $error "An error occured while adding new link!";

    $success "Link successfully added, please choose the page you would like to go to below. You will be automatically redirected to the ad links page in 10 seconds. 
                <br /> 
                <a href=\"http://ad_link.php\">Ad Links Page</a>
                <br />
                <a href=\"jexhost.com\">Site Home</a>"
    ;



        
    // Add Text
    $text stripslashes($_POST['text']);
        if (!
    $text) {
        
    $text "<b>Untitled</b>";
        }
        
        
    // Add Link
    $link stripslashes($_POST['link']);
        if (!
    link) {
        
    $link "";
        }
        
        
        
    // Add Content
    if ($content stripslashes($_POST['content'])) {
        
    $add '
    <!-- Text Ad, '
    .$date.' -->
    <a href="http://'
    .$link.'">'.$title.'</a>
        <br />
        
    '
    ;
        

    $links = @file_get_contents($filename).$add;
        

        
    $file = @fopen($filename"w");
        @
    fwrite($file$news);
        @
    fclose($file);
        
    $message $success;
        }
    else {
        
    $message $error;
        }
    echo 
    "
    <html>
    <head>
    <title>Jexlinks.com | Link Added</title>
    <meta http-equiv=\"refresh\" content=\"10;url=ad_link.php\">
    "
    .$message."
    </head>
    <body>
    </body>
    </html>"
    ;


    ?>
    Add Form
    PHP Code:
    <form action="ad_link_process.php" method="post">
                            
    Link Text:
                            <
    br />
                                <
    input name="text" type="text" />
                            <
    br />
                            
    Link URL:
                            <
    br />
                                <
    input name="link" type="text" />
                            <
    br />
                                <
    input type="submit" value="Submit" />
                          </
    form

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    search/replace works well to remove a link, since you are removing the same thing you're searching for. ID is only important if you don't have the data you're searching for.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33
    search/replace works well to remove a link, since you are removing the same thing you're searching for. ID is only important if you don't have the data you're searching for.
    The thing is that I don't know the data I am searching for. Each link that is added can ad any data, and I need a delete function that will be able to be added to each new link that is added.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yeah. You do know what you're searching for.
    When you delete a link, you obviously know the link at that point. Use it to delete it.
    Store it as $link, then search/replace $link.
    You don't need to hard code the urls into the script.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33
    Yeah. You do know what you're searching for.
    When you delete a link, you obviously know the link at that point. Use it to delete it.
    Store it as $link, then search/replace $link.
    You don't need to hard code the urls into the script.
    What do you mean by not hard storing the link? When the link is written, it looks like this:
    HTML Code:
    <!-- Text Ad, Added October 15, 2006 -->
    	<a href="#" target="_blank">- Testing Link</a>
    <br />
    And that is the code that is entered, so being that there is no $link, i don't know how i could reference the correct link. There will be hundreds of links, so I need a unique ID to be added to each link so that i can click on the "delete" button and execute the delete function for that link.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    How does a user navigate to the link? It's the href of the link, yes?
    Using that href, can store it as $link, through php code, not just waiting for it to show up.... I didn't mean it would automatically be there.

    Somehow, you have the url available. Use that url to add the delete link....
    http://..../delete.php?url=urlhere
    And that should delete it.

    There is absolutely no need for an ID as it just ads one step and you'd STILL need the url in the end. Instead of messing with IDs, just use the url itself, which you MUST!!! have available to link the person.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •