Email record

meanster99

Well-known member
Hi Debbie,

Yes that's fixed it! Thank you so much for all your help and patience, and for the lesson on variable declaration. You really are invaluable to this forum...

Thanks,

Matt
 

DebbieS

DaDaBIK Guru
You're welcome and thank you! I'm glad we got it working!
I now have this code in the event someone asks me for this option.
Have a good one!
 

meanster99

Well-known member
Hi Debbie,

Don't worry - everything is still working nicely.

However, my skills are so very limited and I now have another email conundrum and hoped I could run it by you to see if it's even possible. I have kept it in this topic because it is very much related. I am providing a little detail but not too much in case it's not even realistically possible (I believe it would be possible, but obviously can't expect too much from you...).

The 2 emails you did for me before were being sent to the customers, based on the details of the customer record. Each customer record also contains 6 fields containing the names of upto 6 artistes that they may have booked, which are linked to another table that contains the artiste details (including their email addy). Once a booking is complete I need to send an email to each of the (upto 6) artistes named in the customer record, but whose email addresses are stored in a 2nd table.

So would it be possible to have another button/link that would then send some of the details of the customer record to the (upto 6) different email addresses that are to be found in the 2nd table but which are linked by a field other than the artiste email field?

So to clarify:

I have 2 tables - table 1) [customer_booking_form] - contains all details I need to email, except for the actual email addresses, which are to be found in table 2) [artiste_contacts].

Tables are linked by the primary key field called [act_ID] in [artiste_contacts] to the fields called [act_sent], [act_sent2].....[act_sent6] in [customer_booking_form].

Is this something (relatively) easily do-able based on what you've already coded for me? Should I even consider it?

Many thanks in advance for any advice or guidance.

Regards,

Matt
 

DebbieS

DaDaBIK Guru
Building on what is already done, you should be able to add additional code to get the email addresses from the other table based on the value stored in the primary table. Then wherever there is an option for if (receipt) elseif (latepmt) else, insert another elseif for the email artist option.

I won't have any time to look further into this until sometime on the weekend, so have a go at getting this set up at your end. The best way to learn doing things is to try.
 

meanster99

Well-known member
Hi Debbie,

I spent several hours trying to learn enough PHP/MySQL to help myself, but I ended up more confused than ever. I have never really worked with PHP/MySql outside of using Dadabik and trying to teach myself enough to get to the level I need to be at, to achieve what I want, really overwhelmed me.

I think I need to create a Select statement based on an inner join, but do I need separate statements to return the field from table [artise_contacts] for each of the 6 [act_sent] fields in the record of [customer_booking_form] table? If I could at least get help with the MySQL query, I can then face the obstacle of how to put that into PHP etc.

I doubt you had the time or inclination to look at this over the weekend, but the reality is, once I get this function implemented, my system will be all but complete and I probably won't need to do much else to it, so any extra help would be gratefully accepted.

Thanks,

Matt
 

DebbieS

DaDaBIK Guru
Try this. I have not tested it, however, using the same code I already built, I copied/pasted parts where needed and added a couple extra lines (bold parts).

[pre]
$emailto = $emaildet['email'];
$orderno = $emaildet['ID'];
$efirstname = $emaildet['firstname'];
$esurname = $emaildet['surname'];
$esvcreq = $emaildet['service_required'];
$etotprice = $emaildet['total_price'];
$ebookfee = $emaildet['booking_fee'];
$ebalance = $emaildet['balance'];

for ($ea = 0; $ea <= 6; $ea++) {
$eartist .= "'".$emaildet['act_sent[".$ea."]']."', "; // may need to play with quote placement in this line
}
$eartist = array(substr($eartist, 0, -3));

//get email addresses from artise_contacts databases:
$artsql = "Select ".$quote.[emailaddressfieldname].$quote." FROM ".$quote."artise_contacts".$quote." where ".$quote.[artistIDfield].$quote." IN (".$eartist.")";
artist_email = execute_db($artsql, $conn);
$artemail = fetch_row_db ($artist_email);

foreach ($artemail['emailaddressfield'] as $emailartist){
$to_artists .= $emailartist.', ';
} // end foreach
$to_artists = substr($to_artists, 0, -2);

[/pre]

After the "} elseif (isset($_GET["latepmt"]) && ($_GET["latepmt"] != '')) {" section, but before the "else" which closes it, add the following and make changes as required:
[pre]
} elseif (isset($_GET["artist"]) && ($_GET["artist"] != '')) {
$msgset = $_GET["artist"];
$msgtype = 'Message to Artists';
$reminder_email = 'email message content';
if (mail($to_artists, $msgtype.' - '.$table_name.' - '.$normal_messages_ar['new_update_executed'], $reminder_email, $additional_headers)) {
$msgsent = ' - Late Payment email sent';
// update the record
update_sent('emailstatus', $table_name, $table_internal_name, $where_field, $where_value, 'latepmt');
}
[/pre]

Change the three options for sending email to:
[pre]
txt_out('<li>[url='.preg_replace(]Send Receipt Email[/url] - '.$to_addresses.$msgsent);
// late payment reminder
txt_out('<li>[url='.preg_replace(]Send Late Payment Reminder Email[/url]'.$msgsent);
// artist email notice
txt_out('<li>[url='.preg_replace(]Send Email to Artists[/url]'.$msgsent);
[/pre]

In business_logic.php where the new function is located, add the following elseif after latepmt and before the final else:
[pre]
} elseif ($emailtype == 'artist') {
$statusmsg = 'Artist Notice Sent';
[/pre]

Once you get this going, learning more PHP should be the next order of business. I recommend this to anyone who wants to use PHP on any website they manage.
 

meanster99

Well-known member
Hi Debbie,

I know I need to learn PHP and shouldn't rely on you to do my work for me. I'm sorry to be asking so much of you. I will try and learn as soon as I get a bit more time (working 2 jobs at the moment).

Anyway, I have implemented the code you provided. There were a couple of ommissions that I corrected (missing "$" from the line "artist_email = execute_db($artsql, $conn);" and misspelling of artiste table name - at least I have to read your code and try and understand it, which is helpful and better than me just copying and pasting blindly!).

But now I am getting the following error message regarding the Array:

[08] Error: during query execution.
Select `Act_Email` FROM `artiste_contacts` where `act_ID` IN (Array)
DBMS server said: Unknown column 'Array' in 'where clause'

My updated code is:

[pre]
// artiste_contact email part
for ($ea = 0; $ea <= 6; $ea++) {
$eartist .= "'".$emaildet['act_sent[".$ea."]']."', "; // may need to play with quote placement in this line
}
$eartist = array(substr($eartist, 0, -3));

//get email addresses from artise_contacts databases:
$artsql = "Select ".$quote."Act_Email".$quote." FROM ".$quote."artiste_contacts".$quote." where ".$quote."act_ID".$quote." IN (".$eartist.")";
$artist_email = execute_db($artsql, $conn);
$artemail = fetch_row_db ($artist_email);

foreach ($artemail['Act_Email'] as $emailartist){
$to_artists .= $emailartist.', ';
} // end foreach
$to_artists = substr($to_artists, 0, -2);
//end artiste_contact email part
[/pre]

So do you think this may be to do with the quote placement that you mentioned? I haven't messed with it yet, as I don't want to unless I know thats where the problem is.

Thanks,

Matt
 

DebbieS

DaDaBIK Guru
I'm okay with helping out - I just don't want anyone to short-change themselves by not building a foundation in the code language they are working with. I think you are doing fine and with practise you will really get the hang of PHP (I'm still amazed at the things I can pull off - both DaDaBIK and non-DaDaBIK related).

Onto the question ... I think if you change the following line from this:

$eartist = array(substr($eartist, 0, -3));

to this:

$eartist = substr($eartist, 0, -3);

It may do the trick and work. I wrote it as an array first, but then never used it as one - and then forgot to change it back. Stupid code head sometimes gets ahead of me and then I lose the train of thought and miss something. I would have caught it if I tested it, but I've no data to test this with.
 

meanster99

Well-known member
Hi Debbie,

Thank you - now on to the next error!

[08] Error: during query execution.
Select `Act_Email` FROM `artiste_contacts` where `act_ID` IN ('', '', '', '', '', '', ')
DBMS server said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at line 1

This looks like it could be a problem with the quote placement on this line, as you suggested:

[pre]
$eartist .= "'".$emaildet['act_sent[".$ea."]']."', ";
[/pre]


So I had a play around with it, unsuccessfully. I did add an extra single quote to it:

[pre]
$eartist .= "'".$emaildet['act_sent[".$ea."]']."', '";
[/pre]


But then got the following error:

Warning: Invalid argument supplied for foreach() in /home/y09dann/public_html/database/index.php on line 1025

The code I have on these lines is:

[pre]
foreach ($artemail['Act_Email'] as $emailartist){
$to_artists .= $emailartist.', ';
} // end foreach
$to_artists = substr($to_artists, 0, -2);
//end artiste_contact email part
[/pre]

Not sure what to try next?!
 

DebbieS

DaDaBIK Guru
Are act_sent1, act_sent2, etc, the correct names for the fields where the artist ID are stored? Make sure I had the right field name in there and then try:

$eartist .= "'".$emaildet['act_sent".$ea."']."', ";
 

meanster99

Well-known member
Hi Debbie,

No, the field names are: act_sent, act_sent2...act_sent6, which I actually thought wouldn't work with your loop. I haven't done it yet, but I will change the name of the field act_sent to act_sent1 instead, so that the loop hopefully works. I should start it at 1, rather than 0 aswell, shouldn't I?

Also, in my MySQL database the fields act_sent1..act_sent6 do contain the act_ID (an Auto-increment integer), but in Dadabik the act_sent fields contain the names of the act [Stage_Name], not their ID's. I used the linked field function in the interface config for that. Don't know whether this makes a difference, as it should still return the value of [Act_Email] for each linked field shouldn't it?

Thanks for all your help...I will change the act_sent fieldname to act_sent1, try that line you provided and report back.
 

DebbieS

DaDaBIK Guru
The loop will work if you make the first one act_sent1 if you change the for statement to:

for ($ea = 1; $ea <= 6; $ea++) {

So long as the ID number for the act is stored in the database, the sql should work.
 

meanster99

Well-known member
This is my current code:

[pre]
// artiste_contact email part
for ($ea = 1; $ea <= 6; $ea++) {
$eartist .= "'".$emaildet['act_sent".$ea."']."', ";// may need to play with quote placement in this line
}
$eartist = substr($eartist, 0, -3);

//get email addresses from artise_contacts databases:
$artsql = "Select ".$quote."Act_Email".$quote." FROM ".$quote."artiste_contacts".$quote." where ".$quote."act_ID".$quote." IN (".$eartist.")";
$artist_email = execute_db($artsql, $conn);
$artemail = fetch_row_db ($artist_email);

foreach ($artemail['Act_Email'] as $emailartist){
$to_artists .= $emailartist.', ';
} // end foreach
$to_artists = substr($to_artists, 0, -2);
//end artiste_contact email part
[/pre]


OK, changed field name and loop start, so they are all act_sent1...act_sent6 and used your new line:

[pre]
$eartist .= "'".$emaildet['act_sent".$ea."']."', ";
[/pre]


But I still get the error: [08] Error: during query execution.
Select `Act_Email` FROM `artiste_contacts` where `act_ID` IN ('', '', '', '', '', ')
DBMS server said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at line 1

I did try changing the line:

[pre]
$eartist = substr($eartist, 0, -3);
[/pre]


to

[pre]
$eartist = substr($eartist, 0, -2);
[/pre]
(I think this removes the last ', ' doesn't it, whereas with your line above, won't it remove one character too many? Not sure I fully understand it though...)

But having changed this line I still get the error: Warning: Invalid argument supplied for foreach() in /home/y09dann/public_html/database/index.php on line 1025

Line 1025 starts: foreach ($artemail['Act_Email'] as $emailartist){

The full code for these email sendings (the part in index.php) is:

[pre]
if ($table_name == 'customer_booking_form') {
$email_details = execute_db($sql, $conn);
$emaildet = fetch_row_db ($email_details);
$emailto = $emaildet['email'];
$orderno = $emaildet['ID'];
$efirstname = $emaildet['firstname'];
$esurname = $emaildet['surname'];
$esvcreq = $emaildet['service_required'];
$etotprice = $emaildet['total_price'];
$ebookfee = $emaildet['booking_fee'];
$ebalance = $emaildet['balance'];
$from_address = 'bookings@mydomain.co.uk';

$to_addresses = '';
$cc_addresses = '';
$bcc_addresses = '';

$to_addresses = $emailto; // this is from the db query above (below echo $form;) - use your own email to test email sending

// artiste_contact email part
for ($ea = 1; $ea <= 6; $ea++) {
$eartist .= "'".$emaildet['act_sent".$ea."']."', ";// may need to play with quote placement in this line
}
$eartist = substr($eartist, 0, -2);

//get email addresses from artise_contacts databases:
$artsql = "Select ".$quote."Act_Email".$quote." FROM ".$quote."artiste_contacts".$quote." where ".$quote."act_ID".$quote." IN (".$eartist.")";
$artist_email = execute_db($artsql, $conn);
$artemail = fetch_row_db ($artist_email);

foreach ($artemail['Act_Email'] as $emailartist){
$to_artists .= $emailartist.', ';
} // end foreach
$to_artists = substr($to_artists, 0, -2);
//end artiste_contact email part



foreach ($update_notice_email_cc_recipients_ar as $update_notice_email_cc_recipient){
$cc_addresses .= $update_notice_email_cc_recipient.', ';
} // end foreach

// you could put your email address in the $update_notice_email_cc_recipients_ar part of config.php OR hardcode your email address here if you want to get a cc on these mailings - leave as is / blank in config if you don't want cc on email - this you will need to decide how you want to use ...
$cc_addresses = substr($cc_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_bcc_recipients_ar as $update_notice_email_bcc_recipient){
$bcc_addresses .= $update_notice_email_bcc_recipient.', ';
} // end foreach

$bcc_addresses = substr($bcc_addresses, 0, -2); // delete the last ', '

$additional_headers = '';

if ($cc_addresses != '') {
$additional_headers .= "Cc:".$cc_addresses."\n";
} // end if

if ($bcc_addresses != '') {
$additional_headers .= "Bcc:".$bcc_addresses;
} // end if

if ($from_address != '') {
$additional_headers .= "From: ".$from_address."\r\n".
"Reply-To: ".$from_address."\r\n".
"X-Mailer: PHP/".phpversion();
} // end if


// modify the text below which will go into your subject line:
if (isset($_GET["receipt"]) && ($_GET["receipt"] != '')) {
$msgset = $_GET["receipt"];
$msgtype = 'Payment Receipt';
$reminder_email = 'my message content'; // have removed the actual content for ease of reading

//mail(TO, SUBJECT, MESSAGE, HEADERS(CC, BCC, etc));
//make adjustments as needed to the mail line:
if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Receipt email sent';
// update the record
update_sent('emailstatus', $table_name, $table_internal_name, $where_field, $where_value, 'receipt');
echo "<script type='text/javascript'>
alert('$msgsent to $emailto');</script>";
}
} elseif (isset($_GET["latepmt"]) && ($_GET["latepmt"] != '')) {
$msgset = $_GET["latepmt"];
$msgtype = 'Payment Overdue';
$reminder_email = 'Message content'; // again, removed for ease of reading

if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Late Payment email sent';
// update the record (emailstatus is your email status db field name)
update_sent('emailstatus', $table_name, $table_internal_name, $where_field, $where_value, 'latepmt');
echo "<script type='text/javascript'>
alert('$msgsent to $emailto');</script>";
}
} elseif (isset($_GET["artist"]) && ($_GET["artist"] != '')) {
$msgset = $_GET["artist"];
$msgtype = 'Message to Artists';
$reminder_email = 'email message content';
if (mail($to_artists, $msgtype.' - '.$table_name.' - '.$normal_messages_ar['new_update_executed'], $reminder_email, $additional_headers)) {
$msgsent = ' - Job Details sent to act';
// update the record
update_sent('emailstatus', $table_name, $table_internal_name, $where_field, $where_value, 'artist');
}

} else {
$msgset = '';
$msgtype = '';
$msgsent = '';
$reminder_email = '';
}

txt_out('<ul type="disc"><strong>Email actions will be sent to:</strong> '.$to_addresses.'<ul type="disc">');
//receipt email link
txt_out('<li>[url='.preg_replace(]Send Receipt Email[/url]'.$msgsent);
// late payment reminder
txt_out('<li>[url='.preg_replace(]Send Late Payment Reminder Email[/url]'.$msgsent);
txt_out('</ul></ul>'); // closes the list

// artist email job details notice
txt_out('<ul type="disc"><strong>Job details will be sent to:</strong> '.$to_artists.'<ul type="disc">');
txt_out('<li>[url='.preg_replace(]Send Job Details to Performer(s)[/url]'.$msgsent);
txt_out('</ul></ul>'); // closes the list
} // end code addition for email links in customer_booking_form table edit pages

[/pre]
 

meanster99

Well-known member
Hi Debbie,

Would the fact my [act_ID] primary field in 'artiste_contacts' table is an auto-increment integer and the [act_sent1]...[act_sent6] fields are all varchar, have anything to do with it not working?

Also, just for clarification, in case my explanation of the situation hasn't been that clear, these are the tables/fields involved:

Table 1:
Name: 'customer_booking_form'
Fields being used for this problem: [act_sent1], [act_sent2],...,[act_sent6] - varchar (these fields actually display the value of [Stage_Name] from 'artiste_contacts', but the MySQL database stores the [act_ID] field value)


Table 2:
Name: 'artiste_contacts'
Fields: [act_ID] - primary key auto-increment int, [Stage_Name] - varchar, [Act_Email] - varchar

We are attempting to email the addresses contained in [Act_Email] linked to the fields [act_sent1] [act_sent2] etc by [Stage_Name] of the current record in 'customer_booking_form'.

Not sure if that makes it clearer or not, but I've been looking at this for a few hours now and can't fathom it.

It looks to my novice eyes that the values of [act_sent1] etc are not being returned in the select statement, which is why we are getting the error in the statement: Select `Act_Email` FROM `artiste_contacts` where `act_ID` IN ('', '', '', '', '', ') ???

Again, to my eyes it looks like the quotes may not be placed correctly, but I don't know what the 'where' part of the statement should look like (I'm guessing something like: where 'act_ID' IN ('act_sent1','act_sent2,...'act_sent6') ???
 

DebbieS

DaDaBIK Guru
Try this for the for section:
[pre]
for ($ea = 1; $ea <= 6; $ea++) {
$actsentfld = 'act_sent';
if ($emaildet[$actsentfld.$ea] != '') {
$eartist .= "'".$emaildet[."\"".$actsentfld.$ea."\"".]."', ";
}
}
$eartist = substr($eartist, 0, -2);
[/pre]

Since I have no database to test this against, it is trial and error - so I took the code above and tested by echoing the variables to the screen ... got this:

'$emaildet["act_sent1"]', '$emaildet["act_sent2"]', '$emaildet["act_sent3"]', '$emaildet["act_sent4"]', '$emaildet["act_sent5"]', '$emaildet["act_sent6"]'

Given that I was able to correctly write the variables to the screen, the code should now get the correct values for the email lookup.
 

meanster99

Well-known member
Thanks Debbie,

Just get this error now though (odd, as you got it to work for the echo at least):

Parse error: syntax error, unexpected '.', expecting ']' in /home/y09dann/public_html/database/index.php on line 1018

line 1018: $eartist .= "'".$emaildet[."\"".$actsentfld.$ea."\"".]."', ";

I will have a play around with it but if you can see the mistake, would be grateful for the help.

Thanks,

Matt
 

meanster99

Well-known member
Hi Debbie,

Yes, thanks, I'd already tried that, but still get the error:

Warning: Invalid argument supplied for foreach() in /home/y09dann/public_html/database/index.php on line 1028

Line 1028 onwards:

[pre]
foreach ($artemail['Act_Email'] as $emailartist){
$to_artists .= $emailartist.', ';
} // end foreach
$to_artists = substr($to_artists, 0, -2);
[/pre]

However, the variable $eartist is at least returning the values of act_ID's for the 6 [act_sent] fields (according to a txt_out for $eartist that I used), displays on the screen, for example, as: '191','192','193',194','195','196'

So if I can get the foreach part to work, I hope it should be fine. I imagine it's to do with the use of the column name Act_Email, but even though I've spent a fair while reading about foreach statements, I still can't work it out! Any ideas?

I'm sure you must be sick and tired of this and me, but think we (you!) are getting very close...
 

DebbieS

DaDaBIK Guru
Assuming the field names and table names are 100% correct (including capitalization), there are a couple of things to try. I need you to do this and report back the results.

After the call to the artist table, echo the resulting database array result to the screen (surround by pre tags for formatting - paste this after the $artemail line to print on screen):
[pre]
txt_out('<pre>');
print_r($artemail);
txt_out('</pre>');
[/pre]

If there is data displayed (eg: email addresses are found), then try changing the foreach line to:
[pre]
foreach ($artemail['Act_Email'] as $key => $emailartist){
[/pre]

In some rare cases, I have had a foreach not work until I added the key part (even though I was not using it to reference anything).
 

meanster99

Well-known member
Hi Debbie,

Don't worry, all field names, including capitalisation are 100% correct.

Having used the txt_out you provided, the result is:

Array
(
[0] => name@emailaddress.co.uk
[Act_Email] => name@emailaddress.co.uk
)

(I have obviously changed actual email address to a generic name)

However, there are a few little issues with it.

1) When no act_sent fields of a record are completed, the following error occurs on the loading of the edit record page:

[08] Error: during query execution.
Select `Act_Email` FROM `artiste_contacts` where `act_ID` IN ()
DBMS server said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

2) When more than one act_sent fields are completed, the Array txt_out only displays one email address, sometimes it displays the previous act_sent associated email address to have been updated, other times it displays the latest updated field, other times it only displays the first act_sent email address.

So for example, when I complete [act_sent1], and save the record, the txt_out is:

Array
(
[0] => name@emailadd1.co.uk
[Act_Email] => name@emailadd1.co.uk
)

But when I then complete [act_sent2], and save the record, the array sometimes displays as:

Array
(
[0] => name@emailadd2.co.uk
[Act_Email] => name@emailadd2.co.uk
)

but sometimes just stays as the previous one. Same thing happens after saving the record when completing or updating the other act_sent fields ([act_sent3]...[act_sent6]).

Not sure if I am meant to see all email addresses in the array txt_out but thought this needed mentioning.

Lastly, regardless of whether that part is working correctly, before and after changing the foreach line to the one you suggested (using the $key part), I still get the following error:

Warning: Invalid argument supplied for foreach() in /home/y09dann/public_html/database/index.php on line 1031

Which is obviously still referring to the foreach line.

Headache!
 
Top