Hope this makes sense and hope someone can help me.
I have a database table with the following columns: person_id, firstname, surname, email_address.
Periodically I am given a set of files to send to some of the contacts in the database table. The filenames contain the person_id and they are always of the format: person_id.pdf or person_id-2.pdf. Sometimes there is only one of the files available or sometimes it is both of the files. The files are stored in a directory on our server
files: 1111.pdf, 1111-2.pdf, 2222.pdf, 3333-2.pdf
The user with person_id 1111: should receive an email with two attachements - 1111.pdf and 1111-2.pdf
The user with person_id 2222: should receive an email with one attachement - 2222.pdf
The user with person_id 3333-2: should receive an email with one attachement - 3333-2.pdf
This is just a small example, in real world scenario it could be hundreds of users to send these files to so I need an automated way using PHP to acheive this.
I have got to a point where:
The user with person_id 2222: receives the correct email
The user with person_id 3333: receives the correct email
However the user with person_id 1111: always receives two emails containing the same content.
I think this is because both files exist so whilst executing the loop it is doing it once for each file so my logic is flawed and I can’t figure it out.
The way my file is built is:
Make data connection
Submit button - once clicked:
loops through my letters directory to find the files
set filenames as variables to use
use substr on the filename to extract the person_id
run db query to retrieve relevant record where person_id = “filename” (minus the .pdf or -2.pdf)
attach relevant files and send email.
I have attached a copy of my code to hopefully show what I am trying to acheive.
Please ask if you would like further clarification. My code so far is embedded below
I am aware my PHP may have all sorts of errors or non best practice methods but I am learning all time
Link to my PHP code