I have a PHP script that is a combination of a purchased script and my own programming. It is a mess right now and not easy to work with at all.

Basically, the purchased script is one that will merger PDF files into one and let the user download the single PDF file. That's not the problem, that works great.

What I have is a page that has some check boxes, based on what is checked, I need the script to know what PDF files to merge together, and the PDFs must be in a specific order.

Right now I have a TON of IF statements to determine every possible combination of checkboxes. It works, but if I need to add something or change something, it takes forever.

Here's an example of one IF statement:

if((isset($_POST['N_P_L'])) && (isset($_POST['S_P_L'])) && (isset($_POST['Coarse'])) && (isset($_POST['Fine'])) && (isset($_POST['Pigment'])) && (isset($_POST['IWR'])) && (isset($_POST['NCA'])))
{
$com->Merge($path."/pdfs/cover/cover_form_pl.pdf|".$path."/pdfs/cover/cover_form_pl.pdf|".$path."/pdfs/cover/Leeds.pdf|".$path."/pdfs/cover/test_results.pdf|".$path."/pdfs/cover/technical_notice.pdf|".$path."/pdfs/current/mortars/n/n_p_l.pdf|".$path."/pdfs/current/mortars/s/s_p_l.pdf|".$path."/pdfs/current/grouts/3000_psi_coarse_grout.pdf|".$path."/pdfs/current/grouts/3000_psi_fine_grout.pdf|".$path."/pdfs/current/pigment/solomon.pdf|".$path."/pdfs/current/pigment/prism.pdf|".$path."/pdfs/current/pigment/dcs.pdf|".$path."/pdfs/current/additives/iwr.pdf|".$path."/pdfs/current/additives/nca.pdf|".$path."/pdfs/cover/certs.pdf|".$path."/pdfs/current/certs/holcim.pdf|".$path."/pdfs/current/certs/western_lime.pdf|".$path."/pdfs/cover/mill_tests.pdf|".$path."/pdfs/current/mill_tests/holcim.pdf|".$path."/pdfs/current/mill_tests/western_lime.pdf|".$path."/pdfs/current/mill_tests/mason_sand.pdf|".$path."/pdfs/current/mill_tests/#2_sand.pdf|".$path."/pdfs/current/mill_tests/pea_gravel.pdf|".$path."/pdfs/cover/msds.pdf|".$path."/pdfs/current/msds/pl.pdf|".$path."/pdfs/current/msds/grout.pdf|".$path."/pdfs/current/pigment/solomon_msds.pdf|".$path."/pdfs/current/pigment/prism_msds.pdf|".$path."/pdfs/current/additives/iwr_msds.pdf|".$path."/pdfs/current/additives/nca_msds.pdf|", $path."temp/submittal.pdf");
}

That will merge ALL of those PDF files, in that order, if every box is checked.

Here's another IF statement further down the page:

//n,c,f,p
if((isset($_POST['N_P_L'])) && (!isset($_POST['S_P_L'])) && (isset($_POST['Coarse'])) && (isset($_POST['Fine'])) && (isset($_POST['Pigment'])) && (!isset($_POST['IWR'])) && (!isset($_POST['NCA'])))
{
$com->Merge($path."/pdfs/cover/cover_form_pl.pdf|".$path."/pdfs/cover/Leeds.pdf|".$path."/pdfs/cover/test_results.pdf|".$path."/pdfs/cover/technical_notice.pdf|".$path."/pdfs/current/mortars/n/n_p_l.pdf|".$path."/pdfs/current/grouts/3000_psi_coarse_grout.pdf|".$path."/pdfs/current/grouts/3000_psi_fine_grout.pdf|".$path."/pdfs/current/pigment/solomon.pdf|".$path."/pdfs/current/pigment/prism.pdf|".$path."/pdfs/current/pigment/dcs.pdf|".$path."/pdfs/cover/certs.pdf|".$path."/pdfs/current/certs/holcim.pdf|".$path."/pdfs/current/certs/western_lime.pdf|".$path."/pdfs/cover/mill_tests.pdf|".$path."/pdfs/current/mill_tests/holcim.pdf|".$path."/pdfs/current/mill_tests/western_lime.pdf|".$path."/pdfs/current/mill_tests/mason_sand.pdf|".$path."/pdfs/current/mill_tests/#2_sand.pdf|".$path."/pdfs/current/mill_tests/pea_gravel.pdf|".$path."/pdfs/cover/msds.pdf|".$path."/pdfs/current/msds/pl.pdf|".$path."/pdfs/current/msds/grout.pdf|".$path."/pdfs/current/pigment/solomon_msds.pdf|".$path."/pdfs/current/pigment/prism_msds.pdf|", $path."temp/submittal.pdf");
}

I literally have about 100 of these for each possible combination.

There's gotta be an easier way!

I haven't taken many programming classes, and I'm not too familiar with PHP, but any help would be appreciated.

I assume you have a single form , with lots of input of type checkbox.
In this case , what you can do to avoid the if/else avalanche is to loop through the $_POST array , and make a single if statement.
For this to work , you must give the file you want to concatenate to the value of checkboxes
For example

<form action="some_page.php" method="post">
    <input type="checkbox" name="box1" value="file1" />
    <input type="checkbox" name="box2" value="file2"  />
    <input type="checkbox" name="box3" value="file3" />

   <input type="submit" name="submit" />
</form>
    <input


foreach( $_POST as $key => $value )
{
    if( $key != 'submit' )
   {
        $checkboxes_selected_value[] = $value ;
   }
}

After you have the array with all the files selected , you can concatenate them

I used a single if statement to avoid the submit input.If you have more fields in you form that aren't checkboxes , or they aren't related with the pdf files you need to include them in the if statement( e.g if( $key != ' submit' && $key != 'firstname' && $key != 'lastname'....) )

I hope this is what you are looking for.

please mark the thread as solved, if done with this , or else post your
queries here, we will look into that.

This article has been dead for over six months. Start a new discussion instead.