Combining Two Codes for HTML Widgets
Here are some example for using the HTML code and database routines shown in previous articles to make HTML widgets database update able.
You can find the code at https://github.com/scottauge/workbench/tree/master/workbench/API. You can just look at the file, click raw, and cut and paste it if you don’t have any git tools.
This code is responsible for the basics that a programmer can use to put out the widgets. They can put in the data, write code specific to this, and document the change.
I have basically made a class HtmlWidget that one can call an instance with the database code and prefill with data from the database. Then there is code for making the widgets from the database.
The database is configured as this:
With the data as this:
One basically fills the instance with database connection on its creation and it is just calling the method with the table and where class.
There are more input widgets (DateTime) etc. that can be added for rendering! So something for you to try too!
领英推荐
The code is (parameter_init.php):
//
// Create a drop down list with data initialized from parameter
//
include_once "c:/tmp/Renderhtmltoolkit.php";
include_once "c:/tmp/incdb.php";
class HtmlWidget extends RenderHtmlWidget {
var $Widget;
var $Conn;
// Pass in a database connection
public function __construct ($Connection) {
$this->Conn=$Connection;
}
// Construct a drop down with a where
function db_Select($S, $W) {
$V = $this->Select($S) . $this->CR();
$SQL=AforEach($S,"Title='$W'");
$Result=ApplySQL($this->Conn, $SQL);
while($row=$Result->fetch_assoc()) {
$j=$this->NumEntries($row["Text"]);
$i=1;
while ($i<=$j) {
$V .= $this->addoption($this->Entry($row["Text"],$i)) . $this->CR();
$i++;
}
} // while
$V .= $this->eSelect() . $this->CR();
return $V;
} // function db_select
// simple db doc
function db_Render ($W) {
$V = "";
$SQL=AforEach("parameter","Title='$W'");
$Result=ApplySQL($this->Conn, $SQL);
while($row=$Result->fetch_assoc()) {
$tmp=$row['Text'];
$V .= $tmp;
}
$V .= $this->CR();
return $V;
}
function db_Radio($Name, $W) {
$V="";
$SQL=AforEach("parameter","Title='$W'");
$Result=ApplySQL($this->Conn, $SQL);
while($row=$Result->fetch_assoc()) {
$j=$this->NumEntries($row["Text"]);
$i=1;
while ($i<=$j) {
$V .= $this->Entry($row["Text"],$i);
$V .= $this->Radio($Name, $this->Entry($row["Text"],$i));
$V .= $this->CR();
$i++;
}
} // while
return $V;
} // function db_Radio()
function db_TextInput($Name, $Size, $W) {
$V="";
$SQL=AforEach("parameter","Title='$W'");
$Result=ApplySQL($this->Conn, $SQL);
while($row=$Result->fetch_assoc()) {
$V .= $this->TextInput($Name, $row["Text"], $Size);
} // while
return $V;
}
function db_Button($Name) {
$V=$this->Button($Name);
return $V;
}
function db_Hidden($Name, $W) {
$V="";
$SQL=AforEach("parameter","Title='$W'");
$Result=ApplySQL($this->Conn, $SQL);
while($row=$Result->fetch_assoc()) {
$V .= $this->Hidden("name", $this->Hidden($Name, $row["Text"]));
} // while
return $V;
}
function NumEntries($a) {
$an = explode (',', $a);
return count($an);
}
function Entry($h, $n) {
$n--;
$hn=explode("," , $h);
return $hn[$n];
}
} // class HtmlWidget
$L["Host"]="localhost";
$L["User"]="root";
$L["Password"]="";
$L["DB"]="background";
$Conn = Aconnect($L);
$Parameter = new HtmlWidget ($Conn);
echo $Parameter->form("a", "get");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->db_Select("parameter", "Test");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->db_TextInput("name", 20, "Test");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->db_Render("Test");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->db_Radio("scott", "Test");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->Checkbox("id", "name", "");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->Submit("Go");
echo $Parameter->CRLF();
echo $Parameter->CR();
echo $Parameter->Button("Nothing");
echo $Parameter->CR();
echo $Parameter->Hidden("name", "Test");
echo $Parameter->CR();
echo $Parameter->eform();
Adisconnect($Conn);
which is available in scottauge/workbench in the API directory on github.com (https://github.com/scottauge/workbench/tree/master/workbench/API).
The RenderHtmlWidget.php is as follows:
<?php
class RenderHtmlWidget {
function CRLF () {return "<br>";}
function CR() {return "\r";}
function q ($q) {return("\"" . $q . "\"");}
function Select($name) {
return "<select name=" . $this->q($name) . ">";
}
function addoption($o) {return "<option value=" . $this->q($o) . ">" . $o . "</option>" ;}
function eSelect() {return "</select>";}
function Radio ($Name, $Value){return "<input type="
. $this->q("radio")
. "name="
. $this->q($Name)
. "value="
. $this->q($Value)
. ">";
}
function Submit($value) {return "<input type=" . $this->q("submit") . "value=" . $this->q($value) . ">";}
function TextInput ($name, $value, $size) {
return "<input type=" . $this->q("text") . " name="
. $this->q($name) . " size="
. $this->q($size) . " value="
. $this->q($value) . ">" ;
}
function TextArea($name, $x, $y, $value){return "<textarea name=" . $this->q($name) .
" rows=" . $this->q($y) .
" cols=" . $this->q($x) .
">" . $value .
"</textarea" . ">" . CR();}
function Checkbox ($id, $name, $checked) {return "<input type="
. $this->q("checkbox")
. " name=" . $this->q($name)
. " id=" . $this->q($id)
. (($checked == "yes") ? " checked" : "")
. ">"
. $this->CR();
}
function Label($For, $TheLabel) {return "<label for=" . $this->q($For)
. " >"
. $TheLabel
. "</label>"
. $this->CR();}
function form($name, $method) {return "<form action=" . $this->q($name) . " method=" . $this->q($method) . ">";}
function eForm (){return "</form>" . $this->CR();}
function escript() {return "</script>";}
function script() {return "<script language=\"javascript\">";}
function JumpTo ($Destination) {
//echo script();
return ("window.location=" . $this->q( $Destination));
//echo escript();
}
function Alert ($t) {
return ("alert(" . $this->q($t) . ");");
}
function Button ($Name) {
return "<button type=\"button\">$Name</button>";
}
function Hidden ($Name, $Value) {
return "<hidden name=" . $this->q($Name) . " value=" . $this->q($Value) . ">";
}
} // class
?>
and the incdb.php is as:
<?php
//
// Using arrays as Files
//
// "Table" - the table name - required
// "RecID" - unique indentifier for record - required
// "?" - A Field in Table
// How to create? - done
// How to insert? - done
// How to delete? - done
// How to update? - done
// How to display? - done
// How to make a record ID? - done
// true/false to 1/0 SQLBool()? - done
// nl to out nlout() - done
// out() - done
function nlout($a) {
print($a . "<br>");
}
// SQLbool
function SQLBool ($l) {
$l=($l == true) ? 1 : 0;
return $l;
}
// create a table array from db, else they just need to know the tables
function CrtStruct($TableName) {
return $A;
}
// Create a record ID
function CrtRecID($Size) {
return date_format(date_create(), "YzHis.u");
}
// Display the record
function DispRecord($a) {
foreach($a as $key => $value)
{
$value=str_replace("'","", $value); // get rid of ' for strings
out($key . " : ". $value . "<br>");
}
} // DispRecord
// Connect to db - places connection into Login ["conn"]
function AConnect($Login){
$Login["conn"] = new mysqli($Login["Host"], $Login["User"], $Login["Password"], $Login["DB"]);
return $Login;
} // AConnect()
// Disconnect from db
function ADisconnect($Login) {
$Login["conn"]->close();
$Login["conn"]="closed";
return $Login;
}
// Various ops needed
function Create ($Conn, $Record) {
return AInsert($Conn, $Record);
}
function AInsert($Conn, $Record) {
$cols="";
$values=$cols;
$SQL = "INSERT INTO " . $Record["TableName"] . " (_cols) VALUES (_values);";
//return $SQL;
foreach($Record as $key => $value)
{
// Part of record structure, not record
if ($key == "TableName") continue;
$cols .= $key . ", ";
if(is_string($value))
$values .= "'" . $value . "', ";
else
$values .= $value . ", ";
}
$cols=substr($cols,0, strlen($cols)-2);
$values=substr($values,0,strlen($values)-2);
$SQL = str_replace("_cols", $cols, $SQL);
$SQL = str_replace("_values", $values, $SQL);
return $SQL;
} // AInsert()
function ADelete($Conn, $Record) {
$W="";
$SQL="DELETE FROM " . $Record["TableName"] . " WHERE _where";
foreach($Record as $key => $value) {
if ($key=="RecID") continue;
if ($key=="TableName") continue;
if ($key == "Active") continue;
if(!is_string($value))
$W .= $key . "=" . $value . " ";
else
$W .= $key . "='" . $value . "' ";
}
$SQL=str_replace("_where", $W, $SQL) . ";";
return $SQL;
} // ADelete()
function AUpdate($Conn, $Record, $PrevRecord) {
$SQL = "UPDATE " . $Record["TableName"] . " SET _equals" . " WHERE _where";
$c="";
// _equals section
foreach($Record as $key => $value) {
if($key == "TableName") continue;
if(is_string($value))
$c .= $key . "= '" . $value . "', ";
else
$c .= $key . "=" . $value . ", ";
}
$c=substr($c, 0, strlen($c) - 2);
$SQL=str_replace("_equals", $c, $SQL);
// _where section
$c='';
foreach($PrevRecord as $key => $value) {
if($key == "TableName") continue;
if(is_string($value))
$c .= $key . "= '" . $value . "' and ";
else
$c .= $key . "=" . $value . " and ";
}
$c=substr($c, 0, strlen($c) - 5);
//nlout($c);
$SQL=str_replace("_where", $c, $SQL) . ";";
return $SQL;
} // AUpdate()
function AFind($Conn, $Record) {
} // AFind()
function AForEach($Record, $Where) {
$SQL="select * from " . $Record;
if($Where != "") $SQL .= " where " . $Where;
return $SQL;
} // AForEach
function ShowColumns($Table) {
$sql="SHOW COLUMNS FROM " . $Table;
return $sql;
}
function ApplySQL($Conn, $SQL) {
$Result=$Conn["conn"]->query($SQL);
return $Result;
}
function BeginTransaction($Connection) {
$Connection["conn"]->begin_transaction();
}
function CommitTransaction($Connection) {
$Connection["conn"]->commit();
}
function RollbackTransaction($Connection) {
$Connection["conn"]->rollback();
}
?>
If you want to be alerted to additional files like this, connect up!