Staredit Network

Staredit Network -> Computers and Technical -> [PHP] Deleting...
Report, edit, etc...Posted by Pie_Sniper on 2006-07-02 at 00:35:05
CODE
   $Title = "";
   $Date = "";
   $Post = "";
   $Number = $_POST['Number'];
   $Filename = "Data/PostData/Posts.txt";
   $Posts = file_get_contents( $Filename );
   if( $Number < 1 || $Number > $Posts )
   {
     header( "Location: Manager.php?Mng=Posts&Act=Delete" );
     end();
   }
   file_put_contents( $Filename, $Posts - 1 );
   for( $I = $Number + 1; $I <= $Posts; $I++ )
   {
     $Filename = "Data/PostData/Titles/" . $I . ".txt";
     $Title = file_get_contents( $Filename );
     $Filename = "Data/PostData/Dates/" . $I . ".txt";
     $Date = file_get_contents( $Filename );
     $Filename = "Data/PostData/Posts/" . $I . ".txt";
     $Post = file_get_contents( $Filename );
     $Filename = "Data/PostData/Titles/" . $I - 1 . ".txt";
     file_put_contents( $Filename, $Title );
     $Filename = "Data/PostData/Dates/" . $I - 1 . ".txt";
     file_put_contents( $Filename, $Date );
     $Filename = "Data/PostData/Posts/" . $I - 1 . ".txt";
     file_put_contents( $Filename, $Post );
   }
   $Filename = "Data/PostData/Titles/" . $Posts . ".txt";
   unlink( $Filename );
   $Filename = "Data/PostData/Dates/" . $Posts . ".txt";
   unlink( $Filename );
   $Filename = "Data/PostData/Posts/" . $Posts . ".txt";
   unlink( $Filename );
   header( "Location: Manager.php?Mng=Posts&Act=Delete" );
   end();

Does this (hackish) code look sound to you? Guess again! As far as my logic says, this will take the next file's contents and stick them in the deleted post's file, continue with the rest of the files, and then delete the last file. But it doesn't... Instead, it just deletes the last file and sticks -1.txt with the next post's contents in the root directory. Why?!

P.S. Yeah, I know it's kind of an ugly system. I'm going to convert it all to serialized arrays soon. I might move to SQL but I don't know...

Oh, and don't worry, that's not the code in its entirety. (The rest is irrelevant.)
Report, edit, etc...Posted by RexyRex on 2006-07-03 at 18:25:27
CODE
function end() {
}
???

Start dumping stuff.
CODE
print_r($Filename);
print_r($Posts);
Report, edit, etc...Posted by Pie_Sniper on 2006-07-03 at 19:21:35
Oh yeah, it was supposed to be exit(). I'll start checking things then...

:: Edit
Okay, problem solved. I needed to change it to
QUOTE
    $Filename = "Data/PostData/Titles/" . ( $I - 1 ) . ".txt";
      file_put_contents( $Filename, $Title );
      $Filename = "Data/PostData/Dates/" . ( $I - 1 ) . ".txt";
      file_put_contents( $Filename, $Date );
      $Filename = "Data/PostData/Posts/" . ( $I - 1 ) . ".txt";
      file_put_contents( $Filename, $Post );
Next Page (1)