PURPOSE OF THE SCRIPT
|
|
What the class does
|
« ... the first is characterized by freedom and disorder, and the second by subjection and disorder, the third by subjection and order (...)
Ages of prolonged uncertainty, while they are compatible with the highest degree of saintliness in a few, are inimical to the prosaic every-day virtues of respectable citizens.
There seems no use in thrift, when tomorrow all your savings may be dissipated; no advantage in honesty, when the man towards whom you practice it is pretty sure to swindle you; no point in steadfast adherence to a cause, when no cause is important or has a chance of stable victory; no argument in favour of truthfulness, when only supple tergiversation makes the preservation of life and fortune possible. The man whose virtue has no source except a purely terrestrial prudence will, in such a world, become an adventurer if he has the courage, and, if not, will seek obscurity as a timid time-server.
(...) The purpose of life was rather to escape misfortune than to achieve any positive good. "Metaphysics sink into the background, and ethics, now individual, become of the first importance. Philosophy is no longer the pillar of fire going before a few intrepid seekers of truth: it is rather an ambulance following in the wake of the struggle for existence and picking up the weak and the wounded" (C.F. Angus)»
[Bertrand Russel, History Of Western Philosophy] |
This script has been conceived as a php interface that would allow you to interact quickly between php grabbed SQL queries and your need to print the results.
The idea is that when you have questioned a SQL or mySQL database (for instance by a mySQL PHP Database Manager script) and you have fetched the returned query by a php method such as say mysql_fetch_array, you may want a set of php methods able to print out to the browsers those fields in a set of specified form tags, such as text fields, or textareas, or checkboxes or whatever other form tag.
In this fashion the fields would carry as their value the query value (though with this script you can also instruct to print as the form tag values the query fetched field name rather than the query fetched field value) and an optional label that describes the form field nature.
In this way you can rapidly show to the browser the fetched query in a suitable html format, or print fields quickly so that the user can, if he/she wants, update them. That is, you have an html output format immediately available both for reading purposes and for updating purposes namely also for submitting it again.
The class does not take care of producing the submit buttons or the form tags so by and large your php code might look like, say:
<form action="blah blah">
//php class manipulations go here
print("<input type='submit' etc...>");
</form>
THE CODES
|
|
The codes and how to initialize your class instances.
|
The name of this class is (of course case sensitive):
queryToForm
To initialize it, pick a variable name (our placeholder is foo) and do:
$foo=new queryToForm();
You can pass to that one argument, either zero or 1, which sets the value assigned to an internal variable named doPrint whose role will be discussed in the next section. First your codes.
OVERVIEW OF THE MAIN METHODS
|
|
The class methods more likely to be used
|
Here is an overview of the main methods. In the following section after this one I will also discuss the few additional methods that perform less common tasks and that yet will help you to produce even more customized or sophisticated layout formats.
These methods all share the same characteristic, namely if the class property named doPrint is set to its default value of 1, they will directly print on the browser the fetched input array fields into specified html form input fields, otherwise if doPrint is set to zero the would just store (which they do also if doPrint is set to 1, actually) the produced html form input fields into a class variable array named lastOutput.
By the way, to change the value of the doPrint class variable you can use the method named setPrint and pass to it as its only argument either zero or 1, which gets then assigned to doPrint:
$foo->setPrint(0);/*inhibits immediate printing*/
$foo->setPrint(1);/*restores immediate printing*/
All the methods, each tailored to produce a specified type of form tag to host the input fetched array (textareas, text fields, checkboxes, radio buttons, drop down menus, hidden fields...), allow for a significant amount of arguments to be passed. Normally only the first one is mandatory (the input array).
For each method the arguments are documented below. Study the type of argument values combinations that may best suit your needs, and once devised the argument signatures for each method that best meets your requirement, you can just go on using it consistently throughout all your applications that take avail of this php class interface.
Another characteristic that all these methods share is that the default value of their second argument is set to 1.
This signifies that while scanning the input array, those entries which are indexed after a numerical index (key) will be skipped, thus grabbing only those key/value pairs of the input array where the key element is a string and not a number . This feature has been set in this way because the methods are ideally suited to work with php methods that pass to them as their input array a query fetched by its query table field names/values pairs, whereas the formers are typically strings.
Yet if you want to change this behaviour, set the second argument of these methods to zero, and the methods will honour it and print also numerically indexed entries. It is up to you to decide what behaviour best suits your needs. You rule, I provide the options you can pick from.
|
MAIN METHODS OVERVIEW
|
$foo->text
A Domenico Ghirlandaio painting
|
Outputs either printing and storing into $foo-> lastOutput (or just storing in it without printing if $foo->doPrint is zero) the input array or fetched query into a set of html form
<input type=" text " name="" value="">
tags.
The returned array (therefore also stored into $foo->lastOutput) is an array of two entries, the first entry an array of its own of the generated form tags, the second entry an array of its own again holding the SQL field name of each corresponding entry in the first entry; in short pseudo code:
$foo->lastOutput[0]=/*array of tags*/
$foo->lastOutput[1]=/*array of labels*/
If set to print, it prints the generated form fields inside a table for a more ordered layout.
In any of such form tag its assigned name property value will be the name of the SQL fetched field, and its value property will obviously be set to the related SQL fetched field value.
The arguments of this method, described in their passing order, are:
- $resource: the fetched result of a query, or an array (better if associative, namely whose indexes are strings).
- $skipNumeric: either 1 (default) or zero. If 1, it skips, while parsing the input resource, all those entries which are numerical, thus avoiding duplicates in case the resource was passed as the result of a mysql_fetch_array php call, which returns query pairs fieldName/values also duplicated as pairs numericIndex/value; in fact, from php.net:
«In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
(...) An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. »
- $containerTagClass: the optional css class name of the table tag in which it prints by default.
- $keyTagClass: the optional css class name of the td tag in which it prints by default the name of the printed field (the value is obviously printed inside the text field) as such name appeared in the SQL fetched query row.
- $valueTagClass: the optional css class name of the td tag in which it prints by default the generated html text field.
- $events: optional a string to hold your html javascript event handlers. Defaults to this String:
onChange="" onFocus="" onBlur=""
An example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$foo->text($foofoo, 1/*skips numerical indexes*/);
The above prints:
See the html code (beware a few properties may appear without surrounding quotes, though they have them; it is an issue of the javascript code used to show this html source snippet, not a shortcoming of the html itself as the php class method prints it.)
|
$foo->select
A Theodore Chasseriau painting
|
Outputs either printing and storing into $foo-> lastOutput (or just storing in it without printing if $foo->doPrint is zero) the input array or fetched query into a set of html form
<option value=""> ... </option>;
tags.
The returned array (therefore also stored into $foo->lastOutput) is an array of two entries, the first entry an array of its own of the generated form tags, the second entry an array of its own again holding the SQL field name of each corresponding entry in the first entry; in short pseudo code:
$foo->lastOutput[0]=/*array of tags*/
$foo->lastOutput[1]=/*array of labels*/
If set to print, it prints the generated select drop down menu with its options.
The leading and trailing <select> tags will be included only if the method was called and set to perform the printing by doPrint. So if the result is just returned and not printed, $foo->lastOutput will store the html tags only as a set of <option> tags exclusive of the leading and trailing <select> tag pair.
The arguments of this method, described in their passing order, are:
- $resource: the fetched result of a query, or an array (better if associative, namely whose indexes are strings).
- $skipNumeric: either 1 (default) or zero. If 1, it skips, while parsing the input resource, all those entries which are numerical, thus avoiding duplicates in case the resource was passed as the result of a mysql_fetch_array php call, which returns query pairs fieldName/values also duplicated as pairs numericIndex/value; in fact, from php.net:
«In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
(...) An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. »
- $optionValueIsValue: determines what must be assigned as the value property of each option tag.
If passed as zero, it assigns the query fetched value.
If passed as 1 (default), it assigns the query fetched field name.
If passed as number 2, it assigns both the key field name and its value in the following string syntax:
"fieldName=value"
that is, as a whole string whose two elements are separated by an = sign.
- $optionTextIsValue: determines what must be assigned as the textual property of each option tag.
If zero, assigns the SQL field name.
If 1 (default), assigns the SQL field value.
- $multiple: if passed as 1, sets the outermost select tag to enable multiple rather than single selections.
- $size: defaults to 1, assigns the size property to the select tag.
- $name: defaults to "select1", assigns the name property to the select tag.
- $containerTagClass: the optional css class to give to the select wrapper tag.
- $events: optional a string to hold your html javascript event handlers. Defaults to this String:
onChange="" onFocus="" onBlur=""
An example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$foo->select($foofoo, 1/*skips numerical indexes*/, 2);
The above prints:
See the html code (beware a few properties may appear without surrounding quotes, though they have them; it is an issue of the javascript code used to show this html source snippet, not a shortcoming of the html itself as the php class method prints it.)
|
$foo->checkbox
A James Whistler painting
|
Outputs either printing and storing into $foo-> lastOutput (or just storing in it without printing if $foo->doPrint is zero) the input array or fetched query into a set of html form
<input type=" checkbox " name="" value="">
tags.
The returned array (therefore also stored into $foo->lastOutput) is an array of two entries, the first entry an array of its own of the generated form tags, the second entry an array of its own again holding the SQL field name of each corresponding entry in the first entry; in short pseudo code:
$foo->lastOutput[0]=/*array of tags*/
$foo->lastOutput[1]=/*array of labels*/
If set to print, it prints the generated form fields inside a table for a more ordered layout.
The arguments of this method, described in their passing order, are:
- $resource: the fetched result of a query, or an array (better if associative, namely whose indexes are strings).
- $skipNumeric: either 1 (default) or zero. If 1, it skips, while parsing the input resource, all those entries which are numerical, thus avoiding duplicates in case the resource was passed as the result of a mysql_fetch_array php call, which returns query pairs fieldName/values also duplicated as pairs numericIndex/value; in fact, from php.net:
«In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
(...) An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. »
- $valueIsValue: assigns the value property to the checkbox.
If set as 1 (default), the value is the query field value.
If passed as zero, it will be the query field name.
If passed as number 2, it will be a string in the shape:
"fieldName=value"
that is, as a whole string whose two elements are separated by an = sign.
- $giveSharedName: assigns the name property to the checkboxes.
If passed as a string, will assign such string to all the checkboxes, thus they will all share the same name (for javascript manipulation of these type of objects, see the function called checkCheckboxes).
If passed as a number, if it is zero (default) will assign the query field name, if passed as 1 the query field value.
- $textIs: assigns the text that should describe (label) the checkbox.
If passed as zero (default), assigns no label. If passed as 1 assigns the query field name, if passed as 2 assigns the query field value.
- $onClick: optional on click javascript event handler for the generated form field.
- $containerTagClass: the optional css class name to give to the table in case the method is instructed to print.
- $keyTagClass: the optional css class name to give to the td tag that hosts the label in case the method is instructed to print.
- $valueTagClass: the optional css class name to give to the td tag that hosts the checkbox in case the method is instructed to print.
An example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$foo->checkbox($foofoo, 1/*skips numerical indexes*/, 1, 0, 1);
The above prints:
See the html code (beware a few properties may appear without surrounding quotes, though they have them; it is an issue of the javascript code used to show this html source snippet, not a shortcoming of the html itself as the php class method prints it.)
|
$foo->radio
A Matthias Grunewald painting
|
Outputs either printing and storing into $foo-> lastOutput (or just storing in it without printing if $foo->doPrint is zero) the input array or fetched query into a set of html form
<input type=" radio " name="" value="">
tags.
The returned array (therefore also stored into $foo->lastOutput) is an array of two entries, the first entry an array of its own of the generated form tags, the second entry an array of its own again holding the SQL field name of each corresponding entry in the first entry; in short pseudo code:
$foo->lastOutput[0]=/*array of tags*/
$foo->lastOutput[1]=/*array of labels*/
If set to print, it prints the generated form fields inside a table for a more ordered layout.
The arguments of this method, described in their passing order, are:
- $resource: the fetched result of a query, or an array (better if associative, namely whose indexes are strings).
- $skipNumeric: either 1 (default) or zero. If 1, it skips, while parsing the input resource, all those entries which are numerical, thus avoiding duplicates in case the resource was passed as the result of a mysql_fetch_array php call, which returns query pairs fieldName/values also duplicated as pairs numericIndex/value; in fact, from php.net:
«In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
(...) An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. »
- $valueIsValue: assigns the value property to the checkbox.
If set as 1 (default), the value is the query field value.
If passed as zero, it will be the query field name.
If passed as number 2, it will be a string in the shape:
"fieldName=value"
that is, as a whole string whose two elements are separated by an = sign.
- $giveSharedName: assigns the name property to the radio buttons.
If passed as a string (to which it defaults, and such default name is "radioSet"), will assign such string to all the radio buttons, thus they will all share the same name. The reason it is set by default to assign a shared name is that radio buttons are nearly always set to share a common name property value.
If passed as a number, if it is zero it will assign the query field name, if passed as 1 the query field value.
- $textIs: assigns the text that should describe (label) the checkbox.
If passed as zero (default), assigns no label. If passed as 1 assigns the query field name, if passed as 2 assigns the query field value.
- $onClick: optional on click javascript event handler for the generated form field.
- $containerTagClass: the optional css class name to give to the table in case the method is instructed to print.
- $keyTagClass: the optional css class name to give to the td tag that hosts the label in case the method is instructed to print.
- $valueTagClass: the optional css class name to give to the td tag that hosts the radio button in case the method is instructed to print.
An example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$foo->radio($foofoo, 1/*skips numerical indexes*/, 2, "radiosName", 1);
The above prints:
See the html code (beware a few properties may appear without surrounding quotes, though they have them; it is an issue of the javascript code used to show this html source snippet, not a shortcoming of the html itself as the php class method prints it.)
|
$foo->textarea
A John Melhuish Strudwick painting
|
Outputs either printing and storing into $foo-> lastOutput (or just storing in it without printing if $foo->doPrint is zero) the input array or fetched query into a set of html form
<textarea></textarea>
tags.
The returned array (therefore also stored into $foo->lastOutput) is an array of two entries, the first entry an array of its own of the generated form tags, the second entry an array of its own again holding the SQL field name of each corresponding entry in the first entry; in short pseudo code:
$foo->lastOutput[0]=/*array of tags*/
$foo->lastOutput[1]=/*array of labels*/
If set to print, it prints the generated form fields inside a table for a more ordered layout.
In any of such form tag its assigned name property value will be the name of the SQL fetched field, and its value property will obviously be set to the related SQL fetched field value.
The arguments of this method, described in their passing order, are:
- $resource: the fetched result of a query, or an array (better if associative, namely whose indexes are strings).
- $skipNumeric: either 1 (default) or zero. If 1, it skips, while parsing the input resource, all those entries which are numerical, thus avoiding duplicates in case the resource was passed as the result of a mysql_fetch_array php call, which returns query pairs fieldName/values also duplicated as pairs numericIndex/value; in fact, from php.net:
«In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
(...) An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. »
- $textareaCharacteristics: a string that includes a few of the basic properties of every textarea. Its default is the following string:
'cols="20" rows="3" wrap="hard"'
- $textareaClass: the optional css class name to assign to the textarea tag.
- $containerTagClass: the optional css class name of the table tag in which it prints by default.
- $keyTagClass: the optional css class name of the td tag in which it prints by default the name of the printed field (the value is obviously printed inside the textarea) as such name appeared in the SQL fetched query row.
- $valueTagClass: the optional css class name of the td tag in which it prints by default the generated html form field (the textarea that is).
- $events: optional a string to hold your html javascript event handlers. Defaults to this String:
onChange="" onFocus="" onBlur=""
An example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$foo->textarea($foofoo, 1/*skips numerical indexes*/);
The above prints:
| username: | |
| password: | |
| email: | |
See the html code (beware a few properties may appear without surrounding quotes, though they have them; it is an issue of the javascript code used to show this html source snippet, not a shortcoming of the html itself as the php class method prints it.)
|
$foo->hidden
|
Outputs either printing and storing into $foo-> lastOutput (or just storing in it without printing if $foo->doPrint is zero) the input array or fetched query into a set of html form
<input type=" hidden " name="" value="">
tags.
The returned array (therefore also stored into $foo->lastOutput) is an array of two entries, the first entry an array of its own of the generated form tags, the second entry an array of its own again holding the SQL field name of each corresponding entry in the first entry; in short pseudo code:
$foo->lastOutput[0]=/*array of tags*/
$foo->lastOutput[1]=/*array of labels*/
If set to print, it prints the generated form fields inside a table for a more ordered layout.
In any of such form tag its assigned name property value will be the name of the SQL fetched field, and its value property will obviously be set to the related SQL fetched field value.
The arguments of this method, described in their passing order, are just:
- $resource: the fetched result of a query, or an array (better if associative, namely whose indexes are strings).
- $skipNumeric: either 1 (default) or zero. If 1, it skips, while parsing the input resource, all those entries which are numerical, thus avoiding duplicates in case the resource was passed as the result of a mysql_fetch_array php call, which returns query pairs fieldName/values also duplicated as pairs numericIndex/value; in fact, from php.net:
«In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
(...) An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value. »
|
OVERVIEW OF THE SPECIAL METHODS
|
|
The class more sophisticated methods
|
There are a few methods that you may want to use to get more customed types of printings. Here they are.
This methods actually allows you to remove specific field names from an incoming associative array. Yet the right way to exclude field names that you do not want to be printed down would be to make sure that your query already excludes them from the produced fetched array (that is, you do not include those fields names as wanted in the SQL query itself).
Yet I provide you with this additional method that can do that all the same onto an already produced and fetched array.
It takes these arguments
- inputArray: the array you want to exclude a few field names from.
- removeThese_array: an array whose each value is the string reproducing (case sensitive) the field name you want to delete from the input array.
Example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$cleaned=$foo->cleanKeys($foofoo, array("password"), 0, 0);
$test=$foo->text($cleaned, 0);
That would remove from the input array the field whose name is "password". You could add more field names to the remove these array, of course. This is just an example.
- skipNumeric: if set to 1 it skips numerically indexed fields in the input array. If you pass as removeThese_array an empty array, and you set skipNumeric to 1, the returned output will just exclude the numerically indexed fields.
- skipEmptyValues: if set to 1, it skips all the input array entries which return boolean false: that is, empty strings with not even one whitespace, null, false, zero, and in php the string "0" too.
The method returns a copy of the cleaned up array - does not affect the original.
This method is a conceptual integration to cleanKeys: you pass as first argument the usual associative array, arguably the query fetched row, and as a second argument an array whose each entry is the string name of a field you want to keep - whereas in the previous method ( cleanKeys) this latter argument was meant to array the string names you wanted to exclude.
You can use then the method that best suits your needs.
Keep in mind that if cleanKeys2 would include a numerical index in the second argument array, if such number is present in the first passed array, such numerical field will be kept also if the argument skipNumeric is set to 1.
It takes these arguments :
- inputArray: the array you want to exclude a few field names from.
- keepThese_array: an array whose each value is the string reproducing (case sensitive) the field name you want to keep from the input array.
Example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm();
$cleaned=$foo->cleanKeys2($foofoo, array("password","11"), 0, 0);
$test=$foo->text($cleaned, 0);
That would remove from the input array the field whose name is not "password" or "11", namely would keep these latter. You could add more field names, of course. This is just an example.
- skipNumeric: if set to 1 it skips numerically indexed fields in the input array, unless a specific number is mentioned in the second argument array.
If you pass as keepThese_array an empty array, all fields would be erased from the returned output (that is, the function assumes then you wanted to keep none) and nothing gets printed!
- skipEmptyValues: if set to 1, it skips all the input array entries which return boolean false: that is, empty strings with not even one whitespace, null, false, zero, and in php the string "0" too.
The method returns a copy of the cleaned up array - does not affect the original.
As you may remember all methods designed to print, also return an array of two entries, the first one an array with the form fields, the second entry an array carrying the corresponding label names you may want to use in order to describe onto a browser what the printed form fields are.
You can therefore imagine a situation as follows: you have initialized your class and you have set it not to print:
$foo=new queryToForm(0);
You may remember that your methods now, when called, just return the produced output - without printing if instructed not to print, but would return anyway:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm(0);
$test=$foo->text($foofoo, 0);
What is now stored in $test is an array of two entries:
Entry zero is the set of the form html tags already populated, the second entry is the label names you may want to use: see it and analyze it to understand better what is going on:
Array
(
[0] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[1] => Array
(
[0] => username
[1] => password
[2] => email
[3] => 11
)
)
Now, it is possible you may want to produce out of them one single array that for each entry of the former array concatenates before it its corresponding label. To do that you use outputJoin.
It takes in the following arguments:
- output: it is the output produced (returned that is) by one of the main printing class methods.
- betweenRecords: when the records get rejoined, you may want to include some html between them: defaults to an empty space, inserted before each label; that is, the concatenated record would be like:
betweenRecords + label + tag.
- header: whatever html you want to put as header of each rejoined recordset of label + tag. Defaults to a <strong> tag:
header + betweenRecords + label + tag
- betweenNameAndValue: whatever html you may want to put between the label and the form tag. Defaults to ": ":
header + betweenRecords + label + betweenNameAndValue + tag
- footer: whatever html you want to put as ending footer of each rejoined recordset of label+form field. Defaults to a </strong> tag:
header + betweenRecords + label + betweenNameAndValue + tag + footer
|
REJOINED RECORDSET APPEARANCE
|
header |
between Records |
label |
between Name And Value |
tag |
footer |
<strong> |
" " |
field name |
": " |
form tag |
</strong> |
|
<strong> password :
<input type="text" name="password" value="A big secret" etc> </strong>
|
Thus:
$rejoined=$foo->outputJoin($test);
Produces and returns an array whose each entry concatenates the sets as follows:
Array
(
[0] => username:
[1] => password:
[2] => email:
[3] => 11:
)
Only if you have used outputJoin, you can take avail of this method upon the array outputJoin has returned, in order to format your form fields output and labels stored in that returned array in a way that you prefer, that is without the default table and determining, for instance, how many items you want in a line before breaking the line with a carriage return.
If the class variable doPrint is set to 1, the method prints, otherwise it just returns the, arguably long, generated formatted string.
Its argument are as follows:
- output: the output of a rejoined array (see outputJoin)
- itemsPerRow: a Number: how many items do you want per line? One form field, maybe 2 or 1 or 4... Possible exceeding or lacking fields are managed just fine.
- header: whatever html you want before the whole thing (defaults to a starting <form> tag)
- footer: whatever html you want after the whole thing (defaults to a closing </form> tag)
- startRow: whatever html you want before each line.
- endRow: whatever html you want after each line.
Example:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$foo=new queryToForm(0);/*suspend printing*/
$test=$foo->text($foofoo, 0);
$rejoined=$foo->outputJoin($test);
$format=$foo->formatOutput($rejoined, 3, "HEADERS<br>", "FOOTERS", "Start Row: ", " End Row<br>");
If you now print $format, it would prints:
HEADERS Start Row: username: password: email: End Row Start Row: 11: End Row FOOTERS
You may want to rewrite your labels so that rather than including the names of the SQL query
fields, they carry more user friendly names.
Top achieve this you pass to this method two arguments. The first is the portion of the returned array by some of the print main methods which holds the labels, namely the returned entry that belongs to index [ 1].
The second argument can't be but an array you yourself wrote, and must be an associative one whose each index is the exact match of an old label, and whose pointed value is the new label, say for instance:
$newOnes=array("password"=>"Insert Your Password", "email"=>"Write Your Email", "username"=>"Choose A Username");
There is actually a third optional argument, must be either zero (its default) or 1. If zero, it means that the first argument array is a numerically indexed array and thus the search for the swaps is performed on its values. If set to 1 it flags the first array is an associative one (its keys are not numbers but strings, that is) and the search for the swaps would be performed on its keys. Just an additional power feature.
Follow these guidelines:
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");
$newOnes=array("password"=>"Insert Your Password", "email"=>"Write Your Email", "username"=>"Choose A Username");
$foo=new queryToForm(0);
$test=$foo->text($foofoo, 0);
$foo->resetLabels($test[1], $newOnes);
$rejoined=$foo->outputJoin($test);
$format=$foo->formatOutput($rejoined, 2, "HEADERS<br>", "FOOTERS", "Start Row: ", " End Row<br>");
print $format;
The above requires a bit of work by you, but first consider that if you now print what was stored ( returned) into $format
that nicely prints:
HEADERS Start Row: Choose A Username: Insert Your Password: End Row Start Row: Write Your Email: 11: End Row FOOTERS
In the apparently "convoluted" procedure above is implied a remarkable feature which I'd like you to posit and possibly appreciate before hastening to the conclusion that the procedure just appears (at least relatively) complex.
And this is that, having that procedure separated the two moments the one in which we assign to the form tags their relative name properties and the other in which we assign the descriptive textual labels, the former (the tag name properties) can still carry as values the very same names of the original SQL query key index fields, while at the same time allowing for their modification into a more user friendly format (the latter namely the labels) at a second time that does not interfere with the former assignments.
The importance of this being in place is this: if the form tags still carry the original SQL query key fields, they can still be submitted via http again to another php script keeping intact the consistency of the involved php applications and of the php interfaces which normally and traditionally want to handle the SQL table keys by the same names assigned to their HTML form tag embodiements.
This is why I have separated the two moments, the one that assigns names/values pairs to the form tags, the other that rewrites the copy of such names as stored in the second branch of the arrays returned by the printing methods in order to produce more user friendly textual labels.
And, by the way, nothing would prevent you from using resetLabels on your very same feched query associative array so to change the names of its keys, prior to whatever other call to the other class methods, in case you don't want to assign as html tag names the same names of your SQL table fields.
To do this, since a fetched query array is an associative (String indexed) array and not a numerically indexed array, pass to resetLabels a third argument and set it to number 1:
$foo=new queryToForm();
$foofoo=array("username"=>"John Foo", "password"=>"A big secret", "email"=>"", "11"=>"key is numerical");/*Just the usual fake fetched query row*/
$newOnes=array("password"=>"Insert Your Password", "email"=>"Write Your Email", "username"=>"Choose A Username");/*new labels associative array; by the way: in whatever order!*/
$foo->resetLabels($foofoo, $newOnes, 1);
print_r($foofoo);
The above would rewrite $foofoo as:
Array
(
[Choose A Username] => John Foo
[Insert Your Password] => A big secret
[Write Your Email] =>
[11] => key is numerical
)
Note that the email field has been throughout our examples set intentionally to empty, just to show that the methods work also with empty sets.
resetLabels affects the original of the first passed argument.
You may want to consider writing a custom template of the 5 or 6 php "commands" above, to use such template including it in all your applications that resort to this class feature. It's server side stuff and therefore write once, print everywhere, and thenceforth worth a tidbit of your cooperation or efforts while you use the cooperation and efforts that I put at your disposal here quite gladly and quite for free.
And if eventually you do think you can do better, and that it is badly necessary, well I have at least provided you with a rather good draft, isn't it?
And now don't be mistaken: answer no.
|