IchBin Posted November 21, 2008 Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/ Share on other sites More sharing options...
premiso Posted November 21, 2008 Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695558 Share on other sites More sharing options...
premiso Posted November 21, 2008 Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695568 Share on other sites More sharing options...
IchBin Posted November 21, 2008 Author Share Posted November 21, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695571 Share on other sites More sharing options...
premiso Posted November 21, 2008 Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695579 Share on other sites More sharing options...
IchBin Posted November 21, 2008 Author Share Posted November 21, 2008 Woops... that deleted everything in the file. lol Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695582 Share on other sites More sharing options...
premiso Posted November 21, 2008 Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695590 Share on other sites More sharing options...
IchBin Posted November 21, 2008 Author Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695652 Share on other sites More sharing options...
IchBin Posted November 21, 2008 Author Share Posted November 21, 2008 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/133674-solved-write-to-a-flatfile-array-help/#findComment-695762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.