"; } // send a query to MySQL server. // display an error message if there // was some error in the query Function MySQLQuery($query) { $success= mysql_db_query($GLOBALS["DB_DBName"], $query); if(!$success) { echo mysql_errno().": ".mysql_error()."
"; echo "
"; echo $query; echo "
"; } return $success; } /* the function remove single quote from the string and replace it with two single quotes strString: string to be fixed returns: fixed string */ function FixString($strString) { $strString = str_replace("'", "''", $strString); $strString = str_replace("\'", "'", $strString); return $strString; } /* the function returns true if strString contains strFindWhat within itself otherwise it returns false strString: string to be searched in strFindWhat: string to be searched returns: true if found, flase otherwise */ function HasString($strString, $strFindWhat) { $nPos = strpos($strString, $strFindWhat); if (!is_integer($nPos)) return false; else return true; } // find the number of records in a table // // strTable: name of table to count records in. // strCriteria: select criteria, // if this is not passed, returns the number of all // rows in the table // returns: number of rows in the table // function RecCount($strTable, $strCriteria = "") { if(empty($strCriteria)) $strQuery = "select count(*) as cnt from $strTable;"; else $strQuery = "select count(*) as cnt from $strTable where $strCriteria;"; $nResult = MySQLQuery($strQuery); $rstRow = mysql_fetch_array($nResult); //echo $rstRow["cnt"]; return $rstRow["cnt"]; } /* the function returns an associative array containing the field names and their type strTable: table name to be described returns: associative array, for instance: "user_id" => "int(11)" "user_name" => "varchar(32)" */ function DescTable($strTable) { $strQuery = "desc $strTable"; $nResult = MySQLQuery($strQuery); $arrArray = array(); while($rstRow = mysql_fetch_array($nResult)) { $arrArray[$rstRow["Field"]] = $rstRow["Type"]; } return $arrArray; } /* the function updates the given table. strTable: table name to be updates. strWhere: where clause for record selection. arrValue: an associated array with key-value of fields to be updated. */ function UpdateRec($strTable, $strWhere, $arrValue) { $strQuery = " update $strTable set "; reset($arrValue); while (list ($strKey, $strVal) = each ($arrValue)) { $strQuery .= $strKey . "='" . FixString($strVal) . "',"; } // remove last comma $strQuery = substr($strQuery, 0, strlen($strQuery) - 1); $strQuery .= " where $strWhere;"; // execute query MySQLQuery($strQuery); } /* the function insert a record in strTable with the values given by the associated array strTable: table name where record will be inserted arrValue: assoicated array with key-val pairs returns: ID of the record inserted */ function InsertRec($strTable, $arrValue) { $strQuery = " insert into $strTable ("; reset($arrValue); while(list ($strKey, $strVal) = each($arrValue)) { $strQuery .= $strKey . ","; } // remove last comma $strQuery = substr($strQuery, 0, strlen($strQuery) - 1); $strQuery .= ") values ("; reset($arrValue); while(list ($strKey, $strVal) = each($arrValue)) { $strQuery .= "'" . FixString($strVal) . "',"; } // remove last comma $strQuery = substr($strQuery, 0, strlen($strQuery) - 1); $strQuery .= ");"; // execute query MySQLQuery($strQuery); //echo $strQuery . "
"; // return id of last insert record return mysql_insert_id(); } // the function returns the assocatied array containing // the field name and field value pair for record. // // strTable: table name. // strCriteria: where criteria // function GetRecord($strTable, $strCriteria) { $strQuery = "select * from $strTable "; if(!empty($strCriteria)) $strQuery .= "where $strCriteria;"; $nResult = MySQLQuery($strQuery); return mysql_fetch_array($nResult); } /* the function deletes the record from the given table. strTable: table name. strCriteria: where criteria */ function DeleteRec($strTable, $strCriteria) { $strQuery = "delete from $strTable where $strCriteria"; MySQLQuery($strQuery); } // the function displays the records from the given table // in an nicely formatted HTML table with edit and delete // icons along every record. It also displays next and // previous links to browse the entire table. // // strTable: Table name to be shown. // strCriteria: Expression for where cluase, if empty no where. // is added to the query and all records from // the table are selected. // strOrderBy: Field names for order by clause. // strField: Field name to be displayed // strScript: Script name to be used for new, edit and delete // nRows: Number of rows to be shown per page. // nStart: Start offset of record. // strNewLink: Extra parameters with new link. // strNewTarget: link target on New link // strCallBack: Function given in this argument is called at the end // of each record. // function ShowTable($strTable, $strCriteria, $strOrderBy, $strField, $strScript, $nRows, $nStart, $strNewLink, $strNewTarget, $strCallBack = null, $strEditTarget = null) { $strColor1 = "#86A8EC"; $strColor2 = "#D8D8D8"; // if $arrAddlLinks is null if($arrAddlLinks == null) $arrAddlLinks = array(); // if we are not passed any starting value if(empty($nStart)) $nStart = 0; // lets start from scratch $nNext = $nStart + $nRows; $nPrev = $nStart - $nRows; $nTotalRec = RecCount($strTable, $strCriteria); if(!empty($strNewTarget)) $strNewTarget = "target=$strNewTarget"; echo ""; echo " "; echo " "; $nShowingStart = $nStart+1; if($nStart+$nRows > $nTotalRec) $nShowingEnd = $nTotalRec; else $nShowingEnd = $nShowingStart + $nRows - 1; echo ""; // display all admins from tblAdmin if (empty($strCriteria)) $strQuery = "select * from $strTable "; else $strQuery = "select * from $strTable where $strCriteria "; echo " "; echo " "; echo "
New  "; if($nTotalRec) { if ($nPrev > -1) echo ""; else echo ""; echo ""; if ($nNext < $nTotalRec) echo ""; else echo ""; } echo "

"; echo ""; if(!empty($strOrderBy)) $strQuery .= " order by $strOrderBy "; $strQuery .= " limit $nStart, $nRows;"; $nResult = MySQLQuery($strQuery); while($rstRow = mysql_fetch_array($nResult)) { reset($arrAddlLinks); $nId = $rstRow[0]; $strBGColor = " bgcolor=" . ($bColor ? $strColor1 : $strColor2); echo ""; echo " "; echo " "; echo " "; if(!empty($strCallBack)) { echo " "; } echo ""; $bColor = !$bColor; } echo "
"; echo " Edit"; echo " "; echo " Delete"; echo " "; echo $rstRow[$strField]; echo " "; // callback function eval("echo $strCallBack(\$rstRow);"); echo "\r\n
"; } // the displays a text field in HTML row with two columns in it. // left column contains label and right column contains the // text field. // // strLabel: Label in left column. // strField: Text field name in form. // strValue: Value to be shown in text field. // nSize: Size attribute of text field. // nMaxLength: Max length attribute of text field. // bPassword: 1 if to be displayed as password, 0 as text // strExtra to write some thing extra like some onClick="alert('I'm Good')" // function TextField($strLabel, $strField, $strValue, $nSize, $nMaxLength, $bPassword, $strExtra="") { echo ""; echo " "; echo $strLabel; echo " "; echo " "; if($bPassword) echo " "; else echo " "; echo " "; echo ""; } function TextField2($strLabel, $strField, $strValue, $nSize, $nMaxLength, $bPassword, $strExtra="") { //echo ""; //echo " "; echo $strLabel; //echo " "; //echo " "; //if($bPassword) // echo " "; //else echo " "; //echo " "; //echo ""; } function ReadOnlyField($strLabel, $strField, $strValue, $nSize, $nMaxLength, $strExtra="") { echo ""; echo " "; echo $strLabel; echo " "; echo " "; echo " "; echo " "; echo ""; } // the displays a read only text field for as date field in HTML row with two columns in it. // left column contains label and right column contains the // text field. // // strLabel: Label in left column. // strField: Text field name in form. // strValue: Value to be shown in text field. // nSize: Size attribute of text field. // nMaxLength: Max length attribute of text field. // strFormName: Name of HTML form // function DateField($strLabel, $strField, $strValue, $nSize, $nMaxLength, $strFormName) { $strUnique = time(); echo ""; echo " "; echo $strLabel; echo " "; echo " "; echo " "; echo " "; echo ""; } function DateField2($strLabel, $strField, $strValue, $nSize, $nMaxLength, $strFormName) { $strUnique = time(); //echo ""; //echo " "; echo $strLabel; //echo " "; //echo " "; echo " "; //echo " "; //echo ""; } /* the function displays OK and Cancel buttons in the form */ function OKCancelButtons() { echo ""; echo " "; echo " "; echo " "; echo " "; echo ""; } function OKCancelButtons2() { //echo ""; //echo " "; //echo " "; echo " "; //echo " "; //echo ""; } function OKCancelButtons3() { echo ""; echo " "; echo " "; echo " "; echo " "; echo ""; } function OKCancelButtons4() { echo ""; echo " "; echo " "; echo " "; //echo " "; echo " "; echo ""; } /* the function creates an hidden field strName: name of hidden field strValue: value to be passed in hidden field */ function HiddenField($strName, $strValue) { echo "\r\n"; } /* the function creates a text area strLabel: Label in left column. strField: Text field name in form. strValue: Value to be shown in text field. nRows: number of rows in text area nCols: number of columsn in text area */ function TextArea($strLabel, $strField, $strValue, $nRows, $nCols) { echo ""; echo " "; echo $strLabel; echo " "; echo " "; echo " "; echo " "; echo ""; } /* the function creates a file upload widget on form. strLabel: Label in left column strFileName: File name */ function FileUpload($strLabel, $strFileName) { echo ""; echo " "; echo $strLabel; echo " "; echo " "; echo ""; echo " "; echo ""; } /* the function displays combox box nSelectedVal: index of selected value arr: array containig items to be displayed bIndexValue: true: use array index as item value e.g: 0, 1, 2, ... false: use array value as item value e.g: 2003, 2004, 2005, ... */ function ComboBox($nSelectedVal, $arr, $bIndexValue) { for($i=0; $i < sizeof($arr); $i++) { $j = $i+1; if($j == $nSelectedVal) if($bIndexValue == true) echo "