Jump to content

How to improve my functions that display messages?


Lykos22

Recommended Posts

Hi, I'd like some help please, if its possible. I have created two functions in order to display some messages when is set a $_GET after a redirect. Here's the code:

function display(){if(isset($_GET['cnf_upd']) && $_GET['cnf_upd'] == '1'){  $value = "The update was successful!";  $type = "confirm";  construct_the_div($value, $type);}if(isset($_GET['err_upd']) && $_GET['err_upd'] == '1'){  $value = "The Update failed.";  $type = "error";  construct_the_div($value, $type);}if(isset($_GET['cnf_del']) && $_GET['cnf_del'] == '1'){  $value = "Deleted completely.";  $type = "confirm";  construct_the_div($value, $type);}if(isset($_GET['err_del']) && $_GET['err_del'] == '1'){  $value = "Unable to delete.";  $type = "error";  construct_the_div($value, $type);}}function construct_the_div($value, $type){// creating a div to display the message results$div = "<div class=\"{$type}Msg\">\n";$div .= "<p>{$value}</p>\n";$div .= "</div><!-- end of {$type}Msg -->\n";echo $div;}

What I'd like to make is to try to improve the display function, as it gets longer and longer, so that there whould be only one (or two at most) if statement(s). So the value of the GET will be dynamicly inside if condition and also if it has the preffix 'cnf_' it wil be a 'confirmMsg' and if it has the preffix 'err_' it wil be a 'errorMsg'. Is it possible to make something like this???

Link to comment
Share on other sites

make an 2d array like $fields[]=['name'=>'cnf_upload','value'=>'Update success'];$fields[]=['name'=>'err_upload','value'=>'Update failed']; now you can loop through $fields array and can make divs dynamically. you can also take value of 'name' and can explode by character '_'to get two part of string 'cnf' and 'upload' and check the first chunk to determine if it is 'error' or 'confirm' message and act accordingly. You can also set up a xml file in same way and use simplexml to parse the file and get values and names fron the file to do the same thing. this way you will have a centralised one source of message which you can moderate indinpendantly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...