如何为复选框组创建模板以避免服务器端的代码重复?
Posted
技术标签:
【中文标题】如何为复选框组创建模板以避免服务器端的代码重复?【英文标题】:How to create a template for checkbox groups to avoid code duplication at server side? 【发布时间】:2015-08-14 02:49:41 【问题描述】:我最近发现了一个问题(***.com/questions/30556100/how-to-uncheck-all-checkboxes-if-one-checkbox-checked)。现在,我想为 html 生成创建一个模板,因为避免代码重复似乎是个好主意。这就是我的 html 的生成方式:
// ---------------------------------------
// --- Populate "Body Type" dropdown list:
// ---------------------------------------
// for populate body types:
$bodytype = array();
// Select exsiting body type from database:
$prep_stmt = " SELECT id, name FROM body_type";
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt)
// Bind "$userId" to parameter.
//$stmt->bind_param('i', $userId);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();
// get variables from result.
$stmt->bind_result($id, $name);
// Fetch all the records:
while ($stmt->fetch())
// XSS protection as we might print these values
$name = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $name);
$id = filter_var($id,FILTER_SANITIZE_NUMBER_INT);
$type = "<label class='checkbox-inline'>\n";
if ($name == 'Any')
$type .= "<input type='checkbox' id='bodyTypeAny' name='bodyTypeCheck[]' value='$id'> $name\n";
else
$type .= "<input type='checkbox' id='' name='bodyTypeCheck[]' value='$id'> $name\n";
$type .= "</label>\n";
//Add body types to array
$bodytype[] = $type;
else
echo 'Database error';
// Close the statement:
$stmt->close();
unset($stmt);
复选框组的所有 php 代码:http://pastebin.com/NXN6ZzcK
如何实现会生成复选框组的函数,所以我将其简单地称为函数。
希望有人可以帮助我。 谢谢你。
【问题讨论】:
【参考方案1】:在您执行以下步骤之前,请将您的工作版本保存在某处,以确保您可以在需要时回滚。
您可以为模板创建一个类,如下所示:
class TemplateEngine
public static function getCheckboxTemplate($input, $templateName)
$template = "";
foreach ($input as $element)
$template .= "<label class='checkbox-inline'>\n";
$template .= "<input type='checkbox' ".(($element["name"] === "Any") ? ("id='".$templateName."Any'") : (""))." name='".$templateName."Check' value='".$element["id"]."'>".$element["name"]."\n";
$template .= "</label>\n";
return $template;
确保您在一个单独的文件中创建该类,您将在其中实现以后的模板,并且您需要/包含该文件。然后你可以像这样使用它:
// ---------------------------------------
// --- Populate "Body Type" dropdown list:
// ---------------------------------------
$template = "";
// Select exsiting body type from database:
$prep_stmt = " SELECT id, name FROM body_type";
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt)
// Bind "$userId" to parameter.
//$stmt->bind_param('i', $userId);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();
// get variables from result.
$stmt->bind_result($id, $name);
$input = array();
// Fetch all the records:
while ($stmt->fetch())
$input[]= array("name" => preg_replace("/[^a-zA-Z0-9_\-]+/", "", $name), "id" => filter_var($id,FILTER_SANITIZE_NUMBER_INT));
$template = TemplateEngine::getCheckboxTemplate($input, "bodyType");
else
echo 'Database error';
// Close the statement:
$stmt->close();
unset($stmt);
除了使用$bodytype
array
,您还可以:
echo $template;
代码未经测试。如果有错别字,请告诉我:)
编辑:进一步的模板:
class TemplateEngine
public static function getCheckboxTemplate($input, $templateName)
$template = "";
foreach ($input as $element)
$template .= "<label class='checkbox-inline'>\n";
$template .= "<input type='checkbox' ".(($element["name"] === "Any") ? ("id='".$templateName."Any'") : (""))." name='".$templateName."Check' value='".$element["id"]."'>".$element["name"]."\n";
$template .= "</label>\n";
return $template;
public static function getCheckboxDatabaseWrapperTemplate($tableName, $templateName, $mysqli)
$template = "";
// Select exsiting body type from database:
$prep_stmt = " SELECT id, name FROM ".$tableName;
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt)
// Bind "$userId" to parameter.
//$stmt->bind_param('i', $userId);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();
// get variables from result.
$stmt->bind_result($id, $name);
$input = array();
// Fetch all the records:
while ($stmt->fetch())
$input[]= array("name" => preg_replace("/[^a-zA-Z0-9_\-]+/", "", $name), "id" => filter_var($id,FILTER_SANITIZE_NUMBER_INT));
$template = TemplateEngine::getCheckboxTemplate($input, $templateName);
else
echo 'Database error';
// Close the statement:
$stmt->close();
unset($stmt);
return $template;
【讨论】:
以上是关于如何为复选框组创建模板以避免服务器端的代码重复?的主要内容,如果未能解决你的问题,请参考以下文章
如何为我的 Lambda 函数创建可重复使用的 CloudFormation 模板?