Archive for March, 2008

build readable strings recursively out of a form array in PHP

Tuesday, March 11th, 2008

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.

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

Saturday, March 8th, 2008

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 looking 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