I have a PHP file that does some basic txt file manipulation and I apply the same thing for all my HTML pages with some very small differences. My question is if there is a way to add some loop or some other solution to solve all this repetition of the code:
if(isset($_POST['submit'])){
$second_page_array = file("data/data1.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$final_array = [];
//some shortening of the code
foreach($first_page_array as $key=>$first_page){
$final_array[] = $first_page.''.$second_page_array[$key].''.$ranks_array[$key].''.$time_array[$key]."\n";
}
file_put_contents('output.txt',$final_array, FILE_APPEND | LOCK_EX);
header("Location: main2.html");
}
if(isset($_POST['submit2'])){
$second_page_array = file("data/data2.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$final_array = [];
//some shortening of the code
foreach($first_page_array as $key=>$first_page){
$final_array[] = $first_page.''.$second_page_array[$key].''.$ranks_array[$key].''.$time_array[$key]."\n";
}
file_put_contents('output.txt',$final_array, FILE_APPEND | LOCK_EX);
header("Location: main3.html");
}
if(isset($_POST['submit3'])){
$second_page_array = file("data/data3.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$final_array = [];
//some shortening of the code
foreach($first_page_array as $key=>$first_page){
$final_array[] = $first_page.''.$second_page_array[$key].''.$ranks_array[$key].''.$time_array[$key]."\n";
}
file_put_contents('output.txt',$final_array, FILE_APPEND | LOCK_EX);
header("Location: main4.html");
}
So, the things that change is that submit gets incremented, data.txt and redirection to another page.
Thank you in advance!
data\dataN.txtyou could send that number via a hidden field on a form, and that's it.