Discarding Blank Input

Question: Hi Stephen,
I recently bought your Advanced Email.PHP and am really "over the moon" with the way it works! ...thanks.

However, I´ve stumbled across a problem which I cannot find an answer to ... anywhere!

I´ve just completed a "orderform" and templates for both ´text´ and ´html´ emails.

I get *ALL the ´field data´* without a problem! eg.

´Prod1´  ´Quantity1´ ´Value1´
´Prod2´ ´Quantity2´ ´Value2´
´Prod3´ ´Quantity3´ ´Value3´

What I do not seem able to do, is to create a template with the ability NOT to display ´blank fields´.
eg. If ´Quantity2´ is blank*, then I only wish to receive

´Prod1´  ´Quantity1´ ´Value1´
´Prod3´ ´Quantity3´ ´Value3´

Is there away I can process a ´form´ to send only completed ¨Quantity¨ fields, and still display them in an ´html´ layout?? ie. For the email to ignore ´Quantity2´ if it is blank, and then ignore also ´Prod2´and ´Value2´as well??

Good question eh?

Answer: There is no "built-in" functionality to handle not displaying blank fields (perhaps I should add that into a future version).

To be able to add the functionality to a form yourself you can set up an include file with your own processing in it to set up the form content. The values read in are available from within your own code in an included file using $f_vs[0] (where 0 is the first entry and subsequent values are numbered 1,2, etc. The corresponding field names from your form are stored in $f_ks[0].

You could put code into an include file to do what you want like this: Note that the code separates your fields into three separate arrays for prod, quantity, and value and then adds checks the quantity array to see which entries to output.

$template = "";
for ($i = 0;$i < sizeof($f_ks);$i++) {
  if (substr($f_ks[$i],0,-1) == 'Prod') {
    $n = substr($f_ks[$i],4,1);
    $prod[$n-1] = $f_vs[$i];
  if (substr($f_ks[$i],0,-1) == 'Quantity') {
    $n = substr($f_ks[$i],8,1);
    $quantity[$n-1] = $f_vs[$i];
  if (substr($f_ks[$i],0,-1) == 'Value') {
    $n = substr($f_ks[$i],5,1);
    $value[$n-1] = $f_vs[$i];
for ($i = 0;$i < sizeof($quantity);$i++) {
  if ($quantity[$i] != '')
$template .= "'".$prod[$i]."' '"$quantity[$i]."' '".$value[$i]."'\n";

I hope that this works for you.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow