#!/bin/sh # # This is a shell archive. To extract its contents, # execute this file with /bin/sh to create the file(s): # # README qbe.php # # This shell archive created: Wed Nov 6 16:57:25 EST 2002 # echo "Extracting file README" sed -e 's/^X//' <<\SHAR_EOF > README XThe last Oct 25, 2002 was a thread on the iiug-esp SIG, asking for a Xconstruct like for Visual Basic, I offer my PHP construct equivalent in PHP. X XAs is a short file (5 KB) and received several (more than 3) requests I Xthink could be nice to have it in the repository. X XThis is an example of invocation X X...... X$fields['compmode']='AU'; X$fields['comppart']='AU'; X$fields['compseri']='AU'; X$fields['compproc']='AU'; X$fields['compfvig']='D'; X$qry = "SELECT * FROM equicomp WHERE ".qbe($fields); X$rs = ifx_query($qry, $conn); X$row = ifx_fetch_row($rs, "NEXT"); X...... X XFor this example 'compmode', 'comppart', 'compseri' ... are columns for the Xtable equicomp and also fields in the HTML form. X XThere is still a TODO list and some documentation inside the code. X XHTH X XFernando Ortiz Muñoz XFabrica de Jabón la Corona, SA de CV XTel. (52)(55)57-47-64-80 XFax: (52)(55)5714-37-98 SHAR_EOF if [ `wc -c < README` -ne 873 ] then echo "Lengths do not match -- Bad Copy of README" fi echo "Extracting file qbe.php" sed -e 's/^X//' <<\SHAR_EOF > qbe.php X | X// +----------------------------------------------------------------------+ X// X// $Id$ qbe.php 2002/07/19 X// X/* X* X* @param mixed $fields : Associative array of the list of indexed to the X* $_POST global variable wich are also the column name of the table to X* search. The value is a string of options X* X* 1 = numeric (default) X* A = Alpha, char, string X* D = Date X* U : Convert to uppercase X* M : Use matches (default) X* L : Use Like X* X* @return string with the text of the WHERE condition or " 1=1" if not X* restriction asqued. X* X*/ Xfunction qbe($fields) X{ X if (!is_array($fields)) { X return " 1=1"; X } X $qry = ""; X foreach ($fields as $field => $option) { X $temp = qbe_1field($field,$option); X if (strlen($temp)>0) { X $qry .= (strlen($qry)==0) ? $temp : " and ".$temp; X } X } X return (strlen($qry)==0) ? " 1=1": $qry; X} X Xfunction qbe_1field($field,$option) X{ X if (!isset($_POST[$field])) { /* Not a forms variable */ X return ""; X } X /* Trim, if space needed at beginning quote */ X /* TODO: slashes needed for escaped matches/like */ X $text = trim($_POST[$field]); X if (strlen($text)==0) { /* The user don't type anuthing in field */ X return ""; X } X if (strlen($option)==0) { X $option = "1"; /* No option default is numeric */ X } X if (is_integer(strpos($text,":")) || is_integer(strpos($text,".."))) { X /* the user type range val1:val2 */ X if (is_integer(strpos($text,":"))) { X list($val1,$val2,$extra) = split(':',$text,3); X } else { X /* the user type range val1..val2 */ X $a = strpos($text,".."); X $val1 = trim(str_replace("."," ",substr($text,0,$a))); X $val2 = trim(str_replace("."," ",substr($text,$a))); X } X $val1 = qbe_option($val1,$option); X $val2 = qbe_option($val2,$option); X $temp = $field." between ".$val1." and ".$val2; X } else { X /* Not range single comparation */ X /* TODO: add the list selection IN (.., , .., .. ) */ X $cmp = " = "; X $a = substr($text,0,1); X if ($a == ">" || $a == "<" || $a == "=") { X $cmp = " ".$a; X $text = trim(substr($text, 1)); X $a = substr($text,0,1); X if ($a == ">" || $a == "<" || $a == "=") { X $cmp .= $a; X $text = trim(substr($text, 1)); X } X $cmp .= " "; X } elseif ( is_integer(strpos($text,"*")) || X is_integer(strpos($text,"%")) || X is_integer(strpos($text,"?")) || X is_integer(strpos($text,"_")) ) { X /* TODO: Check for escaped * or % or ? or _*/ X if (!is_integer(strpos($option,"L"))) { X $option .= "M"; /* Matches is default */ X }; X /* TODO: Detect */ X $cmp = (is_integer(strpos($option,"M")))?" MATCHES ":" LIKE "; X $option .= "A"; /* Force alpha if matches or like */ X } X $text = qbe_option($text,$option); X $temp = $field.$cmp.$text; X } X return $temp; X} X Xfunction qbe_option($text, $option) X{ X if (strlen($text)==0) { X return $text; X } X $alpha = is_integer(strpos($option,"A")); X $date = is_integer(strpos($option,"D")); X $upper = is_integer(strpos($option,"U")); X if ($upper) { X $text = strtoupper($text); X } X if ($date) { X $text= qbe_fixdate($text); X } X $ini = substr($text,0,2); X if ($ini != "\\\"" && $ini != "\\'") { X if ($alpha || $date) { /* Alpha or Date quote them */ X $text = "'".$text."'"; X } X } X return $text; X} X Xfunction qbe_fixdate($fech) { X/* X* As some users type the date in format YYYY-MM-DD and the engine (informix) X* is configured with DBDATE=DMY2/, we need to fix it X*/ X $format = getenv("DBDATE"); /* format of date required for DB engine */ X if (strlen($format) <= 3 || strlen($format) > 5) { X $format = "DMY2/"; /* Se default if not set in environment */ X } X if (strlen($format) == 3) { X $format .= "2"; /* Assume 2 digits for year */ X } X if (strlen($format) == 4) { X $format .= "/"; /* Assume slashes as delimiter */ X } X $f1 = substr($format,0,1); X $f2 = substr($format,1,1); X $f3 = substr($format,2,1); X $deli = substr($format,-1); X $text = stripslashes($fech); X $text = str_replace("\""," ",$text); /* Remove quote */ X $text = trim(str_replace("'"," ",$text)); /* Remove quote */ X $text = str_replace("-","/",$text); /* of other deli change by slash */ X $text = str_replace(" ","/",$text); /* of other deli change by slash */ X if (is_integer(strpos($text,"/"))) { X list($a1, $var['M'], $a2, $extra) = split("/",$text,4); X } else { X /* If typed just >180702 */ X $a1 = substr($text,0,2); X $var['M'] = substr($text,2,2); X $a2 = substr($text,4); X } X if ($a1 > 99) { X $var['Y'] = $a1; X $var['D'] = $a2; X } else { X $var['Y'] = $a2; X $var['D'] = $a1; X } X $text=$var[$f1].$deli.$var[$f2].$deli.$var[$f3]; X return $text; X} X?> SHAR_EOF if [ `wc -c < qbe.php` -ne 6453 ] then echo "Lengths do not match -- Bad Copy of qbe.php" fi echo "Done." exit 0