I see. Well I know that I am able to successfully return a value called $status but it is a variable defined within a method, not in the class. At the end of the CheckParams() method in my class, I return the value of $status (a variable defined in the function) and this is successful when I just call the method directly like so:
Code: Select all
$checker = new ParamsChecker();
$status = $checker->CheckParams($_POST, $_GET);
When I do a variable dump on $status, the correct value is returned. So that part of the script now works. The value of $status is then passed to a method in another class. The method in the second class first runs a function that uses a switch value to run a function based on the contents of the $status variable passed to it. At the end of each function called by the switch, the value of the variable $st is defined and returned. The strange this is that the switch statement successfully runs the correct function based on the value of $status, but fails to return the correct value of $st. Here's the code I'm referring to.
Code: Select all
<?php
class CatalogResultBuilder {
function FunctionSwitch($post, $get, $link, $status) {
switch ($status) { //Check to see what the contents of $status is, and depending on its value, perform the appropriate function
case "ShowSearchResults":
$this->ShowSearchResults($post, $link);
break;
case "ShowCategory":
$this->ShowCategory($get, $link);
break;
case "ShowErrorMessage":
$this->ShowErrorMessage();
break;
case "DoNothing":
$this->DoNothing();
break;
}
}
function ShowSearchResults($post, $link) {
$searchParams = 'SELECT * FROM catalog WHERE'; //Create a variable with the
$counter = 0; //This variable will determine the styling of a row based on whether it is odd or even
foreach($post as $key => $value) {
$valueLength = strlen($value);
if(($valueLength != 0) && ($key != 'searchsubmit')) { //Iterate through the $_POST array and determine if it contains data - also exclude the submit button's value
$partialMatch = "'%".$value."%'"; //If it contains data, add % characters to match anything before or after the search field value
$searchParams .= " ".$key." LIKE ".$partialMatch." &&"; //Add search field values to SQL query
}
}
$query = substr($searchParams, 0, -2); //Strip extra ampersands off of the end of the query
$query .= ';'; //Add a semi-colon to the end of the query
$st = mysql_query($query, $link) or die(mysql_error()); //Query the database
if(!$st) {
$st = "Your search returned 0 results.";
}
return $st;
}
function ShowCategory($get, $link) {
if (!is_null($get['param'])) {
$query = "SELECT * FROM catalog WHERE ".$get['param']."='".$get[$get['param']]."';"; //If the parameter value is not null, create a database query with the values
$st = mysql_query($query, $link) or die(mysql_error()); //Query the database and store in $st
}
return $st;
}
function ShowErrorMessage() {
$st = "Please enter a search word!";
return $st;
}
function DoNothing() {
echo "!!!!!!!!!";
$st = "Enter a search!";
return $st;
}
}
?>
And here's how I'm calling it:
Code: Select all
$builder = new CatalogResultBuilder();
$st = $builder->FunctionSwitch($_POST, $_GET, $link, $status);
The part I'm focusing on is the switch statement at the beginning:
Code: Select all
function FunctionSwitch($post, $get, $link, $status) {
switch ($status) { //Check to see what the contents of $status is, and depending on its value, perform the appropriate function
case "ShowSearchResults":
$this->ShowSearchResults($post, $link);
break;
case "ShowCategory":
$this->ShowCategory($get, $link);
break;
case "ShowErrorMessage":
$this->ShowErrorMessage();
break;
case "DoNothing":
$this->DoNothing();
break;
}
}
And the function called DoNothing() :
Code: Select all
function DoNothing() {
echo "!!!!!!!!!";
$st = "Enter a search!";
return $st;
}
I know the switch works, because I have checked the value of $status before passing it to FunctionSwitch() and it's value is "DoNothing". Based on the switch statement, if the value of $status is "DoNothing" it should run the DoNothing() function. The series of exclamation marks shows up successfully, showing that the switch statement is running the correct function. The problem now is that the value returned by the function DoNothing() is not "Enter a search!" as it should be, it comes out as NULL instead. Any idea why this is happening? Maybe it has to do with the fact that I'm using the same variable name ($st) in each function I run. Thanks again! I will keep trying to fix it and post any success I have.