用其他功能禁用php功能

时间:2014-11-10 11:48:30

标签: php

我想知道在调用另一个函数时是否有办法禁用现有的php函数(或部分)。

我的情况如下: 我有一个带文本输入的php搜索功能。这很好用。 (见下面的代码)

if(isset($_POST['search'])){
if(isset($_GET['go'])){
if(preg_match("/[A-Z | a-z]+/", $_POST['search'])){
$input=$_POST['search'];

//connects to database
//selects database table
//searches database
//assigns $results variable

//outputs results:
while($row=mysql_fetch_array($result)){
echo 'results...';

}}}} else {
//connects to database
//selects database table
//searches database:
$sql="SELECT * FROM table";
//outputs results (same as above)
}

这是我工作搜索功能的一种非常简短的形式。在这个非常搜索功能下面,我有一个按钮,当点击时,应该输出一个特定字符串的搜索结果。这也有效。问题是当点击这个按钮时,显示上面函数的“else-part”,当我只希望它显示该特定字符串的结果时,没有其他“表项目“...... 现在遵循search-for-one-specific-string部分的代码:

function results_for_one_specific_string () {
//connects to database

//searches database:
$sql="SELECT * FROM table WHERE Name='string'";
//outputs results (same as above)
}

if (isset($_GET['button123'])) {
results_for_one_specific_string ()
}

只是为了通知您,为了致电function results_for_one_specific_string (),我使用了类似的内容:

<a href="index.php?button123=true" title="">title</a>

现在我的问题是:当function results_for_one_specific_string ()是{{1}}时,是否有人知道如何以某种方式'禁用'我的搜索功能的“其他部分”(第1块代码)叫什么名字? (这样只打印此函数的结果,而不是第一个函数的'else'方)

谢谢你们!

2 个答案:

答案 0 :(得分:0)

这样的事情对你有用吗?

$hasBeenCalled = false; // Set a global var

function results_for_one_specific_string () {
    //connects to database
    //searches database
    //outputs results
}

if (isset($_GET['button123'])) {
    $hasBeenCalled = true; // Update global var here
    results_for_one_specific_string ()
}

...

if(isset($_POST['search'])){
    //connects to database
    //searches database
    //outputs results
} else if($hasBeenCalled==false){ // Check for global var here
    //outputs all table items (not only the search results, everything!)
}

答案 1 :(得分:0)

对我而言,您似乎拥有相同的代码来搜索自定义字符串以及特定字符串。如果这是真的,那么为两种情况制作一个函数会更容易和更透明:

function results_for_any_string ($search='') {
if(strlen($search>0)){
//connects to database
//searches database
//outputs results
}
else {
//outputs all table items (not only the search results, everything!)
}
}

在主脚本中选择您要用作参数的内容。

if(isset($_POST['search'])) $parameter = $_POST['search'];
else if (isset($_GET['button123']))  $parameter = $_GET['button123'];
else $parameter='';
results_for_any_string ($parameter);
}

如果noth按钮在一个表单中,您也可以通过相同的表单和POST提交特定值。不混合帖子和得到。请参阅示例this post