2013-02-23 52 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 
}