Rulli Smith
Rulli Smith

Reputation: 353

PHP use loops to avoid repetition of code

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!

Upvotes: 0

Views: 112

Answers (3)

Onkar
Onkar

Reputation: 2584

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
}

Upvotes: 0

Leigh Bicknell
Leigh Bicknell

Reputation: 872

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");
  }
}

Upvotes: 1

user7126103
user7126103

Reputation:

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);
}

Upvotes: 0

Related Questions