Form generator

I’m new to PHP and I’m currently been assigned a project to create a form generator where I will have a PHP script that will read a text file eg. ?svcreq.form? and print a html form. The sample contents of the file is as follow.

formname=form1 label=Server Request Form Header=Server Requirement

type=radio
label=Server
options=hpux,linux,windows
label=HPUX,LINUX,Windows

type=checkbox
label=Optional accessory
options=cddrive,tapedrive
label=CD Drive,Tape Drive

Header=Requestor Info

type=text
label=Name
properties=maxlength|20,length|23

The 1 above is the text file and the PHP script will have to automatically generate the form according to the fields given. I will have to write a PHP script that will read that file. My question is:

  1. Can I write a PHP script that will read the text file line by line? Or are there any other options to do this?
  • I’ve been going through a lot of books, but I just can’t find an example on how to do this.

Please help and your help will highly appreciated. Thank you in advance. :D

To answer your question… YES.

PHP can read a text file line by line. Any programming language should be able to parse a text file as this is a VERY common practice.

Anyway, I digress… have a look at http://us2.php.net/manual/en/function.fgets.php and the other “File System” commands (fopen, fclose, feof, fputs, etc…)

Might I also suggest an alternative to actually handling the file … file()? It simply reads the file, without requiring the programmer to open and close a handle, and returns an array of which each element represents a single line of the file :slight_smile:

Thanks a lot guys! I’ll try that out! Really appreciate that. :D

I think you can find more useful parse_ini_file() function. I think ini file allows to describe the form fields better and array returned by the function simplifies the work on building the form. May be I should use it also :)

And if you are going to use files and parse it yourself, I think coma separated files will be better for that, but that is only by thought.

Sponsor our Newsletter | Privacy Policy | Terms of Service