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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Advanced search

Status
Not open for further replies.

blasterstudios

Technical User
Dec 30, 2004
128
US
Well, first of, this doesn't work. I don't know why. For some reason, my WHERE string is not being created. secondly, is there an easier way to do this?
Code:
$like = "";
if(isset($_GET['s']) && $_GET['s'] != ""){
	$trimmed = trim($_GET['s']);
	$like1 = " contactname LIKE '%$trimmed%'";
	$like = $like1;
}
if(isset($_GET['sn']) && $_GET['sn'] != ""){
	$sn = trim($_GET['sn']);
	$like2 = " contactname LIKE '%$sn%'";
	if($like != ""){
		$like = $like." AND".$like2;
	}
}
if(isset($_GET['st']) && $_GET['st'] != ""){
	$st = trim($_GET['st']);
	$like3 = " title LIKE '%$st%'";
	if($like != ""){
		$like = $like." AND".$like3;
	}
}
if(isset($_GET['so']) && $_GET['so'] != ""){
	$so = trim($_GET['so']);
	$like4 = " organization LIKE '%$so%'";
	if($like != ""){
		$like = $like." AND".$like4;
	}
}
if(isset($_GET['sa1']) && $_GET['sa1'] != ""){
	$sa1 = trim($_GET['sa1']);
	$like5 = " address1 LIKE '%$sa1%'";
	if($like != ""){
		$like = $like." AND".$like5;
	}
}
if(isset($_GET['sa2']) && $_GET['sa2'] != ""){
	$sa2 = trim($_GET['sa2']);
	$like6 = " address2 LIKE '%$sa2%'";
	if($like != ""){
		$like = $like." AND".$like6;
	}
}
if(isset($_GET['sc']) && $_GET['sc'] != ""){
	$sc = trim($_GET['sc']);
	$like7 = " city LIKE '%$sc%'";
	if($like != ""){
		$like = $like." AND".$like7;
	}
}
if(isset($_GET['ss']) && $_GET['ss'] != ""){
	$ss = trim($_GET['ss']);
	$like8 = " state LIKE '%$ss%'";
	if($like != ""){
		$like = $like." AND".$like8;
	}
}
if(isset($_GET['st']) && $_GET['st'] != ""){
	$st = trim($_GET['st']);
	$like9 = " zip LIKE '%$st%'";
	if($like != ""){
		$like = $like." AND".$like9;
	}
}
if(isset($_GET['sp1']) && $_GET['sp1'] != ""){
	$sp1 = trim($_GET['sp1']);
	$like10 = " phone1 LIKE '%$sp1%'";
	if($like != ""){
		$like = $like." AND".$like10;
	}
}
if(isset($_GET['sp2']) && $_GET['sp2'] != ""){
	$sp2 = trim($_GET['sp2']);
	$like11 = " phone2 LIKE '%$sp2%'";
	if($like != ""){
		$like = $like." AND".$like11;
	}
}
if(isset($_GET['sf']) && $_GET['sf'] != ""){
	$sf = trim($_GET['sf']);
	$like12 = " fax LIKE '%$sf%'";
	if($like != ""){
		$like = $like." AND".$like12;
	}
}
if(isset($_GET['se']) && $_GET['se'] != ""){
	$s3 = trim($_GET['se']);
	$like13 = " email LIKE '%$se%'";
	if($like != ""){
		$like = $like." AND".$like13;
	}
}
if(isset($_GET['sw']) && $_GET['sw'] != ""){
	$sw = trim($_GET['sw']);
	$like14 = " website LIKE '%$sw%'";
	if($like != ""){
		$like = $like." AND".$like14;
	}
}
if(isset($_GET['l'])){
	if($like != ""){
		$like = $like." AND (contactname LIKE '".$_GET['l']."%' OR contactname LIKE '% ".$_GET['l']."%')";
	} else {
		$like = " contactname LIKE '".$_GET['l']."%' OR contactname LIKE '% ".$_GET['l']."%'";
	}
}
$where = "";
if($like != ""){
	$where = " WHERE";
}

$query_getcontacts = "SELECT * FROM hgs_contacts".$where.$like.$sort;

all of the $_GET['s*'] variables are submitted via the advanced search. for instance $_GET['sa1] is the address1 field of the search. is there something i need to do like rawurlencode() or something to get this to work? My fields are name, title, organization, phone1, phone2, fax, email, website, city, state, zip, and some others... What looks wrong?
 
oh, wait, i forgot to my mention, my WHERE string works when $_GET['l'] is set or $_GET['s'] is set. It doesn't work for the rest of them.
 
1) with a possible 14 liike statements, you'd be lucky to get a result this side of the next millenium.
2) $like." AND".$like .... would return a query string like this:

$like AND$like AND$like AND$like .... need to add a space then.

$like." AND ".$like.....
3) umm,
$where = ""; // is nothing
if($like != ""){ // if like is not nothing - possibly use if(!empty($like)){ instead
$where = " WHERE"; //set $where = " WHERE " < need a space here
}

4) don't recall seing $sort but you probably need another space there too, perharps try echoing $query_getcontacts to the page to make sure it reads ok.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
sorry, last night, after i was already in the bed, i realized what was wrong

for each of these if statements, i don't have an else. the space thing is not an issue because each like# has the appropriate spacing in it. anyway, since the first variable is blank, the rest of them show up as blank because it only gives each like a value if like already has a value. i didn't declare for it to do anything if like doesn't have a value!

anyway, what this is doing is:

like1 = [space]string
like = like1
like2 = [space]string
like = like1.like2 ([space]string[space]string)
etc.....

then if like is not blank, then WHERE is put into my SQLquery. otherwise, just don't put anything

anyway, it works now, let me know if you really want to know how i did this, and i can explain better. it's designed to use for an advanced search through a MySQL table. I guess i could have used POST, but o well...

by the way, i did have to add rawurldecode($like#) to each of my likes to plan for all those %2F and %3A things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top