2013-02-15 95 views
0

我有$ _GET数组的问题。在我的网页上,值来自像这样的URL。

http://localhost/search.php?subject=Mathematics 

我检查这个$ _GET值是这样的..

// Check for a valid keyword from search input: 
if ((isset($_GET['subject'])) && (is_string ($_GET['subject']))) { // From SESSION 
    foreach ($_GET AS $key => $subject) { 
     $searchKey = $key; 
     $searchKeyword = '%'.$subject.'%'; 
    } 

} else { // No valid keyword, kill the script. 
    echo 'This page has been accessed in error.'; 
    include ('includes/footer.html'); 
    exit(); 
} 

现在,它为我工作。但我的问题是我使用另外两个变量来通过同一页上的URL来过滤我的数据库值。

echo '<li><a href="?tutor=link">Tutor</a></li> 
     <li><a href="?institute=link">Institute</a></li>'; 

这两个链接我用来过滤我的数据库值(点击此链接)。

$tutor = isset($_GET['institute']) ? '0' : '1'; 
$institute = isset($_GET['tutor']) ? '0' : '1'; 

我的问题是,当我在上面的链接试图过滤数据库的结果点击它总是将这个代码,而不是显示过滤结果。

} else { // No valid keyword, kill the script. 
    echo 'This page has been accessed in error.'; 
    include ('includes/footer.html'); 
    exit(); 
} 

任何人都可以告诉我如何使用这3 $ _GET值。

+0

为什么不是两个查询参数? '目标=导师及链接= ...'?这会减少你检查'$ _GET ['target']'是否包含有效的内容,而不是检查三个不同的$ _GET值。 – 2013-02-15 15:02:34

回答

0

您需要确保网址如下:

http://localhost/search.php?subject=Mathematics&tutor=tutorName&institute=instituteName 

一个?表示URL参数的开始,一个&标志着URL参数之间的分离

+0

是我改变了我的链接这样试了一下 - \t \t回声 '

  • Tutor
  • \t \t \t \t
  • Institute
  • '; – TNK 2013-02-15 15:06:11

    +0

    并将此代码更改为 - \t $ tutor = isset($ _ GET ['subject'])&& isset($ _ GET ['institute'])? '0':'1'; \t $ institute = isset($ _ GET ['subject'])&& isset($ _ GET ['tutor'])? '0':'1'; – TNK 2013-02-15 15:06:55

    +0

    但它不适合我.. – TNK 2013-02-15 15:07:17

    1

    为什么不只是增加一个条款在else

    elseif(!isset($_GET['institute']) && !isset($_GET['tutor'])) 
    { 
        echo 'This page has been accessed in error.'; 
        include ('includes/footer.html'); 
        exit(); 
    } 
    
    0

    你的问题是,你只检查$ _GET [ '主题'] variab 。嘞,这是在没有被传递,您可以在几个方式做到这一点,所有这些都将发生变化:

    if ((isset($_GET['subject'])) && (is_string ($_GET['subject']))) { // From SESSION 
    

    1)包括在条件字符串的所有变量:

    if (((isset($_GET['subject'])) && (is_string ($_GET['subject']))) || ((isset($_GET['institute'])) && (is_string ($_GET['institute']))) || ((isset($_GET['tutor'])) && (is_string ($_GET['tutor'])))) { 
    

    2)通在searchKey =您的所有链接,并使用1或东西:

    if (isset($_GET['searchKey'])) { // From SESSION 
    

    修订链接:

    echo '<li><a href="?searchKey=1&amp;tutor=link">Tutor</a></li> 
         <li><a href="?searchKey=1&amp;institute=link">Institute</a></li>'; 
    

    如果您希望一次传递多个变量,则需要将搜索关键字放入数组中。

    +0

    我对此并不清楚:在searchKey = 1或其他所有通话中使用并使用:您可以用一个示例来详细说明吗? – TNK 2013-02-15 15:14:30

    +0

    刚刚修改我的例子,但认为#2更多的是你所需要的。 – fanfavorite 2013-02-15 15:39:24

    0

    你的问题似乎并不是,你运行到else循环(或更好的说:不是只有问题)。看起来你的第一个参数在第二个链接中丢失了。我认为,第二个链接应该像某种扩展搜索过滤器那样反应,这个链接应该适用于最近显示的内容,还是我完全错误地理解你?

    也许这可以解决您创建后续网址的问题。

    ​​

    当你输出的链接:

    echo '<li><a href="'.$url1.'">Tutor</a></li> 
         <li><a href="'.$url2.'">Institute</a></li>'; 
    
    +0

    我们可以聊天吗.. – TNK 2013-02-15 15:21:08