Patek Philippe |
2005-10-30 22:31 |
Citat:
Originally posted by guran@Oct 30 2005, 22:53
Har du möjlighet att sätta in koden exakt på hur srchSql och sortWord ser ut?
|
Visst, så här ser det ut.
Kod:
$srchSql = "SELECT l.legal_case_id, l.header, l.body, l.publish_date, l.summary, lt.name, lt.legal_case_type_id, la.name, la.legal_area_id FROM legal_case l, legal_case_type lt, legal_area la";
$srchSql .= " WHERE l.legal_case_type_id = lt.legal_case_type_id AND l.legal_area_id = la.legal_area_id ";
$ors = 0;
$sortLink = "";
if ($_REQUEST["legal_case_type1"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 1";
$ors++;
$sortLink .= '&legal_case_type1=1';
}
if ($_REQUEST["legal_case_type2"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 2";
$ors++;
$sortLink .= '&legal_case_type2=2';
}
if ($_REQUEST["legal_case_type3"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 3";
$ors++;
$sortLink .= '&legal_case_type3=3';
}
if ($_REQUEST["legal_case_type4"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 4";
$ors++;
$sortLink .= '&legal_case_type4=4';
}
if ($_REQUEST["legal_case_type5"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 5";
$ors++;
$sortLink .= '&legal_case_type5=5';
}
if ($_REQUEST["legal_case_type6"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 6";
$ors++;
$sortLink .= '&legal_case_type6=6';
}
if ($_REQUEST["legal_case_type7"] != '') {
if($ors == 0) {
$srchSql .= " AND (";
} else {
$srchSql .= " OR ";
}
$srchSql .= "l.legal_case_type_id = 7";
$ors++;
$sortLink .= '&legal_case_type7=7';
}
if($ors > 0) {
$srchSql .= ")";
}
if($_REQUEST['publish_date'] == 'last_3_days'){
$srchSql .= " AND l.publish_date >= DATE_SUB(CURDATE(),INTERVAL 3 DAY)";
$sortLink .= '&publish_date=last_3_days';
} else if($_REQUEST['publish_date'] == 'last_week'){
$srchSql .= " AND l.publish_date >= DATE_SUB(CURDATE(),INTERVAL 7 DAY)";
$sortLink .= '&publish_date=last_3_days';
} else if($_REQUEST['publish_date'] == 'last_month'){
$srchSql .= " AND l.publish_date >= DATE_SUB(CURDATE(),INTERVAL 30 DAY)";
$sortLink .= '&publish_date=last_month';
}
if($_REQUEST['legal_area'] != ''){
$srchSql .= " AND l.legal_area_id = " . $_REQUEST['legal_area'];
$sortLink .= '&legal_area=' . $_REQUEST['legal_area'];
}
if($_REQUEST['header'] != ''){
$srchSql .= " AND l.header LIKE '%" . $_REQUEST['header'] . "%' ";
$sortLink .= '&header=' . $_REQUEST['header'];
}
if($_REQUEST['bodytext'] != ''){
$keyword = makeboolean("l.body",$_REQUEST['bodytext']);
$keyword2 = makeboolean("l.summary",$_REQUEST['bodytext']);
$srchSql .= " AND (" . $keyword;
$srchSql .= " OR " . $keyword2 . ") ";
$sortLink .= '&bodytext=' . $_REQUEST['bodytext'];
}
$srchSql .= $sortWord;
echo $srchSql;
//$srchSql = "SELECT l.legal_case_id, l.header, l.body, l.publish_date, summary, lt.name legal_casetype, la.name legal_area FROM legal_case l, legal_case_type lt, legal_area la WHERE l.legal_case_type_id = lt.legal_case_type_id AND l.legal_area_id = la.legal_area_id AND (l.body LIKE '% sökord%' OR summary LIKE '% sökord%') ORDER BY l.publish_date DESC";
$rs = new MySQLPagedResultSet($srchSql,20);
EDIT: Missade de sista raderna. Om jag byter till den bortkommenterade raden fungerar det alltså.
Funktionen makeboolean: (Nej, jag kan inte använda fulltextindesk med boolean mode i stället)
Kod:
/***********************************************************/
function makeboolean($sqlfieldname, $keywordstr) {
// pass into this function the sqlfieldname that you want to search by and the original string
// that the user entered into the form field on the search engine.
// After you get back the result from this function, include it into your complete search string.
// Note, you may want to call this function repeatedly to search through various fields in your
// tables.
$keyword = $keywordstr;
// Convert String To Lower Case
$keyword = strtolower($keyword);
// Replace Word Operators With Single Character Operators
$keyword = ereg_replace(" and ","+",$keyword);
$keyword = ereg_replace(" or ","|",$keyword);
$keyword = ereg_replace(" not ","-",$keyword);
// Build The Keywords String Based On Operators Assigned Above
$operatorcount = 0;
$len = strlen($keyword);
for ($z = 0; $z < $len; $z++) {
if(($keyword[$z] == "+") || ($keyword[$z] == "|") || ($keyword[$z] == "-")) {
$operatorpos[$operatorcount] = $z;
$operatorcount++;
}
}
if ($operatorcount != 0) {
for ($z = 0; $z < $operatorcount; $z++) {
if($z == 0) {
$startpos = 0;
$endpos = $operatorpos[$z];
} else {
$startpos = $operatorpos[$z - 1] + 1;
$endpos = $operatorpos[$z];
}
$word = $endpos - $startpos;
$keystring = substr($keyword,$startpos,$word);
$keystring = ereg_replace("\(","",$keystring);
$keystring = ereg_replace("\)","",$keystring);
$keywords[$z] = $keystring;
$operator_pos = $operatorpos[$z];
$operators[$z] = $keyword[$operator_pos];
} // end the for loop
$wordcount = $operatorcount + 1;
$startpos = $operatorpos[$z - 1] + 1;
$len2 = strlen($keyword) - $startpos;
$linestr = substr($keyword,$startpos,$len2);
//store the line into the keywords array
$keywords[$wordcount - 1] = $linestr;
//loop through all of the words in the words array replacing them in the original string with a
//LIKE clause
for ($z=0; $z < $wordcount; $z++) {
$replacekeyword = $keywords[$z];
$y = $z -1;
if ($operators[$y] != "-") //odd case is in a NOT...must do something different!
$keyword = ereg_replace($replacekeyword,"$sqlfieldname LIKE '%$replacekeyword%'",$keyword);
else
$keyword = ereg_replace($replacekeyword,"$sqlfieldname NOT LIKE '%$replacekeyword%'",$keyword);
}
// Replace Our Operators With The Correct SQL Operators
$keyword = ereg_replace("\+"," AND ", $keyword);
$keyword = ereg_replace("\|"," OR ", $keyword);
$keyword = ereg_replace("\-"," AND ", $keyword); //I fudged in the above statement so this possible :-)
} // end if operatorcount != 0
else { //there were no operators in the string
$replacekeyword = $keyword;
if ($keyword != "") {
$keyword = ereg_replace($replacekeyword,"$sqlfieldname LIKE '%
$replacekeyword%'",$keyword);
}
}
return($keyword);
} //end the makebooleanstatement function
///////////////////////////////////////////////////////////////
|