Zend - The PHP Company




Forms

Add Code


Query String Generator  

Type: code fragment
Added by: eddyf
Entered: 02/03/2000
Last modified: 08/12/1999
Rating: *** (4 votes)
Views: 10049
Generates a query string from POST'ed form variables.


<?php
//////////////////////////////////////////////////////////////
// Parameters :
//      $tableName: name of table query string is to used on
//      $conOrDis : "and" if where clause is conjuctive
//                  "or" if where clause is disjunctive
function qStrGen($tableName,$conOrDis) {
    Global 
$HTTP_POST_VARS;
    
$qStr "select * from $tableName where ";
    
$count 0;
   
    while(list(
$key,$val) = each($HTTP_POST_VARS)) {

        if(!
strcmp("update",$key) || strstr($key,"Form")) { //not included in qStr
            
continue;
        }
        else if(
$val) {
            if(
$count) { //check to see if first condition in where clause
                
$qStr .= " ".$conOrDis." ";
            }
            if(
strstr($key,"addr") || !strcmp($key,"fname") || !strcmp($key,"name")) { //allow for partial matching
                
$qStr .= "$key like "%$val%"";
            }
            else
                
$qStr .= "$key="$val"";
            ++
$count;
        }
    }
   
    
$qStr .= ";";

    if(!
$count){ //empty where clause...set qStr to zero or whatever
        
$qStr "0";
    }
    return(
$qStr);
}
?>


Usage Example




Rate This Script





Search



This Category All Categories