author:咔咔
wechat:fangkangfk
有没有在添加跟修改的时候很烦,需要写很多页面,修改还需要追加数据,很烦,很烦
下来就解决这样的问题,所有模块的添加,修改,查看,都使用同一个模板
首先从数据库设计开始
注释解释:
域名地址:这个在添加或者修改时在lable标签里边写的
add=text:这个最终会以这个判断这个字段是什么类型,并且显示不同的标签
这里放一个写的比较全的
下来咱们先写一个获取数据库注释的方法
第一个方法是获取数据库注释,第二个方法是对注释进行业务需求的组装
/** * author:咔咔 * * 获取字段注释类型 * @param $table 表名 * @param bool $includePrimary * @return array */ public function getFileCommon($table, $includePrimary = false) { $prefix = config('database.prefix'); $data = Db::query('SHOW FULL COLUMNS FROM '.$prefix.$table); foreach($data as $row){ if ($row['Comment']) { $row = array_merge($row, $this->_parse_comments($row['Comment'])); }else { $row['DisplayName'] = $row['Field']; } $type = $row['Type']; if (preg_match('/^varchar/i', $type)) { $type = str_replace('varchar', '字符串', $type); } else if (preg_match('/^int/i', $type)) { $type = str_replace('int', '数字', $type); } else if (preg_match('/^tinyint/i', $type)) { $type = str_replace('tinyint', '数字', $type); preg_match('/\((\d+)\)/i', $type, $matches); $length = $matches[1]; } else if (preg_match('/^text/i', $type)) { $type = str_replace('text', '文本', $type); } else if (preg_match('/^datetime/i', $type)) { $type = str_replace('datetime', '时间:YYY-MM-DD HH:II:SS', $type); } $row['DisplayType'] = $type; if ($includePrimary) { $fields[] = $row; } else { if ($row['Key'] != 'PRI') { $fields[] = $row; } } } // 整理:以name为key $new_fields = array(); foreach ($fields as $value) { $name = $value['Field']; $new_fields[$name] = $value; } return $new_fields; } /** * author:咔咔 * * 提取字段注释跟显示类型 * @param $comments 注释 * @return array */ protected function _parse_comments($comments) { $parts = explode("|", $comments); $comment = $parts[0]; $params_string = $parts[1]; if ($params_string) { parse_str($params_string, $params); foreach ($params as $key => $value) { if ($value == 'true') { $value = true; } if ($value == 'false') { $value = false; } $params[$key] = $value; } } else { $params = array(); } $result = array('DisplayName' => $comment, 'Params' => $params); return $result; }
怎么使用?
我的domain控制器继承了base控制器,上面俩个方法在base控制器
{include file="../../../application/admin/view/public/head" /} <?php foreach ($fields as $info):?> <?php $type = $info['Params']['add']; $validateClass = "validate[required]"; ?> <?php if ($info['Params']['add'] == 'ignore'): ?> <?php continue; ?> <?php endif; ?> <?php if ($info['Params']['add'] == 'hidden'): ?> <input name="<?php echo $info['Field'];?>" type="hidden" value="<?php echo htmlspecialchars($info['Value']); ?>" /> <?php continue; ?> <?php endif; ?> <tr> <td> <?php if ($type == 'text'): ?> <div class="layui-form-item"> <label class="layui-form-label"><?php echo $info['DisplayName'];?></label> <div class="layui-input-block"> <input id="id_<?php echo $info['Field'];?>" lay-verify="name" class="layui-input" name="<?php echo $info['Field'];?>"> </div> </div> <?php elseif ($type == 'bool'): ?> <label class="radio"> <input type="radio" name="<?php echo $info['Field'];?>" class="<?php echo $validateClass; ?>" value="1" checked> 是 </label> <label class="radio"> <input type="radio" name="<?php echo $info['Field'];?>" class="<?php echo $validateClass; ?>" value="2"> 否 </label> <?php elseif ($type == 'radio'): ?> <div class="layui-form-item"> <label class="layui-form-label">类型</label> <div class="layui-input-block"> <?php $items = explode(",", $info['Params']['items']); ?> <?php foreach ($items as $value): ?> <?php $parts = explode('#', $value, 2); $displayText = $parts[0]; if (count($parts) == 2) { $displayValue = $parts[1]; } else { $displayValue = $parts[0]; } ?> <label class="radio"> <input type="radio" name="<?php echo $info['Field'];?>" value="<?php echo $displayValue; ?>" class="<?php echo $validateClass; ?>" checked> <?php echo $displayText; ?> </label> <?php endforeach; ?> </div> </div> <?php elseif ($type == 'checkbox'): ?> <?php $items = explode(",", $info['Params']['items']); ?> <?php foreach ($items as $value): ?> <?php $parts = explode('#', $value, 2); $displayText = $parts[0]; if (count($parts) == 2) { $displayValue = $parts[1]; } else { $displayValue = $parts[0]; } ?> <label class="checkbox"> <input type="checkbox" name="<?php echo $info['Field'];?>[]" value="<?php echo $displayValue;?>" class="<?php echo $validateClass; ?>"> <?php echo $displayText;?> </label> <?php endforeach; ?> <?php elseif ($type == 'textarea'): ?> <textarea name="<?php echo $info['Field'];?>" rows="10" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea> <?php elseif ($type == 'textarea_html'): ?> <textarea name="<?php echo $info['Field'];?>" rows="20" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea> <?php elseif ($type == 'label'): ?> <?php echo htmlspecialchars($info['Default']);?> <?php elseif ($type == 'datetime'): ?> <?php continue; ?> <?php elseif ($type == 'image_upload'): ?> <input name="<?php echo $info['Field'];?>" type="file" class="<?php echo $validateClass; ?>" /> <?php elseif ($type == 'file_upload'): ?> <input name="<?php echo $info['Field'];?>" type="file" class="<?php echo $validateClass; ?>" /> <?php else: ?> <textarea name="<?php echo $info['Field'];?>" rows="10" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea> <?php endif; ?> </td> </tr> <?php endforeach;?>