title: How to merge PDF-files with php if you get your pdf filenames out of a database?
body: <p>Hello I want to get my pdf filenames out of my database and merge them only I got 2 problems in my database are names but not with ".pdf" after it how can I set it after the filename when i get it in an array.
  And my second problem is how can I merge them?</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  // run query
  $query = mysql_query("SELECT antwoord_id FROM antwoordklant WHERE gebruiker_id = ('" . $id . "')");
  
  // set array
  $filearray = array();
  
  // look through query
  while ($row = mysql_fetch_assoc($query)) {
      // add each row returned into an array
      $filearray[] = $row;
  
      // OR just echo the data:
      echo $row['antwoord_id']; // etc
  }
  
  // debug:
  print_r($filearray);               // show all array data
  echo $filearray[0]['antwoord_id']; // print the first rows username
  
  $datadir = "/home/easyhous/public_html/architectenplan/pdf";
  $outputName = $datadir."merged.pdf";
  
  $cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";
  //Add each pdf file to the end of the command
  foreach($filearray as $file) {
      $cmd .= $file." ";
  }
  $result = shell_exec($cmd);
  </code></pre>