我写了一个非常简单的if语句来检查url。它工作得很好,但是有什么办法可以减少它吗?
<?php if ($_SERVER['REQUEST_URI'] === "/jim.html") { ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">Inman jim <i class="fa fa-angle-down"></i></a>
<?php } elseif ($_SERVER['REQUEST_URI'] === "/bob.html") { ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">bob <i class="fa fa-angle-down"></i></a>
<?php } elseif ($_SERVER['REQUEST_URI'] === "/dereck.html") { ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">dereck <i class="fa fa-angle-down"></i></a>
<?php } elseif ($_SERVER['REQUEST_URI'] === "/maxamilamatronicus") { ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">maxamilamatronicus <i class="fa fa-angle-down"></i></a>
只需在下拉菜单中显示正确的标题即可。
谢谢!
答案 0 :(得分:2)
这是一个更简单的代码,它给出了相同的结果:
JSONObject jsonObj = new JSONObject(json_response);
String source = jsonObj.getString("source");
String msgType = jsonObj.getString("msgType");
JSONArray jsonArray = jsonObj.getJSONArray("properties");
JSONObject IMEIObject = jsonArray.getJSONObject("IMEI");
String IMEI = IMEIObject.getJSONString("string");
JSONObject myTimeObject = jsonArray.getJSONObject("My Time");
String myTime = myTimeObject.getJSONString("string");
JSONObject posObject = jsonArray.getJSONObject("Position");
JSONObject lljsonObject = posObject.getJSONObject("geographicPosition");
String latitude = lljsonObject.getJSONString(String.valueOf("latitude"));
String longitude = lljsonObject.getJSONString(String.valueOf("longitude"));
请注意,如果<?php
$menus = [
'/jim.html' => [ 'title' => 'Inman jim', 'href' => '#'],
'/bob.html' => [ 'title' => 'bob', 'href' => '#'],
'/dereck.html' => [ 'title' => 'dereck', 'href' => '#'],
'/maxamilamatronicus' => [ 'title' => 'maxamilamatronicus', 'href' => '#']
];
$menu = ['title' => 'Unknown', 'href' => '#'];
if(isset($menus[$_SERVER['REQUEST_URI']]))
$menu = $menus[$_SERVER['REQUEST_URI']];
?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="<?=$menu['href']?>">
<?= $menu['title'] ?>
<i class="fa fa-angle-down"></i>
</a>
不等于"Unknown"
数组中的任何键,则文字为$_SERVER['REQUEST_URI']
。
答案 1 :(得分:1)
使用Switch
更易读且易于维护:
switch ($_SERVER['REQUEST_URI']) {
case "/jim.html" : ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">Inman jim <i class="fa fa-angle-down"></i></a><?php
break;
case "/bob.html" : ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">bob <i class="fa fa-angle-down"></i></a><?php
break;
case "/dereck.html" : ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">dereck <i class="fa fa-angle-down"></i></a><?php
break;
case "/maxamilamatronicus" : ?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">maxamilamatronicus <i class="fa fa-angle-down"></i></a><?php
break;
}
答案 2 :(得分:0)
您可以在PHP中使用switch case。哪个更有效率。请看这里的示例:http://www.w3schools.com/php/php_switch.asp
还尝试在PHP之外保留下面的项目。这样您就可以提高可读性和效率。
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown">
因此,更好的解决方案是:
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown"
<?php switch ($_SERVER['REQUEST_URI']) {
case "/jim.html" : ?>
href="#">Inman jim <?php
break;
case "/bob.html" : ?>
href="#">bob <?php
break;
case "/dereck.html" : ?>
href="#">dereck <?php
break;
case "/maxamilamatronicus" : ?>
href="#">maxamilamatronicus <?php
break;
}?>
<i class="fa fa-angle-down"></i></a>
答案 3 :(得分:0)
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">
<?php
switch ($_SERVER['REQUEST_URI']) {
case '/jim.html': $a='Inman jim'; break;
case '/bob.html': $a='bob'; break;
case '/dereck.html': $a='dereck'; break;
case '/maxamilamatronicus': $a='maxamilamatronicus'; break;
default: $a='';
}
echo $a;
?>
<i class="fa fa-angle-down"></i></a>