2013-02-23 54 views
0

我一直在设计一些网站。我一直在尝试使用多个if(isset($ _GET ['type'])=='page'){}等函数。他们似乎没有工作,有人能够启发我吗?

<?php 
    if(isset($_GET['type']) == 'all') { 
    // View All Pages Title 
    echo '<h3>View All Pages</h3>'; 

     if($control1 == 'all') { 
      echo '<table width="100%" border="0">'; 
      echo '<tr>'; 
      echo '<th scope="col">Page Name</th>'; 
      echo '<th scope="col">Time Created</th>'; 
      echo '<th scope="col">View</th>'; 
      echo '<th scope="col">Edit</th>'; 
      echo '<th scope="col">Delete</th>'; 
      echo '</tr>';     
      $qry=mysql_query("SELECT * FROM page", $con); 
      if(!$qry) { 
       die("Query Failed: ". mysql_error()); 
     } 
     while($row=mysql_fetch_array($qry)) { 
      echo '<tr>'; 
      echo '<td height="38">'.$row['page_title'].'</td>'; 
      echo '<td>'.$row['page_updated'].'</td>'; 
      echo '<td><a href="page.php?view=page&page='.$row['page_link'].'">View</a></td>'; 
      echo '<td><a href="page.php?edit='.$row['page_link'].'">Edit</td>'; 
      echo '<td><a href="page.php?delete='.$row['id'].'">Delete</td>'; 
      echo '</tr>'; 
     }  
     echo '</table>'; 
    } 
    else { 
     echo "<p>You can't view all the pages. Your Account does not hold the correct priorirty</p>"; 
    } 
} 
elseif(isset($_GET['type']) == 'add') { 
    // Add New Page Title 
    echo '<h3>Add New Page</h3>'; 
     if($control1 == 'all') { 
      echo '<p><a href="pages.php?type=add&add=control" target="new">Click Here to add a new page</a></p>'; 
    } 
    else { 
     echo "<p>You can't add new pages. Your Account does not hold the correct priority.</p>"; 
    } 
} 
elseif(isset($_GET['type']) == 'edit') { 
    // Edit Pages Title 
    echo '<h3>Edit a Page</h3>'; 
     if($control1 == 'all') { 
      echo '<table width="100%" border="0">'; 
      echo '<tr>'; 
      echo '<th scope="col">Page Name</th>'; 
      echo '<th scope="col">Edit</th>'; 
      echo '</tr>'; 
      $qry=mysql_query("SELECT * FROM page", $con); 
       if(!$qry) { 
        die("Query Failed: ". mysql_error()); 
      } 
      while($row=mysql_fetch_array($qry)) { 
       echo '<tr>'; 
       echo '<td height="38">'.$row['page_title'].'</td>'; 
       echo '<td><a href="page.php?edit='.$row['page_link'].'">Edit</td>'; 
       echo '</tr>'; 
      } 
      echo '</table>'; 
     } 
     else { 
      echo "<p>You can't edit any pages. Your Account does not hold the correct priority </p>"; 
     } 
    } 
?> 

这是确切的代码。如果你能解释我能做些什么来解决它,那将不胜感激!提前致谢!

回答

2

isset($_GET['type'])返回一个布尔值,因此,如果您比较,与任何布尔值是不一样的布尔(isset($_GET['type']) == 'all'),它会返回false。

你应该做

if (isset($_GET['type']) && $_GET['type'] == 'all') { // code } 
+0

谢谢,我从来没有意识到。所以我应该用我所有的$ _GET函数来做到这一点? – Fearless 2013-02-23 20:49:48

+0

我只是试了一下,它效果很好!谢谢! – Fearless 2013-02-23 20:56:02

0

必须像这样

if(isset($_GET['type']) && $_GET['type'] == 'page') 
0

isset返回布尔:真或假。因此,您的代码:

if(isset($_GET['type']) == 'page') 

将不起作用。你可以这样改变它:

if(isset($_GET['type']) and $_GET['type'] == 'page') 
0

的问题是,isset()函数只返回TRUEFALSE,你是它比较字符串。只要使用这种方式:

if (isset($_GET['type']) { 
    if ($_GET['type'] == 'page') { 
     //... 
    } elseif($_GET['type'] == 'add') { 
     //and so on... 
    } 
} 

这是更好地把isset测试一开始,所以你不会在每一个if声明检查。

+0

其他人都说todo:if(isset($ _ GET ['type'])和$ _GET ['type'] =='page')所以我不确定你的答案是否正确? – Fearless 2013-02-23 20:51:28

+0

是的,它是正确的:)如果没有设置$ _GET ['type']',检查'$ _GET ['type'] =='add''是没有意义的。但是如果你在每个if语句中检查它,你都会重复自己,但是你真的不需要,你可以检查'$ _GET'是否只设置了一次。尝试一下,它会肯定工作。 – 2013-02-23 21:00:47

+0

感谢彼得,我做了,它有点工作,我可能没有正确实施它。 – Fearless 2013-02-23 21:14:07

0

正如我所看到的,isset()返回一个布尔值(true/false),与“all”,“add”等比较。这不是你想要的。

你可以这样做:

if (isset($_GET['type']) && $_GET['type'] == "all") { 
    #your code here 
}