<分区> 分区>
我正在使用 PHP 来布局导航菜单并根据 URL 显示内容。
我已经到达 IF-ELSE 堆栈,它确定要显示的内容(通过加载必要的类和/或方法)。但是必须有更好的方式来写这个……有什么建议吗?
BreadCrumbs::getCrumb() 是一个静态方法,用于根据索引值(URI Request,以'/'分隔,然后保存在数组中)检索从 URL 中保存的元素。
...(ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavPrimary()) 根据包含来自 Config 类的导航列表的数组检查 URI 元素。
BreadCrumbs::setEmptyCrumb(1, "home") 运行一个方法,如果值不存在或无效(在导航列表数组中)则设置默认值
<?php
// set bread crumbs
BreadCrumbs::setCrumbs($_SERVER['REQUEST_URI']);
BreadCrumbs::setEmptyCrumb(1, "home");
BreadCrumbs::setEmptyCrumb(2, "all");
if (BreadCrumbs::getCrumb(1) == 'about') {
echo 'This is the <b>About</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'contact') {
echo 'This is the <b>Contact</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'search') {
echo 'This is the <b>Search</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'home') {
if (BreadCrumbs::getCrumb(2) == 'all') {
echo 'This is the <b>Home</b> Page';
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavSecondary())) {
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
}
else {
echo 'change filter value and go to the <b>home</b> page';
}
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(1), Config::getNavPrimary())) {
if (BreadCrumbs::getCrumb(2) == 'all') {
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is <b>all countries</b>';
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavSecondary())) {
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
}
else {
echo 'change filter value to all and go to category: <b>' . BreadCrumbs::getCrumb(1) . '</b>';
}
}
else {
echo 'redirect page to home/all';
}
?>
编辑:改成下面的 Switch 语句,好一点..
<?php
$array_helper = new ArrayHelp;
$valid_primary = $array_helper->recValueReturn(BreadCrumbs::getCrumb(1), Config::getNavPrimary());
$valid_secondary = $array_helper->recValueReturn(BreadCrumbs::getCrumb(2), Config::getNavSecondary());
switch (BreadCrumbs::getCrumb(1)) {
case 'about' :
case 'contact' :
case 'search' :
echo 'This is the <b>' . BreadCrumbs::getCrumb(1) . '</b> Page';
break;
case 'home' :
switch (BreadCrumbs::getCrumb(2)) {
case 'all' :
echo 'This is the <b>Home</b> Page';
break;
case ($valid_secondary[1]) :
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
break;
default:
echo 'change filter value and go to the <b>' . BreadCrumbs::getCrumb(1) . '</b> page';
break;
}
break;
case ($valid_primary[1]) :
switch (BreadCrumbs::getCrumb(2)) {
case ($valid_secondary[1]) :
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
break;
default:
echo 'change filter value and go to the <b>' . BreadCrumbs::getCrumb(1) . '</b> page';
break;
}
break;
default:
echo 'redirect page to home/all';
break;
}
?>
我在加密来self正在使用的第三方供应商的值时遇到问题。他们的指令如下:1)Converttheencryptionpasswordtoabytearray.2)Convertthevaluetobeencryptedtoabytearray.3)Theentirelengthofthearrayisinsertedasthefirstfourbytesontothefrontofthefirstblockoftheresultantbytearraybeforeencryption.4)EncryptthevalueusingAESwith:1.256-bitkeysize,2.25
我正在开发西洋跳棋实现,其中有许多易于测试的方法,但我不确定如何测试我的主要#play_game方法。我的大多数方法都可以很容易地确定输入和输出,因此也很容易测试,但这种方法是多方面的,实际上并没有容易辨别的输出。这是代码:defplay_gameputs@gui.introwhile(game_over?==false)message=nil@gui.render_board(@board)@gui.move_requestplayer_input=getscoordinates=UserInput.translate_move_request_to_coordinates(play
方法应返回-1,0或1分别表示“小于”、“等于”和“大于”。对于某些类型的可排序对象,通常将排序顺序基于多个属性。以下是可行的,但我认为它看起来很笨拙:classLeagueStatsattr_accessor:points,:goal_diffdefinitializepts,gd@points=pts@goal_diff=gdenddefothercompare_pts=pointsother.pointsreturncompare_ptsunlesscompare_pts==0goal_diffother.goal_diffendend尝试一下:[LeagueStats.new(
关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭8年前。Improvethisquestion我们有以下(以及更多)系统,我们将数据从一个应用推送/拉取到另一个:托管CRM(InsideSales.com)Asterisk电话系统(内部)横幅广告系统(openx,我们托管)潜在客户生成系统(自行开发)电子商务商店(spree,我们托管)工作板(本土)一些工作网站抓取+入站工作提要电子邮件传送系统(如Mailchimp,自主开发)事件管理系统(如eventbrite,自主开发)仪表板系统(大量图表和
我有以下字符串:a="001;Barbara;122"我拆分成字符串数组:names=a.split(";")names=["001","Barbara","122"]我应该怎么做才能将每个元素另外用''引号括起来?结果应该是names=["'001'","'Barbara'","'122'"]我知道这听起来很奇怪,但我需要它在rubyonrails中进行数据库查询。出于某种原因,如果我的名字在“”引号中,我将无法访问数据库记录。我在数据库中确实有mk1==0006但rails不想以某种方式访问它。但是,它确实访问1222。sql="SELECTmk1,mk2,pk1,pk
假设我有一个函数defodd_or_evennifn%2==0return:evenelsereturn:oddendend我有一个简单的可枚举数组simple=[1,2,3,4,5]然后我用我的函数在map中运行它,使用一个do-endblock:simple.mapdo|n|odd_or_even(n)end#=>[:odd,:even,:odd,:even,:odd]如果不首先定义函数,我怎么能做到这一点?例如,#doesnotworksimple.mapdo|n|ifn%2==0return:evenelsereturn:oddendend#Desiredresult:#=>[
我的Rails应用程序在暂存服务器上运行速度非常慢,这让我遇到了一些麻烦。最令人困惑的是每个请求的日志输出的最后一行。看起来View和数据库时间甚至不接近整个渲染时间。在一页上,完成时间大约1000毫秒,View大约450毫秒,数据库大约20毫秒。渲染页面所需的其余时间从何而来? 最佳答案 当事情变得神秘时......分析器是你的friend!分析器将统计哪些方法被调用最多以及每个方法调用花费多长时间。ruby-prof当我在RubyLand时,它会帮我解决这个问题,它会生成一个漂亮的调用图(如果需要,可以是html格式),这使得查
这行ruby代码检测素数(太棒了!)。("1"*n)!~/^1?$|^(11+?)\1+$/#wherenisapositiveinteger详细信息在这篇博文中解释http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/我很好奇它在BIG-O表示法中的表现。有人帮忙吗? 最佳答案 根据经验数据,它似乎是O(n2)。我对前10000个质数中的每100个运行Ruby代码。以下是结果:蓝点是记录的时间,橙色线是
link有两个组件:componenta_id和componentb_id。为此,在Link模型文件中我有:belongs_to:componenta,class_name:"Component"belongs_to:componentb,class_name:"Component"validates:componenta_id,presence:truevalidates:componentb_id,presence:truevalidates:componenta_id,uniqueness:{scope::componentb_id}validates:componentb_id
我正在使用searchkick库作为产品搜索的elasticsearch客户端。https://github.com/ankane/searchkick可以创建'OR'条件和'AND'条件;AND运算Product.search其中:{price:{lte:200},in_stock:true}或运算Product.search其中:{或:[[{in_stock:true},{backordered:true}]]}但我坚持使用searchkick创建多个“AND”“OR”条件。我需要类似的东西A或B或(C和D)或者我需要这样,A与B与(C或D)请指导我,如何实现这一目标谢谢