Re: Populating textboxes and listboxes from a query result of a database table
| From: | Gerald Timothy Quimpo | Date: | Thu, 26 Dec 2002 20:57:16 +0000 |
| Subject: | Re: Populating textboxes and listboxes from a query result of a database table | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-129381@lists.php.net to get a copy of this message | ||
On Thursday 26 December 2002 06:59 am, Rick Emery wrote:
> Text box:
> print "<INPUT type=text name=myname value=\"$data_value\">\n";
i prefer to use templates. i use my own template code, but there are
good template systems online that you can use,
e.g., http://smarty.php.net is powerful.
in a simple template system, assume that there is a file (the template)
that contains HTML (in my system, i use mixed php and html, with a
special function TemplateVirtual to make things work, but in this
example let's assume it's straight HTML to keep it simple as possible).
we load the file and replace substrings in there with the data. the
convention is:
database fieldname is, for example, dbfield. corresponding input fieldname
is dbfield_name, corresponding input value template (in the HTML, to be
replaced with the actual contents of the dbfield) is {dbfield_value}.
so the HTML might look like:
<INPUT name="dbfield_name" value="{dbfield_value"}>
function templateLoad($fn) /* returns template as one long string */
{
$fp=file($fn,1);
/* insert error handling here, e.g., error might not exist or be
unreadable */
$ret="";
while(list(,$v)=each($tmp))
$ret.=$v; /* or $ret.="$v\n" is nice too but might mangle
multiline HTML or PHP.*/
return $ret;
}
/* convenience/naming-consistency function only. use str_replace
directly if you want (i think the maintainability/consistency is more
valuable than the trivial speed increase */
function templateReplace($templ,$from,$to)
{
$templ=str_replace($from,$to,$templ);
return $templ;
}
to use this:
/* first load data from database or cookies, contents are
in $field1_value, $field2_value, $field3_value, etc, for
the database fields field1, field2, field3 */
$templ=templateLoad("mytemplate.inc");
if(!$templ) /* error handling or something */
die("Could not load template, aborting\n");
$templ=templateReplace($templ,"{field1_value}",$field1_value);
$templ=templateReplace($templ,"{field2_value}",$field2_value);
$templ=templateReplace($templ,"{field3_value}",$field3_value);
print($templ);
template systems have been discussed a lot online. do a google
search on templates, php, separation of display data and
programming logic. templates look complicated when used with
simple/small projects. however, they become much more
useful on larger projects since the look of the project is not
hardcoded in your PHP. you can do multiple looks (skins
or themes) by just switching to a different set of templates.
you can also have graphic designers working on the "look"
of the pages without needing to have them understand PHP.
they can edit just the templates, and as long as they follow
your convention on templates to be replaced and field
naming, the programming group and the graphic designers
can work separately and in parallel.
tiger
--
Gerald Timothy Quimpo tiger*quimpo*org gquimpo*sni-inc.com tiger*sni*ph
Public Key: "gpg --keyserver pgp.mit.edu --recv-keys 672F4C78"
Veritas liberabit vos.
Doveryai no proveryai.