Created
February 20, 2021 14:43
-
-
Save matezito/3925a77cbeb72e562e5d0603c8374191 to your computer and use it in GitHub Desktop.
Create form fields class (mini)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Fields { | |
static private $initialized = false; | |
public static function initialize() | |
{ | |
if (self::$initialized) | |
return false; | |
self::$initialized = true; | |
} | |
/** | |
* Fields | |
*/ | |
public static function simple_field($label = '',$type = 'text', $name, $placeholder = '', $id, $class = '', $value = '') | |
{ | |
$id = !$id ? '' : 'id="'.$id.'"'; | |
echo '<div>'; | |
echo '<label>'.$label.'</label>'; | |
echo '<input type="' . $type . '" name="' . $name . '" placeholder="' . $placeholder . '" ' . $id . ' class="' . $class . '" value="' . $value . '" />'; | |
echo '</div>'; | |
} | |
public static function textarea_field($label = '', $name, $placeholder = '', $id, $class = '', $value = '') | |
{ | |
$id = !$id ? '' : 'id="'.$id.'"'; | |
echo '<div>'; | |
echo '<label>'.$label.'</label>'; | |
echo '<textarea name="' . $name . '" placeholder="' . $placeholder . '" ' . $id . ' class="' . $class . '">' . $value . '</textarea>'; | |
echo '</div>'; | |
} | |
public static function select_field($label = '', $name, $id, $class = '', $empty=true, $options = [], $selected) | |
{ | |
$id = !$id ? '' : 'id="'.$id.'"'; | |
echo '<div>'; | |
echo '<label>'.$label.'</label>'; | |
echo '<select name="'.$name.'" '.$id.' class="'.$class.'">'; | |
if($empty) { | |
echo '<option value=""> -- select option --</option>'; | |
} | |
foreach($options as $option => $text) { | |
echo '<option value="'.$option.'" '.selected( $selected, $option, false ).'>'.$text.'</option>'; | |
} | |
echo '</select>'; | |
echo '</div>'; | |
} | |
public static function checkbox_field($label,$name,$id,$class='',$value,$checked=false) | |
{ | |
$id = !$id ? '' : 'id="'.$id.'"'; | |
$checked = !$checked ? '' : 'checked="checked"'; | |
echo '<div>'; | |
echo '<label>'.$label; | |
echo '<input type="checkbox" value="'.$value.'" name="'.$name.'" '.$id.' class="'.$class.'" '.$checked.' />'; | |
echo '</label>'; | |
echo '</div>'; | |
} | |
public static function radio_field($label,$name,$id,$class='',$value,$checked=false) | |
{ | |
$id = !$id ? '' : 'id="'.$id.'"'; | |
$checked = !$checked ? '' : 'checked="checked"'; | |
echo '<div>'; | |
echo '<label>'.$label; | |
echo '<input type="radio" value="'.$value.'" name="'.$name.'" '.$id.' class="'.$class.'" '.$checked.' />'; | |
echo '</label>'; | |
echo '</div>'; | |
} | |
public static function button($text,$type,$name,$id,$class='',$value) | |
{ | |
$id = !$id ? '' : 'id="'.$id.'"'; | |
echo '<div>'; | |
echo '<button type="'.$type.'" name="'.$name.'" '.$id.' class="'.$class.'">'.$value.'</button>'; | |
echo '</div>'; | |
} | |
} | |
Fields::initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Se puede usar con Fields::simple_field($atts); por ejemplo