Jump to content

Recommended Posts

So I have a little script I'm working on, just for learning PHP sake. :) Here's what I'm doing.

 

One page has an input box that you can submit a link to. It then writes the link to the flatfile. But now I want to make it so I can remove the links. I can't figure out how to make it work right, so I'll post some code with an explanation shortly. On my edit links page I create the list of links by reading the file, looping through and output a list of links in a form with a checkbox next to each link. Here's what the input form code looks like:

        $getLinks = file($this->feed_file);
        echo '
        <form method="post" action="index.php?page=edit">';

        foreach ($getLinks as $key => $value)
        {
            echo '
            <input type="checkbox" name="links[',$key,']" />',$value,'<br />';
        }
        echo '
            <input type="submit" name="sub_delete" value="Delete Links" />
        </form>';

 

For the removal of links I have this code.

        $file = file($this->feed_file);
        $links = '';

        for ($i = 0; $i < count($file); $i++)
        {
            if (!in_array($i, $_POST['links']))
            {
			    $links .= $file[$i];
            }
        }

        $handle = fopen($this->feed_file, "w");
        if ($handle)
        {
            fwrite($handle, $links);
        }
        fclose($handle);
    }

 

What I was thinking in the removal code above, is to check to see if the loop counter is "not" in the $_POST['links'] array. If it isn't, then store the link in the $links array. Then write the links back to the file. I know this may not be the best way to do things, but I'm just looking to make this work so I can understand it fully.

 

The problem: Removing a link always removes the first element of the file. Checking multiple boxes does the same thing. I can't wrap my brain around this to make it work just yet. Any input is appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/
Share on other sites

Part of your problem here:

        foreach ($getLinks as $key => $value)
        {
            echo '
            <input type="checkbox" name="links[',$key,']" />',$value,'<br />';
        }

 

Is you are using commas instead of periods for concatenation.

 

EDIT:

 

Ok figured it out. This should work.

$file = file($this->feed_file);
        $links = '';

        for ($i = 0; $i < count($file); $i++) {
     if ($_POST['links'][$i] != "on") {
           $links .= $file[$i];
            }
        }

        $handle = fopen($this->feed_file, "w");
        if ($handle)
        {
            fwrite($handle, $links);
        }
        fclose($handle);

 

Basically just re-worked the logic since a checkbox is returned as "on" the above should do the right checks and omit the right links.

Updated version:

 

$file = file($this->feed_file);
        $links = '';

        for ($i = 0; $i < count($file); $i++) {
     if ($_POST['links'][$i] != "on") {
           $links .= $file[$i] . "\n";
            }
        }

        $handle = fopen($this->feed_file, "w");
        if ($handle)
        {
            fwrite($handle, $links);
        }
        fclose($handle);

 

added a \n to the links so it writes the new links on seperate lines like expected.

That seems to work, however I'm getting an undefined offset for this line of code:

 

if ($_POST['links'][$i] != 'on')

 

I have error reporting turned on, and this is what it outputs for the line above.

Notice: Undefined offset: 0 in /var/www/rss/class.php on line 86

 

Notice: Undefined offset: 1 in /var/www/rss/class.php on line 86

 

Notice: Undefined offset: 2 in /var/www/rss/class.php on line 86

 

Notice: Undefined offset: 3 in /var/www/rss/class.php on line 86

 

Thanks for the help!

That seems to work, however I'm getting an undefined offset for this line of code:

 

if ($_POST['links'][$i] != 'on')

 

I have error reporting turned on, and this is what it outputs for the line above.

Notice: Undefined offset: 0 in /var/www/rss/class.php on line 86

 

Notice: Undefined offset: 1 in /var/www/rss/class.php on line 86

 

Notice: Undefined offset: 2 in /var/www/rss/class.php on line 86

 

Notice: Undefined offset: 3 in /var/www/rss/class.php on line 86

 

Thanks for the help!

 

$file = file($this->feed_file);
        $links = '';

        for ($i = 0; $i < count($file); $i++) {
     if (isset($_POST['links'][$i]) && $_POST['links'][$i] != "on") {
           $links .= $file[$i] . "\n";
            }
        }

        $handle = fopen($this->feed_file, "w");
        if ($handle)
        {
            fwrite($handle, $links);
        }
        fclose($handle);

 

That should work, not 100% sure. Try that and let me know if not I do know the solution.

Woops... that deleted everything in the file. lol

 

Yea I figured. Let's try this route instead.

 

        $getLinks = file($this->feed_file);
        echo '
        <form method="post" action="index.php?page=edit">';

        foreach ($getLinks as $key => $value)
        {
            echo '
            <input type="checkbox" name="links['.$key.']" />'.$value.'<br />'; // note change here
        }
        echo '
            <input type="submit" name="sub_delete" value="Delete Links" />
        </form>';

 

$file = file($this->feed_file);
        $links = "";

        if (isset($_POST['links'])) {
	foreach ($_POST['links'] as $key => $val) {
				if ($val == "on") {
					unset($file[$key]);
				}
	}

	$links = implode("\n", $file);
}


        $handle = fopen($this->feed_file, "w");
        if ($handle)
        {
            fwrite($handle, $links);
        }
        fclose($handle);

 

That should be a bit better...and work lol.

 

EDIT:

Removed the extra index part because it was unecessary. Should yield the same results.

That worked better, but not quite there yet. I'm not sure where its happening, but its inserting an extra \n in between each link. So now when I view the edit links page is shows an empty line with a checkbox in between each result. Thanks for your help, this has been a great learning experience to see how it can be done different ways.

Bah... never mind I figured it out. For some reason I thought that implode was separating the array by the already existing \n that I input when I add the links. I removed the \n in the implode function and everything is functioning normally. Thanks!!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.