Please help. I've been stuck on this for days. I've searched and searched for a solution, but I've had no luck.
I have the following:
$query = "SELECT * FROM leagues WHERE 1";
if (!empty($_GET['lg_id'])) { $query .= " AND lg_id LIKE '%".$_GET['lg_id']."%'"; }
if (!empty($_GET['lg_initials'])) { $query .= " AND lg_initials LIKE '%".$_GET['lg_initials']."%'"; }
//and numerous similar other lines
$query .= " ORDER BY lg_full_name ASC";
, which produces the query I want in:
SELECT * FROM leagues WHERE 1 AND defunct LIKE '%UserInput%' AND site_address LIKE '%UserInput' ORDER BY lg_full_name ASC
However, I want to read the table header names from an array and for loop through that middle chunk. To do that, I've gotten:
$query = "SELECT * FROM leagues WHERE 1";
// include that contains the $hs array of table header names
include("includes/arrays.inc");
function get_search_pars($headershort) {
if (!empty($_GET[$headershort])) {
" AND $headershort LIKE '%".$_GET[$headershort]."%'";
}
}
for ($x = 1; $x < count($hs); $x++) {
get_search_pars($hs[$x]);
}
$query .= " ORDER BY lg_full_name ASC";
But the problem is I can't figure out how to get that middle stuff to concatenate the query like in the original code:
I tried switching:
" AND $headershort LIKE '%".$_GET[$headershort]."%'";
with:
$query .= " AND $headershort LIKE '%".$_GET[$headershort]."%'";
and I can echo that right after with:
echo "$query .= ' AND $headershort LIKE '%".$_GET[$headershort]."%'";
, which produces:
Notice: Undefined variable: query in e:\program files\easyphp1-8\www\iold\search.php on line 55
AND defunct LIKE '%Yes%' .= ' AND defunct LIKE '%Yes%'
Notice: Undefined variable: query in e:\program files\easyphp1-8\www\iold\search.php on line 55
AND site_address LIKE '%http://%' .= ' AND site_address LIKE '%http://%'
, which tells me I might be on the right track, but then it wants to use the query variable inside the function and I obviously don't want it to because it's copying itself.
So, I figured what I really want to do is something like:
$query .= "
for ($x = 1; $x < count($hs); $x++) {
get_search_pars($hs[$x]);
}
";
, but of course that doesn't work.
So, I tried things like:
for ($x = 1; $x < count($hs); $x++) {
$query .= " get_search_pars($hs[$x]);";
}
But that doesn't work either. 🙁
How can I concatenate that looped middle chunk to the query?
Yes, I know I should filter my input and escape my output, but one step at a time here. 🙂
Thanks in advance.