Category Archives: Websites

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.

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.= "rnCC:".$postformAR['Email'];
$headers.= "rnBCC:".$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.

quick way to print an array in php

Printing arrays in PHP can be maddening if you don’t know the shortcuts! There is always the standard:

foreach ($array as $key => $value)
{
echo $
key.' is key<br>';
echo $value.' is value<br>';
}

but if it’s not an array (and you have error checking on), you’ll get an error:

Warning: Invalid argument supplied for foreach() in /home/username/public_html/file.php on line x

And unfortunately, putting the ‘@’ in front of the ‘foreach’ only yields another error:

Parse error: syntax error, unexpected T_FOREACH in /home/username/public_html/file.php on line x

Of course, you could do a check to see if it *is* an array, but that’s more lines of code, and we want it quick.

Here’s a clever way to get what I call in my Dreamweaver snippets panel a “quick print of array”:

print '<pre>'; print_r($_POST); print '</pre>';

Here’s what it will look like:

Array
(
[asso_key_name_1] => value 1
[asso_key_name_2] => value 2
)

That’s great, but suppose you have several arrays on one page you want to print the results for, how can you keep them apart? Simple! Put the name of the array after the first <pre> tag, like this:

print '<pre>_POST '; print_r($_POST); print '</pre>';

The results will be like this:

_POST Array
(
[asso_key_name_1] => value 1
[asso_key_name_2] => value 2
)

You can use this quick printing of arrays to print POST vars, SESSION vars, recordsets, any kind of PHP array!