How can I make a PHP 'foreach' loop repeat entire line?

92 Views Asked by At

My objective is to allow users to select from check boxes and when they click submit it combines all PDFs into 1 PDF. (each check box has a PDF value).

<?php 
include 'PDFMerger.php';
$pdf = new PDFMerger;


$pdf1 = "/path/pdf1.pdf";
$pdf2 = "/path/pdf2.pdf";
?>

<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="<?php echo $pdf1 ?>"><label>PDF ONE</label><br/>
<input type="checkbox" name="check_list[]" value="<?php echo $pdf2 ?>"><label>PDF TWO</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>


<?php

if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $selected){

    $pdf->addPDF($selected);
}
}
?>
<?php
ob_start();

if(isset($_POST['submit'])){

$pdf->merge('download', 'TEST223.pdf');

}
?>

I am not a PHP expert at all, so I am sure there are many errors in the above code. But the problem lies with the 'foreach" area I think. It only repeats the $selected part.. not the entire line for each PDF.

So, if users select only 1 box.. it works fine. If they select more than one it puts the pdf path one after the other in the code and the Merger program can't use that.

//To visualize the issue.. I need the loop to produce this:
$pdf->addPDF("/path/pdf1.pdf");
$pdf->addPDF("/path/pdf2.pdf");
$pdf->addPDF("/path/pdf3.pdf");
etc...

//It is currently producing:
$pdf->addPDF("/path/pdf1.pdf/path/pdf2.pdf/path/pdf3.pdf");

Thank you for any help in advance.

0

There are 0 best solutions below