I am trying to develop a basic select statement that I can use over and over with adjustabel parameters.
Here is the code I originally had which works just fine:
$myArray = array("user", "category");
$num = count($myArray);
$sq = "";
$sq .= "SELECT * FROM $table_name ";
$arr_clause = array();
// ************
//these are what I am trying to replace with a for loop
if ($user != ""){
$arr_clause[] = "user='" . $user . "'";
}
if ($category != "") {
$arr_clause[] = "category='" . $category . "'";
}
// *** above is what I am replacing
$str_clause = implode(" AND " , $arr_clause);
if ($str_clause != ""){
$sq .= " WHERE " . $str_clause;
}
$result = ...
I am replacing the a piece of the above code with a for loop, if you look at my comments it tells which block I am replacing.
Here is the for loop that replaces it:
for ($i=1; $i<=$num; ++$i)
{
$name = $myArray[$i-1];
$vName = "$".$name;
if ($vName != "")
{
$arr_clause[] = "$name='" . $vName . "'";
}
}
As soon as I put the for loop in place of the original script, it doesn't seem to work.
hopefully you understand my explanation.
The reason I want to do this is so I can put this statement in file and use it as a function over and over again. I want to be able to teach someone how to populate the array and leave the rest of the code alone. I am pretty sure this is possible and maybe I am not approaching it the best way. Any help would be greatly appreciated.
Thanks for you time.