0

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!

2
  • 1
    it's called functions ... Commented May 22, 2018 at 15:04
  • Yeah, you can create a funcion that recieves a number reffering to data\dataN.txt you could send that number via a hidden field on a form, and that's it. Commented May 22, 2018 at 15:07

3 Answers 3

1

The only change in your repeated code is a single number. Just add a number to the value of your submit and use that to make the modifications:

if (isset($_POST['submit'])) {
    $num = $_POST['submit'];
    $location = 'main'.$num+1.'.html';


    $second_page_array = file("data/data$num.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: $location");
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wrapped inside of a foreach function, of course. This is exactly how I would do this.
0

you can keep the below logic in a function and call that function with some parameters when a particular page is submitted

function callPage()
{
    $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");
}

//Example:
if($_POST[submit1])
{
  echo callPage(1);
}

Comments

0

You can create a function and for switch case and pass the data for function according the case value some thing like this.

$submit = $_POST[submitvalue];
switch($submit){
    case 'submit':
         $file = data.txt;
         $headerLocation: file.html;
         functionName($file,$file);
         break;
    case 'submit1':
         $file = data1.txt;
         $headerLocation: file1.html;
         functionName($file,$file);
         break;
}

functionName(arg1,arg2){
    // do yours stuff here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.