通过数据库自动化处理生成表单

<?php
//this function process the mysql query
function form_get($form_query = NULL, $form_name = NULL, $form_action = NULL, $form_method = 'post', $form_cancel = 0) {
        //object oriented query
        $row = $this->dbc->query($form_query);
        $output = NULL;
        $output .= '<form action="' . $form_action . '" method="' . $form_method . '">';
        //generate form items based on fields
        while ($field = $row->fetch_field()) {

            //print show_array($field);

            //form field error class
            isset($_SESSION[str_replace(' ', '_', $field->name)]) ? $input_class = $_SESSION[str_replace(' ', '_', $field->name)]: $input_class = '';
            //form field default value
            isset($_POST[str_replace(' ', '_', $field->name)]) ? $field_value = $_POST[str_replace(' ', '_', $field->name)]: $field_value = '';
            //form field password type
            (preg_match('/\s*password\s*/', $field->name)) ? $field_type = 'password': $field_type = 'text';
            //form input/textarea type
            ($field->length > 100) ? $input_type = '<label>' . ucwords($field->name) . '</label><text area class="' . $input_class . '" name="' . str_replace(' ', '_', $field->name) . '" cols="20" rows="5">' . $field_value . '</text area>':
                                   $input_type = '<label>' . ucwords($field->name) . '</label><input class="' . $input_class . '" type="' .  $field_type . '" name="' . str_replace(' ', '_', $field->name) . '" value="' . $field_value . '"/>';
            $output .= $input_type;
            //reset empty error class
            $_SESSION[str_replace(' ', '_', $field->name)] = '';
        }
        //cancel submit button switch
        ($form_cancel) ? $cancel = '<input name="cancel" type="submit" value="Cancel">': $cancel = '';
        //form sumit button
        $output .= '<input type="submit" name="' . $form_name . '" value="' . ucwords($form_name) . '" /> ' . $cancel;
        $output .= '</form>';
        return $output;
}

//with a few lines of codes you can automatically generate the registration form, of course you need to setup a database for this to work
function user_register() {
	//form get function input
	$form_query = 'SELECT name as user, password, password as "retype password", email FROM user LIMIT 1';
	$form_name = 'register';
	$form_action = '';
	$form_method = 'post';
	$form_cancel = 1;
       
	$output = NULL;
	if(isset($_GET['q']) && $_GET['q'] == 'register') {
		$output .= $this->form_get($form_query, $form_name, $form_action, $form_method, $form_cancel);
	} else {
		$output .= '<span class="register">Not a member yet? <a href="' . BASE_URL . 'registration">' . ucwords($form_name) . '</a></span>';
	}
	return $output;
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值