Forum Moderators: coopster

Message Too Old, No Replies

email electrolysis

         

Gilead

12:21 am on Nov 11, 2011 (gmt 0)

10+ Year Member



I have a very large order form with many fields that will be blank upon submission. I want to save myself a large amount of work and only send through those fields that actually have a value in them.

What is the best way to do this?

This is for a coffee site and the results are emailed in for the order(not a true shopping cart, nor does the client want one)
I'm using simple text fields that the visitor can put in the quantity of the coffee flavor they want and of course, some contact info.

Thanks!

omoutop

6:41 am on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



how about this approach:
$msg = ''; // the email body text
foreach ($_POST as $name=>$data)
{
if ($data!='') { $msg .= $name.': '.$data.'<br>';}
}

Gilead

3:05 pm on Nov 11, 2011 (gmt 0)

10+ Year Member



Thanks! I'll give it a try.
What does the => mean?

rocknbil

5:23 pm on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or . . . empty will work:

foreach ($_POST as $name=>$data) {
if (! empty($data)) { $msg .= "$name: $data<br>";}
}

One of the advantages to working with double quotes is allowing the scalar variables ($name, $data) to interpolate.

What does the => mean?


You know, I don't know what they call that operator, it's also used in Perl. :-) It's a symbol used to dereference an associative array, not to be confused with >= (greater than or equal.) The array key is left, value right. It's also used to set associative array values. The following two are equivalent.

$myarray = array (
'foo' => 'example.com',
'bar' => 'webmasterworld.com'
);

$myarray = array ();
$myarray['foo'] = 'example.com';
$myarray['bar'] = 'webmasterworld.com';

Either is dereferenced like so. Again, the following are equivalent but the second example is silly if you already have $value in hand -= but there are cases where you'd use it.

foreach ($myarray as $key => $value) {
echo "<p>$key $value</p>";
}

foreach ($myarray as $key => $value) {
echo "<p>$key " . $myarray[$key] . "</p>";
}

(or use {} to avoid concatenation)

While we're at it, an associative array with a list array as members.


$myarray = array (
'foo' => array('apples','oranges','pears'),
'bar' => array('lions','tigers','bears')
);

Since there's no key, list arrays are dereferenced as single members. These two are also equivalent, but the first is more efficient - the array indices are not needed to reference the members, although there are cases where accesing it by index is needed.


foreach ($myarray as $key => $value) {
echo "<p><strong>Key:</strong> $key <strong> Values:</strong> ";
foreach ($value as $v) {
echo " $v ";
}
echo "</p>";
}


foreach ($myarray as $key => $value) {
echo "<p><strong>Key:</strong> $key ";
echo " <strong>Values:</strong> " . $value[0] . " " . $value[1] . " " . $value[2] . "</p>";
}


OR ... more excessive, but as mentioned, sometimes access by index is needed.


foreach ($myarray as $key => $value) {
$len = count($value)-1; // starts at zero
echo "<p><strong>Key:</strong> $key ";
for ($j=0;$j<=$len;$j++) {
echo " " . $value[$j] . " ";
}
echo "</p>";
}

Gilead

12:40 am on Nov 12, 2011 (gmt 0)

10+ Year Member



It WORKS!
Thanks so much!
Only one niggly thing, the \n doesn't seem to do a proper line-break, plus what comes through via email is underlined as if it was a link. How can you prevent that?
Thanks!

I see that it does the entire order, which is very cool! However, is there any way to reverse the order so the contact info is first rather than last?

Thanks!

omoutop

7:05 am on Nov 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Only one niggly thing, the \n doesn't seem to do a proper line-break,

if you have php 5.3+ ( i think) you can use the PHP_EOL for cross platform newline character.

$html ="<p>This is my HTML</p>" . PHP_EOL;


I see that it does the entire order, which is very cool! However, is there any way to reverse the order so the contact info is first rather than last?

Can't think of a quick fix other than re-ordering your form inputs to the order you want to appear on email. Since $_POST is an array, you can use the various array sorts to try to manipulate your outcome.