Combining Two Codes for HTML Widgets

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:

No alt text provided for this image

With the data as this:

No alt text provided for this image

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!

要查看或添加评论,请登录

Scott Augé的更多文章

  • Useful Tool: PHP Wiki (mediawiki)

    Useful Tool: PHP Wiki (mediawiki)

    So many times programmers (or other) want a tool that can be a quick document for work out there. Something to answer…

    1 条评论
  • Management Article: What is quality software?

    Management Article: What is quality software?

    (This is from a E-Zine for developing in Progress. URLs and such are out dated.

  • Menus And The Like On PHP Programs

    Menus And The Like On PHP Programs

    Since it is snowy out today and I have experienced four stokes so don't mind the spelling if it you see an error, I…

  • GetValue() for form/url nvp and cookies with PHP

    GetValue() for form/url nvp and cookies with PHP

    When dealing with PHP and you have Webspeed experience (https://progress.com), it might be useful to create a GetValue()…

  • PHP to 4GL/ABL

    PHP to 4GL/ABL

    Added Entry() and NumEntries() to PHP code for a string of comma delimited substrings like the ABL/4GL has. Also…

  • PHP Code and Dictionary Help Tools

    PHP Code and Dictionary Help Tools

    There are a lot of times when a form on a web app wants information already in the database. Such as the example below:…

  • Queue in PHP (Code)

    Queue in PHP (Code)

    Of course if I write about Stacks(1) (LIFO - Last In, First Out), then I got to write about Queues(1) (FIFO - First In,…

  • Simple stack in PHP (Code)

    Simple stack in PHP (Code)

    Here is a class called Stack that implements a stack. A stack is one of the basic data structures one learns in college.

  • Simple stack in PHP/Maria (Dictionary)

    Simple stack in PHP/Maria (Dictionary)

    This is a dictionary used for a simple stack code in an upcoming article. First of all, what is a stack? Well, it is…

  • HTML/JAVASCRIPT Validating Inputs (using Workbench)

    HTML/JAVASCRIPT Validating Inputs (using Workbench)

    Had a question: How do you validate inputs? (I do pay attention to my comments!) A good question (and gives me the…

社区洞察

其他会员也浏览了