我最近发现了一个问题 (stackoverflow.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
我怎样才能实现一个函数来生成一个复选框组,所以我会简单地将它作为一个函数来调用。
希望有人能帮帮我。 谢谢你。
最佳答案
在执行以下步骤之前,请将您的工作版本保存在某处,以确保您可以在需要时回滚。
您可以为模板创建一个类,如下所示:
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;
}
}
关于php - 如何为复选框组创建模板以避免服务器端代码重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560034/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?