我对$_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值。
发布于 2013-02-15 23:03:19
为什么不在else
中添加一个子句
elseif(!isset($_GET['institute']) && !isset($_GET['tutor']))
{
echo 'This page has been accessed in error.';
include ('includes/footer.html');
exit();
}
发布于 2013-02-15 23:02:50
你需要确保url看起来像这样:
http://localhost/search.php?subject=Mathematics&tutor=tutorName&institute=instituteName
?
表示URL参数的开始,&
表示url参数之间的分隔。
发布于 2013-02-15 23:06:01
您的问题是,您只检查了$_GET‘’subject‘变量,该变量没有被传入。您可以通过几种方式来完成此操作,所有这些操作都会导致更改:
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&tutor=link">Tutor</a></li>
<li><a href="?searchKey=1&institute=link">Institute</a></li>';
如果您希望一次传入多个变量,则需要将搜索关键字放入数组中。
https://stackoverflow.com/questions/14897396
复制相似问题