Jump to content


Photo

array needed?


  • Please log in to reply
2 replies to this topic

#1 tgavin

tgavin
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts
  • LocationNaples, FL

Posted 04 October 2006 - 02:27 PM

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: failure($fail_title,$fail_msg);

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

YOU FAILED
Do something right, will ya?


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:

FAILED TITLE 1
failed message 1

FAILED TITLE 2
failed message 2


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

Here's the function:
<?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>
		";
	}
}
?>

Thanks for any help.
A noble spirit embiggens the smallest man.
- Jebediah Springfield

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 October 2006 - 02:45 PM

If you want to use an array for your error message, you can do something like:
<?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>';
?>

Ken

#3 tgavin

tgavin
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts
  • LocationNaples, FL

Posted 04 October 2006 - 03:32 PM

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 "=>" for?

Thanks!
A noble spirit embiggens the smallest man.
- Jebediah Springfield




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users