There are many approaches to write your code in nice method but nowadays I am working on html function method to write my code. If your are developing a site which is based on Bootstrap, then I have a good trick for you. Let’s assume that you are using following lines for getting input:
1 2 3 4 5 6 7 8 9 10 |
<div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="f_name"> First Name <span class="required"> * </span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="text" name="f_name" value="Muhammad Faryad" class="form-control col-md-7 col-xs-12" required="" /> </div> </div> |
And this structure is repeating many times on one page and then on other pages then what will you do? Will you write each of these lines over and over and over… or you have some better idea?
I have an idea. Let’s convert these lines into a custom function. See the function below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php function html_input($input_name='',$label='',$is_required='yes',$input_type='text',$value='') { $is_required=='yes'?$get_required='required=""':$get_required=''; ?> <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="<?php echo $input_name;?>"> <?php echo get_phrase($label); if($is_required=='yes'){ ?> <span class="required"> * </span> <?php }?> </label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="<?php echo $input_type;?>" name="<?php echo $input_name;?>" value="<?php echo $value;?>" class="form-control col-md-7 col-xs-12" <?php echo $get_required;?> /> </div> </div> <?php } ?> |
Now implement this code is as under:
1 2 3 4 5 |
<?php html_input('f_name','First Name','yes','text','Muhammad Faryad'); ?> |
And you have done. One line is equal to several lines and two lines are equal to several and several lines and so on………
The idea is very simple just write a function and put all of your HTML code and put necessary parameters and enjoy. In the above PHP code you are calling a function html_input which I have written for you. You can use this function everywhere into your site.