Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Database driven <FORM> generation

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Mar 10, 2009 3:48 am    Post subject: Database driven <FORM> generation Reply with quote

A lot of months, I worked on generating HTML FORM with dynamic data from the MySQL database. Here, I found a portion of this tool for you.

It looks into the database for a particular form ID and finds out the elements to display.

Example of use:
{form id=$form_id data=$form_data ... }
Logic: ID is the database's form ID (Primary key)
$form_data is an associated array to display the values while editing some data.
There are many options.
This script is a concept only, here, to assist the public; how to generate the dynamic form.

Code:
<?php
/**
* All these features come from a strategy to make software components highly reusable.
* For the details of these smarty components, please look into http://bimal.org.np/
* Contact for support, to: smarty(at)bimal.org.np
* Write to the original author of these scripts!
*
* This script comes to fulfill a particular purpose aimed by the author,
* and, so, it may not be suitable for general use.
* This comes without any warranties, and fitness of particular use.
*
* @link http://bimal.org.np/
* @version 1.0.0
* @copyright Copyright: 2007-2008 Bimal Poudel
* @author Bimal Poudel <smarty@bimal.org.np>
* @access private
* @package Smarty
*/

/**
* Database driven HTML form generation!
* Use {validate} function to validate it.
*/
function smarty_function_form($params=array(), &$smarty)
{
   # Example
   # {form name='edit_suggesties' id=1 mode='edit' data=$sc submit='Edit' key='id'}

   $form  = '';
   $forms = array();
   $value='';
   $input='';
   $db = new mysql();

   $params['id'] = !empty($params['id'])?$params['id']:0;
   if(!$params['id'])
   {
      return('Supply a form ID to load!');
   }

   $params['key'] = !empty($params['key'])?$params['key']:''; # Measure to go aginst hacking
   $params['key_value'] = !empty($params['key_value'])?$params['key_value']:'';
   $params['form_signature'] = !empty($params['form_signature'])?$params['form_signature']:md5(time());
   
   $params['submit'] = !empty($params['submit'])?$params['submit']:'Submit';
   $params['data'] = !empty($params['data'])?$params['data']:array();
   $params['mode'] = !empty($params['mode'])?$params['mode']:'edit'; # edit | add
   if($params['mode']=='edit')
   {
      if(!$params['data'] && !is_array($params['data'])) { return("Supply valid data for editing in this form!"); }

      if(!$params['key']) { return("Give a unique key of this form entity, eg. a primary key of this tupule!"); }
      if(!isset($params['data'][$params['key']])) { return("Key ID data not found to generate a secure key. Did you forget to assign it?"); }

      $params['key_value'] = $params['data'][$params['key']]; # Not as was given! MUST have some unique data.
      $params['form_signature'] = md5($params['key_value']); # some encrypted value!
   }

   $form_sql="SELECT * FROM admin_forms WHERE form_id={$params['id']};";
   
   $form_details = $db->query_row($form_sql);
   if(!$form_details)
   {
      return("This form (id: {$params['id']}) does not exist.");
   }

   $sql="SELECT * FROM admin_form_fields WHERE form_id={$params['id']} AND is_active='Y' ORDER BY weight ASC, field_id ASC;";
   //echo $sql;
   $db->query($sql);
   while($db->next_record())
   {
   
      $value=isset($params['data'][$db->row_data['field_name']])?$params['data'][$db->row_data['field_name']]:$db->row_data['default_value'];

      #echo("Checking: {$db->row_data['field_type']} : {$db->row_data['default_value_function']} (".(function_exists($db->row_data['default_value_function'])?"Ya":"NO").")<br>");
      
      $value = ($db->row_data['default_value_function'] && function_exists($db->row_data['default_value_function']))?$db->row_data['default_value_function']($value):$value;

      
      if($db->row_data['is_editable']=='Y')
      {
         switch(strtolower($db->row_data['field_type']))
         {
         case 'nothing':
            $input = $value;
            # For comments and other texts only
            break;
         case 'textarea':
            $input="<textarea name='{$db->row_data['field_name']}' id='{$db->row_data['field_name']}' class='{$db->row_data['field_class']}'>{$value}</textarea>";
            break;
         case 'select':
            $input="<select name='{$db->row_data['field_name']}' id='{$db->row_data['field_name']}' class='{$db->row_data['field_class']}'>{$value}</select>";
            break;
         case 'button':
         case 'checkbox':
         case 'file':
         case 'hidden':
         case 'image':
         case 'password':
         case 'radio':
         case 'reset':
         case 'submit':
         case 'text':
            if($db->row_data['field_id']=='61')
               $value='http://';
            $input="<input type='{$db->row_data['field_type']}' name='{$db->row_data['field_name']}' id='{$db->row_data['field_name']}' value='{$value}' class='{$db->row_data['field_class']}' maxlength='{$db->row_data['maxlength']}' >";
            break;
         default:
            $input="<input type='text' name='{$db->row_data['field_name']}' id='{$db->row_data['field_name']}' value='{$value}' class='{$db->row_data['field_class']}' maxlength='{$db->row_data['maxlength']}'>";
         } # switch()
      } # if()
      else
      {
         $input=$value;
      }
      $forms[] = "
<div class=holder>
   <div class='field'>{$db->row_data['field_display_name']}</div>
   <div class='value'>{$input}</div>
</div>
";
   }

   $buttons='';
   if($form_details['is_button']=='Y')
   {
      $buttons = "
<div class=holder>
   <div class='field'>&nbsp;</div>
   <div class='value'>
      <input type='hidden' name='form_signature' value='{$params['form_signature']}'>
      <input type='hidden' name='{$params['key']}' value='{$params['key_value']}'>
      <input type='submit' name='submit' value='{$params['submit']}'>
   </div>
</div>
";
   }

   $form = "
<form name='{$form_details['form_name']}' id='{$form_details['form_name']}' method='{$form_details['method']}' action='{$form_details['action']}' title='{$form_details['title']}' enctype='multipart/form-data'>"
.implode('', $forms)
.$buttons
.'</form>';

   return($form);
} # form()
?>


There are a lot of possibilities.
For example, you can add more validation scripts from http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
And, you can make your database editor yourself to manage the form and its elements.
For time saving, you may just use phpmyadmin only. Because, once you complete your form design, it is less likely to change in the lifespan of your website.

This is actually a form plugin; but since it does not work immediately without reworking; I published it here; not in the plugins section.

Thanks.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP