TheLoveableMonty Posted February 9, 2009 Share Posted February 9, 2009 function editLinkSave() { global $separator; $postTitle = $_POST["title"]; $postLink = $_POST["link"]; $postContent = $_POST["content"]; $postContent = eregi_replace("\n","<br>",$postContent); str_replace("\\","",$postContent); $category = $_POST["category"]; $postFile = $_POST["filenumber"]; $categoryURL = "$category.dat"; $categoryFile = file_get_contents($categoryURL); $categorySearch = explode("\n", $categoryFile); $fp = fopen($categoryURL,"w") or die($errorMessage); foreach ($categorySearch as $line) { $compare = explode("#~#", $line); $compare[2] = trim($compare[2]); $complete = 0; if ($postFile == $compare[2] && $compare[0] != ""){ $ammend = "\n".str_replace("\\","",$postTitle)."#~#".$postLink."#~#".$postFile; if ($complete <= 0 && $compare[0] != ""){ fwrite($fp, $ammend); $complete = ($complete+1); echo "FILE CHANGED"; } else { echo "KNOCKED BACK"; } } else { fwrite($fp, $line."\n"); echo "FILE REWRITTEN"; } } fclose($fp); if ($complete != 0) { echo "<TD BGCOLOR=\"#2A338D\" HEIGHT=\"100%\" VALIGN=\"top\"><FONT FACE=\"trebuchet ms\" COLOR=\"#F3B809\" SIZE=\"3\"><B>Link edited!</B></FONT><BR>"; echo "<FONT FACE=\"trebuchet ms\" COLOR=\"#FFFFFF\" SIZE=\"2\">Your link has been edited!<BR><BR>"; echo "<A HREF=\"".$_SERVER['PHP_SELF']."\">Click here</A> to return to link index. $complete, $categoryURL. $postTitle"; echo '</FONT></TD></TR>'; } else { echo "<TD BGCOLOR=\"#2A338D\" HEIGHT=\"100%\" VALIGN=\"top\"><FONT FACE=\"trebuchet ms\" COLOR=\"#F3B809\" SIZE=\"3\"><B>Link edit failed!</B></FONT><BR>"; echo "<FONT FACE=\"trebuchet ms\" COLOR=\"#FFFFFF\" SIZE=\"2\">Nothing was edited!<BR><BR>"; echo "<A HREF=\"".$_SERVER['PHP_SELF']."\">Click here</A> to return to link index."; echo '</FONT></TD></TR>'; } } I've written this code to edit lines in a data file. It loops an array and attempts to identify the correct file number using a variable taken from a string. I've had to put many unnecessary bug fixes to get the script working, but it's left me with a huge headache. The script echoes different phrases through the if/else functions to debug how the scripts performing. For some reason, it keeps looping an unnecessary amount of times for the number of data contained. For example, it'd loop five times if there was only one line in the data file. I'm sure it's due in part to my bug fixing, but I can't seem to crack it. I've spent two solid days on this thing. Has anybody got a clue where I've went wrong? Link to comment https://forums.phpfreaks.com/topic/144523-unnecessary-loops-causing-script-problems/ Share on other sites More sharing options...
TheLoveableMonty Posted February 9, 2009 Author Share Posted February 9, 2009 Anybody? Link to comment https://forums.phpfreaks.com/topic/144523-unnecessary-loops-causing-script-problems/#findComment-758411 Share on other sites More sharing options...
TheLoveableMonty Posted February 9, 2009 Author Share Posted February 9, 2009 Alright, I'm going to try pruning the script. It's this area essentially that has me baffled. foreach ($categorySearch as $line) { $compare = explode("#~#", $line); $compare[2] = trim($compare[2]); $complete = 0; if ($postFile == $compare[2] && $compare[0] != ""){ $ammend = "\n".str_replace("\\","",$postTitle)."#~#".$postLink."#~#".$postFile; if ($complete <= 0 && $compare[0] != ""){ fwrite($fp, $ammend); $complete = ($complete+1); echo "FILE CHANGED"; } else { echo "KNOCKED BACK"; } } else { fwrite($fp, $line."\n"); echo "FILE REWRITTEN"; } } I've pruned as much as possible but can't find any resources that give an easier method. Link to comment https://forums.phpfreaks.com/topic/144523-unnecessary-loops-causing-script-problems/#findComment-758560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.