2014-10-12 21 views
-6

我试图用if语句获得一个包含3个部分的页面。请看代码,并帮助我。

如果你不告诉我学习这个或那个,我会很高兴,因为我正在做这件事。

<?php 
if (isset($_GET['part'])&&!empty($_GET['part'])){ 
    if($_GET['part'] = '1'){ 
     ## Part 1 
     ## Some content 
     echo '1'; 
    }else if($_GET['part'] = '2'){ 
     ## part 2 
     ## Some content 
     echo '2'; 
    }else if($_GET['part'] = '3'){ 
     ## Part 3 
     ## Some content 
     echo '3'; 
    } 

    }else{ 
     header('Location: index.php?part=1'); 
    } 
?> 
+3

在条件相等性比较应该是''==不'='。所以你应该有'$ _GET ['part'] =='value''。 – Bogdan 2014-10-12 12:00:31

+0

,而不是一个等号,用于分配使用两个等号作比较! – goseo 2014-10-12 12:01:47

+0

谢谢,这是帮助:) – Magnus 2014-10-12 12:12:42

回答

0

这难道不容易吗?

<?php 
switch ($_GET['part'])) { 
    case '1': 
     ## part 1 
     ## Some content 
     break; 
    case '2': 
     ## part 2 
     ## Some content 
     break; 
    case '3': 
     ## Part 3 
     ## Some content 
     break; 
    default: 
     header('Location: index.php?part=1'); 
} 
?> 
+0

它可能是。但为什么我的代码不工作? :S 但交换机看起来更好。 – Magnus 2014-10-12 12:04:10

+1

@Magnus,你的代码不会工作,它在评论中解释! – goseo 2014-10-12 12:05:13

0
<?php 
if (isset($_GET['part'])&&!empty($_GET['part'])){ 
    if($_GET['part'] >=1 or $_GET['part'] <=3) 
     header('Location: index.php?part = $_GET['part']'); 
    else 
     header('Location: index.php?part = 1'); 
?>