Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic MySQL LIKE Clause

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I've done this countless times but for some reason it is giving grief this time. I am trying to create a dynamic NOT LIKE clause for use in a function that creates a select box. It is working but it is repeating the query over and ever even though there are no other loops within the function and the function itself is not being called inside any loop. This started out as a simple [bold]for[/bold] and was also tried as [bold]foreach[/bold], now [bold]while[/bold], all with identical results. What could be wrong?

Code:
if ($NotLikeSelect):
	$values = $NotLikeSelect;
	while(list(, $value) = each($values)):		
		$LikeWhere .= "AND ". $Field." NOT LIKE '".$value."%' ";
	endwhile;
		$Where = " WHERE ".$Field ." IS NOT NULL ".$LikeWhere;
endif;

In case it helps, here is the function itself:

Code:
function SelectBox($Field, $Key, $Table, $Multiple, $LikeSelect, $NotLikeSelect, $Default = '', $mysqli) {
	global $Selected;
	global $AddRow;

	if ($NotLikeSelect):
		$values = $NotLikeSelect;
		reset($values);
		while(list(, $value) = each($values)):		
			$LikeWhere .= "AND ". $Field." NOT LIKE '".$value."%' ";
		endwhile;
			$Where = " WHERE ".$Field ." IS NOT NULL ".$LikeWhere;
	elseif ($LikeSelect):
		$values = $LikeSelect;
		reset($values);
		while(list(, $value) = each($values)):			
			$LikeWhere .= "AND ". $Field." LIKE '".$value."%' ";
		endwhile;
			$Where = " WHERE ".$Field ." IS NOT NULL ".$LikeWhere;			
	endif;

	if ($Multiple == '1'):
		$Multiple = " multiple=\"multiple\"";
		$Selector = "[]";
		$NumRows = " size=\"2\"";
		$AddRow = "<option value=\"\"></option>\n";
	elseif ($Multiple >= '2'):
		$Multiple = " multiple=\"multiple\"";
		$Selector = "[]";
		$NumRows = " size=\"$Multiple\"";
		$AddRow = "<option value=\"\"></option>\n";		
	else:
		$Multiple = "";
		$Selector = "";
		$AddRow = "";	
		$NumRows = "";
	endif;
	
	$Output = "<select name=\"".str_replace(" ", "", $Default).$Selector."\" Class=\"SelectBox\"".$Multiple.$NumRows." onchange='this.form.submit()'>\n";
	$Output .= "<option value=\"\">".$Default."</option>\n";
	$Output .= "$AddRow";
	$Query = "Select DISTINCT ".$Field .", ID FROM ".$Table.$Where." GROUP BY ".$Field." ORDER BY ".$Field;
	if ($result = $mysqli->query($Query)):
		$i = 0;
		while ($row = $result->fetch_row()):
			if (isset($_POST[$Field]) && $row[$Key] == $_POST[$Field] && $_POST[$Field] != ""):
				$Selected = " SELECTED";
			else:
				$Selected = "";
			endif;
			$Output .= "<option value=\"".$row[$Key]."\"".$Selected.">".substr(basename($row[0], '.csv'),0,50)."</option>\n";
			$i++;
		endwhile;
		$result->close();
	endif;
	$Output .= "</select>";
	$Output .= $row[$Key];
	echo $Query;
}
 
Before anyone tells me I'm an idiot, I'll say so myself! I'm an idiot. I just realized that the function, while not in a loop, is being called several times for the various select boxes so of course the query repeats! No need to reply (unless you want to call me more names) because it is working perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top