tgavin Posted October 4, 2006 Share Posted October 4, 2006 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 messagesfunction 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 4, 2006 Share Posted October 4, 2006 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 Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 4, 2006 Author Share Posted October 4, 2006 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.