今天我完成了我的标记,所以我想在php中移动它。所以我有导航仪。
<nav id="cd-vertical-nav">
<ul>
<li>
<a href="#destinations" data-number="1" class="">
<span class="cd-dot"></span>
<span class="cd-label">Choose Destinations</span>
</a>
</li>
<li>
<a href="#activities" data-number="2" class="">
<span class="cd-dot"></span>
<span class="cd-label">Activities</span>
</a>
</li>
<li>
<a href="#accommodation" data-number="3" class="">
<span class="cd-dot"></span>
<span class="cd-label">Accommodation</span>
</a>
</li>
<li>
<a href="#transportation" data-number="4" class="is-selected">
<span class="cd-dot"></span>
<span class="cd-label">Transportation</span>
</a>
</li>
<li>
<a href="#maspindzei" data-number="5" class="">
<span class="cd-dot"></span>
<span class="cd-label">Choose your Host</span>
</a>
</li>
<li>
<a href="#contactinfo" data-number="6" class="">
<span class="cd-dot"></span>
<span class="cd-label">Contact Information</span>
</a>
</li>
</ul>
所以我需要创建一个像这样工作的新功能。
CreateNewSection($secid);
我为此尝试了此代码。
function CreateNewSection($secid)
{
switch($secid)
{
case 1:
{
echo '<nav id="cd-vertical-nav">
<ul>
<li>
<a href="#destinations" data-number="1" class="">
<span class="cd-dot"></span>
<span class="cd-label">Choose Destinations</span>
</a>
</li></ul></nav>';
}
case 2:
{
echo '<nav id="cd-vertical-nav"><li><a href="#activities" data-number="2" class=""><span class="cd-dot"></span><span class="cd-label">Activities</span></a></li></li></ul></nav>';
}
}
}
问题是,当我呼叫ID 1时,它仍然呼叫id 2。这是我的第一次,当我将标记转换为这样的函数时。
答案 0 :(得分:2)
以下是switch-case应该是的方式,每个case
都有break
,然后最后有default
:
switch($secid)
{
case 1:
// code here....
break;
case 2:
// code here....
break;
default:
// code here....
}
因此,您的代码应更新如下:
function CreateNewSection($secid)
{
switch($secid)
{
case 1:
echo '<nav id="cd-vertical-nav">
<ul>
<li>
<a href="#destinations" data-number="1" class="">
<span class="cd-dot"></span>
<span class="cd-label">Choose Destinations</span>
</a>
</li></ul></nav>';
break;
case 2:
echo '<nav id="cd-vertical-nav"><li><a href="#activities" data-number="2" class=""><span class="cd-dot"></span><span class="cd-label">Activities</span></a></li></li></ul></nav>';
break;
}
}
答案 1 :(得分:1)
开关盒末端缺少断裂,导致它掉落。
function CreateNewSection($secid)
{
switch($secid)
{
case 1:
echo '<nav id="cd-vertical-nav">
<ul>
<li>
<a href="#destinations" data-number="1" class="">
<span class="cd-dot"></span>
<span class="cd-label">Choose Destinations</span>
</a>
</li></ul></nav>';
break;
case 2:
echo '<nav id="cd-vertical-nav"><li><a href="#activities" data-number="2" class=""><span class="cd-dot"></span><span class="cd-label">Activities</span></a></li></li></ul></nav>';
break;
}
}
答案 2 :(得分:1)
我强烈建议放弃冗长的switch-case块的想法。有一种更清洁的方式...
更多DRY并且更容易管理将html的动态部分存储在数组中,然后只需将这些值注入其他静态html中。稍后你会感谢自己走这条路。
function CreateNewSection($secid,$class=''){
$secs=[
1=>['href'=>'#destinations','label'=>'Choose Destinations'],
2=>['href'=>'#activities','label'=>'Activities'],
3=>['href'=>'#accommodation','label'=>'Accommodation'],
4=>['href'=>'#transportation','label'=>'Transportation'],
5=>['href'=>'#maspindzei','label'=>'Choose your Host'],
6=>['href'=>'#contactinfo','label'=>'Contact Information']
];
if(!isset($secs[$secid])){
// echo some default behavior or error message
}else{
$sec=$secs[$secid]; // merely to shorten the variable
echo "<li>";
echo "<a href=\"{$sec['href']}\" data-number=\"{$secid}\" class=\"{$class}\">";
echo "<span class=\"cd-dot\"></span>";
echo "<span class=\"cd-label\">{$sec['label']}</span>";
echo "</a>";
echo "</li>";
}
}
echo "<nav id=\"cd-vertical-nav\">";
echo "<ul>";
$selected=4;
for($x=1; $x<7; ++$x){
CreateNewSection($x,($x==$selected?'is-selected':''));
}
echo "</ul>";
echo "</nav>";
*注意我在回声中有{}
卷曲括号的所有变量(即使字符串变量不需要它),因此变量在文本编辑器中突出(或者可以< / em>如果您的编辑器设置为这样做。)
答案 3 :(得分:0)
switch语句是一种循环形式,为了成功执行循环,需要在每个case语句块的末尾添加一个break语句。语法如下所示
17460: URL_REQUEST
http://localhost/profile_pics/Vulturemox_pic.png
Start Time: 2017-08-14 13:24:31.954
t=287810 [st= 0] +REQUEST_ALIVE [dt=47819]
--> priority = "LOWEST"
--> url = "http://localhost/profile_pics/Vulturemox_pic.png"
t=287810 [st= 0] URL_REQUEST_DELEGATE [dt=0]
t=287810 [st= 0] +URL_REQUEST_START_JOB [dt=47818]
--> load_flags = 33026 (BYPASS_CACHE | MAYBE_USER_GESTURE | VERIFY_EV_CERT)
--> method = "GET"
--> url = "http://localhost/profile_pics/Vulturemox_pic.png"
t=287810 [st= 0] URL_REQUEST_DELEGATE [dt=0]
t=287810 [st= 0] HTTP_CACHE_GET_BACKEND [dt=0]
t=287810 [st= 0] HTTP_CACHE_DOOM_ENTRY [dt=0]
--> net_error = -2 (ERR_FAILED)
t=287810 [st= 0] +HTTP_CACHE_CREATE_ENTRY [dt=1]
t=287811 [st= 1] URL_REQUEST_SET_PRIORITY
--> priority = "MEDIUM"
t=287811 [st= 1] -HTTP_CACHE_CREATE_ENTRY
t=287811 [st= 1] HTTP_CACHE_ADD_TO_ENTRY [dt=0]
t=287811 [st= 1] +HTTP_STREAM_REQUEST [dt=0]
t=287811 [st= 1] HTTP_STREAM_JOB_CONTROLLER_BOUND
--> source_dependency = 17466 (HTTP_STREAM_JOB_CONTROLLER)
t=287811 [st= 1] HTTP_STREAM_REQUEST_BOUND_TO_JOB
--> source_dependency = 17467 (HTTP_STREAM_JOB)
t=287811 [st= 1] -HTTP_STREAM_REQUEST
t=287811 [st= 1] +HTTP_TRANSACTION_SEND_REQUEST [dt=0]
t=287811 [st= 1] HTTP_TRANSACTION_SEND_REQUEST_HEADERS
--> GET /profile_pics/Vulturemox_pic.png HTTP/1.1
Host: localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://localhost/index.php?login=success
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: [226 bytes were stripped]
t=287811 [st= 1] -HTTP_TRANSACTION_SEND_REQUEST
t=287811 [st= 1] +HTTP_TRANSACTION_READ_HEADERS [dt=23907]
t=287811 [st= 1] HTTP_STREAM_PARSER_READ_HEADERS [dt=23906]
--> net_error = -101 (ERR_CONNECTION_RESET)
t=311717 [st=23907] HTTP_TRANSACTION_RESTART_AFTER_ERROR
--> net_error = -101 (ERR_CONNECTION_RESET)
t=311718 [st=23908] -HTTP_TRANSACTION_READ_HEADERS
t=311718 [st=23908] +HTTP_STREAM_REQUEST [dt=2]
t=311718 [st=23908] HTTP_STREAM_JOB_CONTROLLER_BOUND
--> source_dependency = 17476 (HTTP_STREAM_JOB_CONTROLLER)
t=311720 [st=23910] HTTP_STREAM_REQUEST_BOUND_TO_JOB
--> source_dependency = 17477 (HTTP_STREAM_JOB)
t=311720 [st=23910] -HTTP_STREAM_REQUEST
t=311721 [st=23911] +HTTP_TRANSACTION_SEND_REQUEST [dt=0]
t=311721 [st=23911] HTTP_TRANSACTION_SEND_REQUEST_HEADERS
--> GET /profile_pics/Vulturemox_pic.png HTTP/1.1
Host: localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://localhost/index.php?login=success
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: [226 bytes were stripped]
t=311721 [st=23911] -HTTP_TRANSACTION_SEND_REQUEST
t=311721 [st=23911] +HTTP_TRANSACTION_READ_HEADERS [dt=23907]
t=311721 [st=23911] HTTP_STREAM_PARSER_READ_HEADERS [dt=23907]
--> net_error = -101 (ERR_CONNECTION_RESET)
t=335628 [st=47818] -HTTP_TRANSACTION_READ_HEADERS
--> net_error = -101 (ERR_CONNECTION_RESET)
t=335628 [st=47818] -URL_REQUEST_START_JOB
--> net_error = -101 (ERR_CONNECTION_RESET)
t=335629 [st=47819] URL_REQUEST_DELEGATE [dt=0]
t=335629 [st=47819] -REQUEST_ALIVE
--> net_error = -101 (ERR_CONNECTION_RESET)
考虑到这一点,您的代码现在应该如下所示
switch($param){
case $value1: // executes if $param = $value1
//execute your code
break; //the break statement forces the processing out of the loop after execution
case $value2: // executes if $param = $value2
//execute your code
break; //the break statement forces the processing out of the loop after execution
default:
//code here executes only if the case 1 and 2 is not matched
}
}
希望这会帮助你!如果您有任何问题,请发表意见并进行排序。