Some utility functions in PHP
PHP Database functions
/** getEnumFieldVals()
*get the enum values for an enum field from a table
*/
function getEnumFieldVals($db,$table,$field){
// Get the Status Enum values
$query="show columns FROM $table like '$field'";
$res=$db->query($query);
handleErrors(&$db);
$row=$res->fetchRow(DB_FETCHMODE_ASSOC);
//a regex way to do this is as follows. I am new to regex, so I used my own technique as below
//feel free to try this regex code out: Binod
//$regex = "/'(.*?)'/";
//preg_match_all( $regex , $row['Type'], $enum_array );
//print_r($enum_array);echo "<Br>";
//$enum_fields = $enum_array[1];
//print_r($enum_fields); echo "<Br>";
$type=$row['Type'];
$type=str_replace('(','',$type);
$type=str_replace(')','',$type);
$type=str_replace('enum','',$type);
$enum_types=split(',',$type);
return $enum_types;
}
/**
* function getComboBoxHTMLForField($name,$db,$table,$field,$value)
* Code to generate a database enabled combo box with a given $name
*given a database object $db, $table and $field name,
*returns the html for a combo box which lists the enum values of the enum field $field
*and selects the currently active value, passed as $value
* requires the function getEnumFieldVals, to fetch the enumeration values
*/
function getComboBoxHTMLForField($name,$db,$table,$field,$value){
$enum_Values=getEnumFieldVals($db,$table,$field);
$comboBoxHTML="<select name='$name'>";
foreach($enum_Values as $curValue){
$detectCurValue= ($curValue=="'$value'");
$curValue=str_replace("'","",$curValue);
$comboBoxHTML .="<option value='$curValue'";
if($detectCurValue==1)$comboBoxHTML .=" selected ";
$comboBoxHTML .=">$curValue </option>";
}
$comboBoxHTML .="</select>";
return $comboBoxHTML;
}
PHP Interpolation functions
//SOME INterpolation functions - Binod Pant 03/29/2006
//==========================================================
//insert linearly interpolated values into this array at location x_index, based on the two values y0 and y1 at x0 and x1
//uses the function interpolate
function get_interp_values($x0,$x1,$y0,$y1,$deltaX){
$yret=array();
echo "X1 = $x1 and X2 = $x2, Y1= $y1 and Y2= $y2 <br>";
$num_points=floor(($x1-$x0)/$deltaX);
$m=$num_points-1; //no of points to insert
echo "no of points = $m <br>";
//echo $m."<br>";
for ($i=1;$i<=$m;$i++){
$xi=$x0+$deltaX * $i;
$yret[$i-1]=interpolate($x0,$x1,$y0,$y1,$xi);
//echo "$i value between $x0,$y0 and $x1,$y1 = ".$yret[$i-1]."<br>";
}
return $yret;
}
function interpolate($x0,$xN,$y0,$yN,$xDesired){
$yDesired = $y0 + ($yN-$y0) / ($xN-$x0) * ($xDesired-$x0);
return $yDesired;
}
//Insert at $pos array $items inside array $source
function array_insert($source, $pos, $items){
$sub1 = array_slice($source, 0, $pos);
$sub2 = array_slice($source, $pos);
return array_merge($sub1,$items,$sub2);
}
function strip_infs($xvals='',$array){
$n=count($array)-1;
while(is_infinite($array[$n])){$n--;}
$m=0;
while(is_infinite($array[$m])){$m++;}
//echo $n; echo "<br>";
$j=0;
for($i=$m;$i<$n+1;$i++,$j++){
$retarr[$j]=$array[$i];
$retXarr[$j]=$xvals[$i];
}
return array($retXarr,$retarr);
}
function strip_leading_blanks($array){
$n=0;
while($array[$n]===''){$n++;}
//echo $n; echo "<br>";
$j=0;
for($i=$n;$i<count($array);$i++,$j++)
$retarr[$j]=$array[$i];
return $retarr;
}
//a function to calculate $N points that lie on a straight line between two points $yBegin and $yEnd
//this is not currently used
function getNPointsOnTheLine($yBegin,$yEnd,$N){
$intermediate_points=array();
$totalN=$N+1; //including the end points
$deltaY=($yEnd-$yBegin)/$totalN; //gives the delta value to increment or decrement x
for($i=0;$i<$N;$i++){
$intermediate_points[$i]=$yBegin + ($i+1)*$deltaY;
}
return $intermediate_points;
}
Copyright © 2006, Binod Pant