Jump to content

Archived

This topic is now archived and is closed to further replies.

tgavin

array needed?

Recommended Posts

I'm creating a very basic function for displaying error messages. So far it's working great for "single" errors. I'm having a bit of a problem with multiple errors. Currently, the function is called like so: [tt]failure($fail_title,$fail_msg);[/tt]

In my scripts, I then build the failure titles and messages
[code]<?php
// error encountered
$success = 'false';
$fail_title .= 'You Failed';
$fail_msg .= 'Do something right, will ya?';
?>[/code]
Which is displayed like:

[color=red][b]YOU FAILED[/b]
Do something right, will ya?[/color]

Again, this works great for scripts where only one failure at a time is possible. But now I've run into a script (form) where I can have many failures on screen at a time. I could continue doing as I'm doing, and just adding < br / >to the end of each $fail var, but that creates a display problem, as all of the titles are together, and all of the messages are together. I need to be able to echo them together:

[color=red][b]FAILED TITLE 1[/b]
failed message 1

[b]FAILED TITLE 2[/b]
failed message 2[/color]

I probably need an array for this, but am still learning about them and don't know how to construct it.[color=red][/color]

Here's the function:[code]
<?php
/* FAILURE MESSAGE */
// div box for error messages
function failure($title,$message,$colspan) {
if($colspan == 0) {
echo "<div align=\"center\" class=\"failure\"><div class=\"failureTitle\">".$title."<br /></div>".$message."</div>";
} else {
echo "
<tr>
<td align=\"center\" colspan=\"".$colspan."\"><div align=\"center\" class=\"failure\"><div class=\"failureTitle\">".$title."<br /></div>".$message."</div></td>
</tr>
";
}
}
?>[/code]

Thanks for any help.

Share this post


Link to post
Share on other sites
If you want to use an array for your error message, you can do something like:
[code]<?php
$errors = array();
//
//
$errors['You Failed'] = 'Do something right, will ya?'; // set error
//
//
// process errors (if any)
//
if (!empty($errors))
  foreach ($errors as $title => $message)
          failure($title,$message,$colspan);
//
function failure($t,$m,$clsp) {
  if ($clsp == 0)
        echo '<div align="center" class="failure"><span class="failureTitle">' . $t . '<br /></span>' . $m . '</div>';
  else
        echo '<tr><td align="center" colspan="' . $clsp . '"><div align="center" class="failure"><span class="failureTitle">' . $t . '<br /></span>' . $m . '</div><tr>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
Perfect! That's so fast and easy. That's really going to help me in all future development as well!
I've always been curious about something: what is this "[tt]=>[/tt]" for?

Thanks!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.