FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Dynamic form generation
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Dynamic form generation [message #177681 is a reply to message #177642] Fri, 13 April 2012 09:39 Go to previous messageGo to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma:
Senior Member
On 4/12/2012 5:48 AM, Charles wrote:
> I'm working on a database application, and I'd like to write a function that takes the table structure from a MySQL database table and generates a fresh data entry form from it each time someone browses to it.
>
> That way I can have four functions that serve the four purposes of data entry, view/query, edit, and delete, instead of 4 scripts for each table (the application has 64 tables so far) that have to be editted every time the table changes.
>
> Is it possible to use PHP to generate data entry forms on the fly?
>
> Does anyone have any recommendation for a reference resource or some sample code to study for this?
>

Hi Charles,

I have written such a contraption for Postgres 8.1 a few years ago.
I don't think I am allowed to send you the code, but I can describe it
(translated to mysql).

The approach is really simple:

http://dev.mysql.com/doc/refman/5.0/en/getting-information.html

1) Figure out how you can get a listing of all your tables in the db.
('show tables' in mysql, I think)
2) List them all on the screen, and hyperlink to a details page.
3) Look up all the columns in use for that given table.
('describe table' I think)
4) Make a mapping for each datatype you want to support.
Don't try to support everything: BLOB makes no sense to edit in a html form.

For example:
$myMapping = [
"CHAR(x)" => "TS",
"VARCHAR(x)" => "TS",
"TEXT" => "TB",
"TINYINT" => "I",
etc...
];

TS meaning Texttype and small (Use INPUT type="text" in form)
TB meaning Texttype and big (Use textarea in form)
I meaning int.

You need a bit smarter code than plain matching, since CHAR(30) must match.
You can use a regular expression.

Now you can map every column to a suitable HTML form element.

You can also use that mapping to do the right casting before inserting
when you receive a new row.
for example: use mysql_realescape() on TS and TB.
Cast received integers to int.
Cast floats/doubles to... etc.

It isn't too difficult to write if you use a simple mapping array and
only support datatypes that make sense. I wrote a simple one in 1 day.
And it is fun. :-)

Hope this helps you going.

Good luck.
Regards,
Erwin Moller


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Data injection problems
Next Topic: Do you want to develop PHP for the Web and make money
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 00:58:19 GMT 2024

Total time taken to generate the page: 0.06829 seconds