March 11, 2008

build readable strings recursively out of a form array in PHP

Here’s a great way to turn an array into readable strings in PHP. For example, I want an error notice to be sent to me automatically when a database action in a function fails. The problem is, I cannot possibly know what is going to be in every form that is used in the function that generates the database query. While I *can* easily get the keys and values using

foreach ($in_array as $key => $value)

and building a string, what if the value itself is an array? The value for any array within the array would be “Array”. Not very useful!

Fortunately, PHP functions can be recursive. Here’s a function I wrote to handle that:

function getArrayAsString($in_array)
{

$string = '<br />';
foreach ($in_array as $key => $value)
{

if (is_array($value)) $string = $string.'<br /><strong>'.$key.'</strong>: '.getArrayAsString($value).'<br />';
else $string = $string.'<strong>'.$key.'</strong>: '.$value.'<br />';

}
return $string;

}

Each value is checked to see if it is an array. If it is, it (the array value) is sent to this function, and it processes until all of its values have been evaluated, sending any of those array values into this function as well.

I use this function by concatenating the result to an error message by calling the function with the $_POST variable as the array parameter. This way, I have my custom-built message specific to the failed function, and I get all the information that was posted so I can evaluate the error without having to generate the array values one at a time, or worrying if any values are themselves arrays. Click here to fill out a form and see how this recursive function works.

Personally, I like my “keys” to be bold and the values to be normal, but you can change that to suit your tastes.

March 8, 2008

When Insults Had Class (i.e., no 4-letter words)

My mother-in-law is always complaining about language these days, especially in movies. Vin sent me this (in an email) and I immediately thought of how much she’d approve of these!

These glorious insults are from an era when cleverness with words was still valued, before a great portion of the English language got boiled down to 4-letter words.

The exchange between Churchill & Lady Astor: She said, “If you were my husband I’d give you poison,” and he said, “If you were my wife, I’d drink it.”

A member of Parliament to Disraeli: “Sir, you will either die on the gallows or of some unspeakable disease.” “That depends, Sir,” said Disraeli, “whether I embrace your policies or your mistress.”

“He had delusions of adequacy.” - Walter Kerr

“He has all the virtues I dislike and none of the vices I admire.” - Winston Churchill “A modest little person, with much to be modest about.” - Winston Churchill

“I have never killed a man, but I have read many obituaries with great pleasure.”? ? Clarence Darrow

“He has never been known to use a word that might send a reader to the dictionary.” - William Faulkner (about Ernest Hemingway).

“Poor Faulkner. Does he really think big emotions come from big words?”? - Ernest Hemingway (about William Faulkner)

“Thank you for sending me a copy of your book; I’ll waste no time reading it.” - Moses Hadas

“He can compress the most words into the smallest idea of any man I know.”? -? Abraham Lincoln

“I didn’t attend the funeral, but I sent a nice letter saying I approved of it.” - Mark Twain

“He has no enemies, but is intensely disliked by his friends.” - Oscar Wilde

“I am enclosing two tickets to the first night of my new play; bring a friend…. if you have one.” - George Bernard Shaw to Winston Churchill

“Cannot possibly attend first night, will attend second… if there is one.” - Winston Churchill, in response.

“I feel so miserable without you; it’s almost like having you here.” - Stephen Bishop

“He is a self-made man and worships his creator.” - John Bright

“I’ve just learned about his illness. Let’s hope it’s nothing trivial.” - Irvin S. Cobb

“He is not only dull himself, he is the cause of dullness in others.” - Samuel Johnson

“He is simply a shiver looking for a spine to run up.” - Paul Keating

“There’s nothing wrong with you that reincarnation won’t cure.” Jack E. Leonard

“He has the attention span of a lightning bolt.” - Robert Redford

“They never open their mouths without subtracting from the sum of human knowledge.” - Thomas Brackett Reed

“In order to avoid being called a flirt, she always yielded easily.” - Charles, Count Talleyrand

“He loves nature in spite of what it did to him.” - Forrest Tucker

“Why do you sit there look ing like an envelope without any address on it?” - Mark Twain

“His mother should have thrown him away and kept the stork.” - Mae West

“Some cause happiness wherever they go; others, whenever they go.” - Oscar Wilde

“He uses statistics as a drunken man uses lamp-posts… for support rather than illumination.” - Andrew Lang (1844-1912)

“He has Van Gogh’s ear for music.” - Billy Wilder

“I’ve had a perfectly wonderful evening. But this wasn’t it.” - Groucho Marx

February 26, 2008

troubleshooting obscure php email issues

One of my current web development/programming projects is being built on a server that I have no access to. It’s quite challenging, to say the least! One of my issues, and the hardest to resolve, was email.

I use the ordinary mail() PHP function. No classes, no cgi, just the function with HTML headers. But it seemed that certain emails I sent from the server were going nowhere, and no failure was being reported either. For example, I could receive mail at my “theconnorswebsite.com” domain, but not my “whitewavedesigns.com” domain.

Well, first I learned that there was no formal MX record, and my server followed that protocol. One was set up on this particular server, but still, I could not receive email at my wwd domain.

I then tried to specify the smtp server, port and from address:

ini_set("smtp","mail.website.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","info@website.com");

That didn’t work either. I also made sure that the server would allow “from” emails to be from any domain by substituting various emails at different domains as the “from”. I even double-checked my HTML headers:

MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
From: Website <info@website.com>

Then, one of the programmers who works on this mystery server told me that their MTA (mail transfer agent) was Postfix, and a quirky thing about Postfix on a Linux box was that it would not tolerate the “\r” tag when adding additional headers such as “CC” or BCC”. For example, this is how I was writing my headers:

$headers.= "\r\nCC:".$postformAR['Email'];
$headers.= “\r\nBCC:”.$postformAR['Webmaster'];

(I put the carriage-return/line feed in front of the cc and bcc so it can be optional.) I had heard that Windows servers don’t especially like the “\r” carriage-return tag, but a Linux server? Well, okay, I removed it.

Then, as if by miracle, some of the emails were coming through! But still not all of them… I finally sat down today and line-by-line tried to figure out why some of my code was working and some wasn’t. Eventually, I got down to the actual mail() function.

Now, according to php.net, this is the mail() function description:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Note the last “additional_parameters”. Sometimes, I use this last parameter “-finfo@website.com” for failed messages, bouncebacks, etc. I don’t tend to use it when someone submits an email to my site, but I do use it when sending out bulk emails like newsletters. As I found out today, for whatever reason, that last parameter was actually required!

So, my fellow programmers, should you ever have email that is sent from your serve simply vanish into cyber-space, I hope this gives you a few more tricks up your sleeve to resolve the issue.

« Previous entries · Next entries »