Jump to content

array needed?


tgavin

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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.