visualAd wrote:Of course it will take longer. Any query to the database is expensive performance wise. The exact impact on performance will depend on the usage of the site and the number of users.
If at all possible it might be desirable and update it only when it changes or every 15 mins for example.
Yes, it will probably take longer. but we're talking computer time "longer" here. A well-designed, properly indexed set of database tables, accessed via efficient PHP code and queries might mean a few milliseconds more of CPU time than a bunch of hard-coded HTML forms. This could be virtually unnoticeable in comparison to the total internet communication time.
Guess I was bored, but decided to build a simple prototype system:
http://www.charles-reace.com/test/form.php?form=1
At least on my internet connection, to my eye it loads as quickly as any hard-coded page. (Yes, it's a simple example and there are only a few rows in each database table, but I think it shows that the act of executing a couple MySQL queries does not put an undue load and time lag on the process.)
This uses two MySQL tables:
CREATE TABLE `form` (
`form_id` mediumint(9) NOT NULL auto_increment,
`form_name` varchar(255) NOT NULL default '',
`form_description` mediumtext,
PRIMARY KEY (`form_id`)
) TYPE=MyISAM ;
CREATE TABLE `field` (
`field_id` mediumint(8) unsigned NOT NULL auto_increment,
`form_id` mediumint(8) unsigned NOT NULL default '0',
`field_sequence` tinyint(4) NOT NULL default '1',
`field_name` varchar(16) NOT NULL default '',
`field_type` varchar(16) NOT NULL default '',
`field_sub_type` varchar(16) default NULL,
`field_size` tinyint(4) NOT NULL default '20',
`field_maxlength` mediumint(9) NOT NULL default '255',
`field_value` varchar(255) default NULL,
`field_label` varchar(255) default NULL,
PRIMARY KEY (`field_id`)
) TYPE=MyISAM ;
The form page code:
<?php
require_once "class.Mysql.php";
$db = new Mysql();
$db->set_all_connect_params(
'localhost',
'********',
'********',
'test'
);
$db->connect();
$form = (int)$_GET['form'];
$formData = $db->query("SELECT * FROM `form` WHERE `form_id` = $form");
if(count($formData->data) == 0)
{
die("Invalid form number");
}
$fieldData = $db->query("SELECT * FROM `field` WHERE `form_id` = $form ORDER BY `field_sequence`");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title><?php
echo $formData->data[0]['form_name'];
?></title>
<!-- link rel='stylesheet' href='/include/style.css' type='text/css' -->
<style type="text/css">
<!--
-->
</style>
</head>
<body>
<h1><?php
echo $formData->data[0]['form_name'];
?></h1>
<p><?php
echo $formData->data[0]['form_description'];
?></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
foreach($fieldData->data as $field)
{
echo "<p><label for='{$field['field_name']}'>{$field['field_label']}:</label>\n";
echo "<{$field['field_type']} name='{$field['field_name']}' id='{$field['field_name']}'";
if(!empty($field['field_sub_type']))
{
echo " type='{$field['field_sub_type']}'";
}
if(!empty($field['field_size']))
{
echo " size='{$field['field_size']}'";
}
if(!empty($field['field_maxlength']))
{
echo " size='{$field['field_maxlength']}'";
}
echo " value='{$field['field_value']}'";
echo "></p>\n";
}
?>
</form>
</body>
</html>