| |
Only a fortnight to construct, but here it is (that's irony, for the benefit of our American cousins).
The file is listed below. It can be called 3 basic ways:
- In a browser as-is. This gives a page which looks like this (the linked page will not produce anything on this site).
- Selecting "feattypes" & clicking the Edit button gives the following URL:
http://127.0.0.1/form.php?table=feattypes&edit=Edit+Selected+Table
which gives a page which looks like this (only one entry saved in this table, and again clicking the buttons does not do anything on this site). Clicking the New Table button, by the way, loads up a Form identical to the link in (1) above.
- Adding "Voice" to the first Name text-box & clicking the Edit button gives the following URL:
http://127.0.0.1/form.php?table=feattypes&id%5B%5D=0
&name%5B%5D=Voice&id%5B%5D=1&name%5B%5D=Data+Fax&save=Save+Changes
which both adds another row to the feattypes table and also reloads the Form, now with the extra row with an ID of 2.
If you are not used to HTTP conventions, this is how the URLs break down:
http://127.0.0.1/form.php this references the file form.php on my own computer viewed in the browser whilst both Apache & MySQL are running on that computer. 127.0.0.1 is the universal IP address for the localhost.
I could have entered "http://localhost/form.php" rather than the above. This would have been translated by the TCP/IP stack on my Windows 98 computer via a hosts.sam file within the <WINDOWS> folder. If mine was a networked computer, it could have been referenced via the DNS server, which is responsible for translating FQDN (Fully Qualified Domain Names) names - "localhost" in this case - into IP addresses - "127.0.0.1" in this case. The IP address can always be entered direct, as above, which saves a DNS server lookup.
Settings within the Apache configuration file automatically locate a specific folder as the Home folder, and "form.php" is one of the files within that folder.
?table=feattypes the question mark indicates the beginning of a string to be passed to form.php, and this will be translated as variables which can be accessed within the PHP file. The very first variable is called "table" & will be given a value of "feattypes".
The fact that the Form input appears in the URL at all indicates that the form METHOD is "GET" (the default) - see line 42 below. This allows bookmarking & is OK up to about 100 characters of form-data. Above this size a method of "POST" should be used, in which case the form-data is sent by a different method & does not appear within the URL, but can be any size.
&id%5B%5D=0 the ampersand (&) is the character used as a separator between Form fields. The next variable is called "id[]" & will be given a value of "0" (zero). The [] indicate that id is an array variable. Note that:
- The square brackets ([]) are reserved characters in a URL and are therefore replaced by the "%hh" format, in which hh is the hexadecimal value of their ASCII code.
- Form arrays do not have their indices between the brackets. The array index is instead indicated by the order of their appearance within the form-data (zero-based in PHP).
The rest of the URL should be understandable from what has already been written. The form.php listing is below, and explanation of various of it's parts below that. |
| |
- <?php
- include("myConnectFile");
- $cp = "\t\t\t<TD CLASS=cap>Database Forms</TD>\n"; // caption
- $ds = "<INPUT CLASS=dis TYPE='radio' NAME='table' VALUE=";
- $ei = "'id[]' SIZE=5 VALUE=";
- $en = "'name[]' SIZE=40 VALUE=";
- $ey = "<INPUT CLASS=lnk TYPE='text' NAME="; // entry
- // $i - scratch variable used in iterations
- // $id[] - from Form, holds content of PK column
- // $iName - variable to hold table ID
- $lt = "\t\t\t<TD CLASS=t_title"; // left-aligned title row
- // $name[] - from Form, holds content of Name column
- // $nameRev - scratch array to hold $name[] in reverse order
- // $new - from Form, indicates choose a new table if set
- $pt = "\t\t\t<TD CLASS=desc"; // plain text
- // $q - scratch variable to hold SQL query values
- // $r - scratch variable to hold SQL query results
- // $row - scratch variable to hold row values from $r
- $rt = "\t\t\t<TD CLASS=l_title"; // right-aligned title row
- // $s - scratch variable to hold SQL query values
- $sb = "<INPUT TYPE='submit' NAME=";
- $su = $sb."'new' VALUE='Select New Table'> "; // new button
- // $table - from Form, name of table to edit
- // $tblList - from include file; pointer to list of all tables
- // $tbl[] - scratch variable to hold table names
- $td = "</TD>\n";
- $tr = "\t\t</TR><TR>\n"; // a new html table row
- // $v - scratch variable to hold value from arrays
- // $vName - variable to hold table Name
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML LANG=en>
- <HEAD>
- <TITLE>Database Forms</TITLE>
- <LINK REV=Made HREF="mailto:webmaster'at'modem-help.co.uk">
- <LINK REL=StyleSheet HREF="style.css" TYPE="text/css">
- <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
- <META HTTP-EQUIV="Author" CONTENT="Alex Kemp">
- </HEAD>
-
- <BODY>
- <FORM METHOD=GET ACTION="form.php" NAME="typeform">
- <TABLE><TR>
- <?php
- echo("\t\t\t<TD CLASS=h COLSPAN='3'><STRONG>");
-
- if(!$HTTP_GET_VARS or !isset($table) or isset($new)) {
- echo("Choose a Type Table</STRONG>".$td.$cp);
- if($HTTP_GET_VARS and !isset($table)) {
- echo($tr.$rt.">No Table selected".$td);
- }
- echo($tr.$rt."><B>Select</B>".$td);
- echo($lt."><B>Table</B>".$td);
-
- $i=0;
- while($i < mysql_num_rows($tblList)) {
- $tbl[$i] = mysql_tablename($tblList, $i);
- if(ereg("type", $tbl[$i])) {
- echo($tr.$rt.">".$ds."'$tbl[$i]'>".$td);
- echo($pt.">".$tbl[$i].$td);
- }
- $i++;
- }
- echo($tr.$pt." COLSPAN='3'> ".$td);
- echo($rt.">".$su.$sb."'edit' VALUE='Edit Selected Table'>".$td);
- } else {
- $r = mysql_query("SELECT * FROM $table");
- $iName= mysql_field_name($r, 0);
- $vName = mysql_field_name($r, 1);
-
- echo("Edit $table Table</STRONG>".$td.$cp.$tr);
-
- if(isset($save)) {
- $nameRev = array_reverse($name, TRUE);
- foreach($nameRev as $i=>$v) {
- $i=mysql_escape_string($id[$i]);
- $v=mysql_escape_string($v);
- if($v) {
- if($i) {
- $q = "SELECT * FROM $table WHERE $iName=$i";
- $r = "UPDATE $table SET $vName='$v' WHERE $iName=$i";
- $s = "INSERT INTO $table ($iName, $vName)".
"VALUES ($i, '$v');";
- $q = mysql_num_rows(mysql_query($q))?$r:$s;
- } else {
- $q = "INSERT INTO $table SET Name='$v'";
- }
- } elseif($i) {
- $q = "DELETE FROM $table WHERE $iName=$i";
- } else {
- $q=NULL;
- }
- if($q) mysql_query($q);
- }
- }
-
- echo($rt."><B>Select</B>".$td.$lt."><B>Table</B>".$td);
-
- $i=0;
- $q = "SELECT * FROM $table";
- $r = mysql_query($q);
- $iName = mysql_field_name($r, 0);
- while($i < mysql_num_rows($tblList)) {
- $tbl[$i] = mysql_tablename($tblList, $i);
- if(ereg("type", $tbl[$i])) {
- echo($tr.$rt.">".$ds."'$tbl[$i]'");
- if($tbl[$i]==$table) {
- echo(" CHECKED>".$td);
- } else {
- echo(" DISABLED>".$td);
- }
- echo($pt.">");
- if($tbl[$i]==$table) {
- echo("<B>".$tbl[$i]."</B>".$td);
- } else {
- echo($tbl[$i].$td);
- }
- }
- $i++;
- }
- echo($tr.$rt." COLSPAN='3'><B>ID</B>".$td);
- echo($lt."><B>Name</B>".$td);
- echo($tr);
- echo($rt." COLSPAN='2'>ID=0 to add, Name=empty to delete".$td);
- echo($rt.">".$ey.$ei."'0'>".$td);
- echo($pt.">".$ey.$en."''>".$td);
-
- while($row=mysql_fetch_row($r)) {
- while(list ($i, $v) = each($row)) {
- echo($tr.$rt." COLSPAN='3'>".$ey.$ei."'$v'>".$td);
- list ($i, $v) = each($row);
- echo($pt.">".$ey.$en."'$v'>".$td);
- }
- }
-
- echo($tr.$pt." COLSPAN='3'> ".$td);
- echo($rt.">".$su.$sb."'save' VALUE='Save Changes'>".$td);
- }
- mysql_close();
- ?>
- </TR></TABLE>
- </FORM>
- </BODY></HTML>
|
| |
Particulars of (parts of) the listing above:
- The purpose of the file is to provide a means to choose, display, add to, amend & delete rows within the "...Type" tables of the database, using a PHP script file under the Apache HTML server.
- Lines 1-30: examples of PHP script, enclosed by <?php..?> de-limiters
- lines 2, 24: an include file which connects to the database & provides an array of table names. Making the connection inside the include file provides a connection link (a variable - see also 4 Oct). All subsequent connections (eg line 67) use this link by default.
- lines 3-7: fragments of HTML script stored in variables. I'm addicted to keeping files as short as possible, which usually makes them faster but unfortunately also makes the listing more difficult to understand.
- lines 8-10: the "//" begins a line comment. Multi-line comments can be delimited by "/*...*/" (C- syntax). Variables don't need to be declared before use, which can be a source of bugs.
- lines 9, 14: some variables are supplied from form-data.
- Lines 31-43: examples of plain HTML, served up by Apache without amendment.
- Lines 48-65: if no form-data or the Select New Table button is pressed these lines are run.
- lines 55-63: this iterates through the table-list. Line 58 selects each table name that contains "type" & puts an option-select row into the HTML file.
- Lines 67-137: these lines are run once a table has been selected.
- lines 73-94: these lines are run if the "Save" button is pressed. This has possibly cost me the most time - one entire week of which was because of a daft mistake.
- line 74: this reverses the order of the name array (from form-data) but keeps the index-association (TRUE-option). This is necessary to help keep the order of names after changes, especially deletions.
- line 83: known as the ternary operator (?:), the part before the ? - mysql_num_rows(mysql_query($q)) - is a TRUE/FALSE expression, in this case asking if there is a row with a key of $i. If TRUE $r is returned, if FALSE $s is returned.
- lines 98-119 this repeats lines 56-63 but DISABLEs each non-selected table.
- lines 127-133 this puts the rows for the selected table in the Form as sets of text-boxes.
|