Jump to content

Should I use PHP to Build a Form?


doubledee

Recommended Posts

Is it overkill to use PHP/MySQL to query my database, retrieve the universe of Questions, and then dynamically build a Form?

 

I have a "Q&A Form" with a whopping 10 Questions.

 

I suppose dynamically building the Form has the advantage that I always have the most up-to-date Questions and Question Text, but them again, if there is a database issue, I have a larger issue...

 

Thoughts?  :shrug:

 

 

Debbie

Link to comment
Share on other sites

Having the form built dynamically allows you to have the ability of updating or editing and adding questions through an Admin interface.  Although you may only have ten questions now, you may someday be saying, "I sure wish I had set this up dynamically."  Having a dynamic form also allows you to show only questions you wish based some other factor, like only show questions unanswered, OR allow ONE update, OR only show question x number of days after initially being answered.  There are many reasons why you might use it and I believe I gave you a pretty good example of making this form recently.

Link to comment
Share on other sites

There is no performance issue at all.  There wouldn't be a performance issue if you had 100x that many questions.  You are talking about a really small amount of data, that will be cached, even without the use of additional caching.  If you're using innodb the data would be in the mysql server data cache 100% of the time.  The execution of the php script itself represents far more overhead.

 

However, these questions are highly academic in most cases.  Unless you have a site where you have a reasonable expectation that you will be encountering more traffic than that server can reasonably handle, there is no reason to consider these questions.  Functionality, flexibility, potential reuse, and data analysis are all far more important considerations.

 

It's also a really bad approach in my experience to start over thinking problems, and trying to hypothesize your bottlenecks when you don't have substantial real-world experience to point to as a basis for your opinions. Even for experienced people, it's dangerous, because you often find that your performance issues are a moving target, and can only be addressed through monitoring and profiling. 

 

In general and as a rule of thumb, one database can front a number of frontend webservers -- 4 or more in my experience, because memory and network bandwidth are the first places you encounter bottlenecks in a web stack.  If you do have database bottlenecks, it is typically because your data set has gotten extremely large, or you are trying to have a mix of unoptimized queries or analytical queries that are being run against a server that is at the same time being asked to perform a lot of transactions.  These are problems that can be mitigated in various ways, long before you get down to philosophical questions about a database.

 

With that said, we really don't have the salient information needed to provide you advise that is all that worthwhile, because the question is not whether or not database use is "overkill".  The real questions are in regards, to what your requirements are, and why you need a Q+A form in the first place.  The important questions are:

 

-What requirement are you trying to fulfill

-Does that requirement involve a Q+A?

-If it does, what will be done with the responses?

-Is this anonymous or will the Q+A be tied to people

-Is it possible there will be further questionnaires in the future?

-What type of reporting is involved?

etc.

 

 

Link to comment
Share on other sites

I'm quite lazy, so this question to me reads like:

 

Should I either:

a) Write it all out and display it as a static page.

or b) Write some code so I can enter the questions and what-not into the database, write the questions into the database, then write more code to display the questions and build the form.

 

I'd pick 'a' in a heartbeat.. But I'd probley also do something weird like put each question into it's own file and include them in the script so that I can quickly just scan for 'question_#.inc.php' in the directory and make any changes to it. Or quickly find it in the main page and comment it out / remove it from the list of questions.

 

It really depends on how often you can see yourself altering that one thing. If you were having different questions every week/month then I'd DB it, and if you did DB it then over time you might have a huge list of maybe 300 questions. Then you could randomly pick 10 from 300 and build a form dynamically from that, so each time the user does it (I hate when I put an 'it' right after an 'it') it appears to have changed and would impress them to no end.

 

Otherwise, write it once, as simple and easy as you can. Implement. Profit.

Link to comment
Share on other sites

Having the form built dynamically allows you to have the ability of updating or editing and adding questions through an Admin interface.  Although you may only have ten questions now, you may someday be saying, "I sure wish I had set this up dynamically."  Having a dynamic form also allows you to show only questions you wish based some other factor, like only show questions unanswered, OR allow ONE update, OR only show question x number of days after initially being answered.  There are many reasons why you might use it and I believe I gave you a pretty good example of making this form recently.

 

You have some good points - as usual - Drummin.

 

Are you volunteering to help me fix this latest problem?!  ;)

 

 

Debbie

 

Link to comment
Share on other sites

gizmola,

 

Nice response!!!  (We need a "Thumbs Up" smiley!!)

 

Ask some good questions and you'll get good answers from me!

 

Okay, here is what I am trying to do.  (BTW, I have this 90% done and working, and am just try automate the last part which is my form...)

 

The website is for people wanting to start their own Small-Business.  One important component is that I am trying to build things that drive "Community" much like PHPFreaks has. 

 

As part of this, people can create "Profiles", and one feature in a Profile is - for now - a series of 10 "Profile Questions", for example...

1.) Why did you decide to start your own business?

 

2.) What advice would you share with others on what to do?

 

3.) What advice would you share with others on what NOT to do?

 

4.) How do you compete against large corporations?

:

:

10.) What Business Structure do you have?

 

 

Here are my 3 key Tables...

 

member

- id

- first_name

 

 

bio_answer

- id

- member_id

- question_id

- answer_text

 

 

bio_question

- id

- question_no

- question_text

 

 

 

Relationships:

member -||-------0< bio_answer ->0--------||- bio_question

 

 

It seemed like an easy thing to do to use code to generate my Q&A section, but upon reflecting on things more, this looks trickier than I thought.

 

Originally just created an INNER JOIN to grab data from my answer and question tables to build and populate the Form.  But the problem is that if there are no Answers then nothing shows up.

 

So then I switched to a LEFT JOIN, but that still won't work when there are no Answers because I lose the MemberID with no Answer records.

 

Here was the query I hoped to use...

SELECT q.question_no, q.question_text, a.question_id, a.answer_text
FROM bio_question AS q
LEFT JOIN bio_answer AS a
ON q.id = a.question_id
WHERE member_id=19
ORDER BY question_no

 

 

So then I was just going to build two queries...  One for the Questions, and one for the Answers.

 

I was going to just store the questionNo and questionText in a questionArray, and then the questionID and answer in an answerArray but this leads me to where I am currently stuck...

 

Life isn't so simple that the questionID and questionNo always match up.  The first is the PK, and the second is for "presentation" only.  I need a way to make sure that questionNo, questionText, questionID, and answerText match up or I have big problems on my Form?!

 

Hope that makes some sense?!

 

 

And back to your earlier questions, gizmola...

 

- Yes, there will be more questions over time.

- Yes, I might want to display only certain Questions

- Yes, Questions (and Answers) pertain to certain Members

- Each Member Profile will have - for now - the same 10 Questions but each Member will obviously have unique Answers

- The 10 Questions are optional so not everyone will Answer them

 

 

Debbie

 

Link to comment
Share on other sites

l0gic,

 

I think it is worth the effort to automate this and use a database.

 

Ironically, my main motivation is that I'd be pretty pissed if I hand-typed in the WRONG Question-sequence in my Form and so people's Questions and Answers don't match up on the backend.

 

I did that to myself?!  :o

 

What would happen if this was a production site?!

 

Better use a database and rely on "Referential Integrity" and all that stuff!!

 

But, as explained above, this is turning out to be trickier than first appeared?!  :(

 

 

Debbie

 

Link to comment
Share on other sites

Currently I have all of my Questions in the "bio_question" table (short for "Biographical Sketch: Questions").  And the Member's responses are stored in the "bio_answer" table.

 

I want a way to dynamically populate my Form with a query where I could do things like picks question_id's: 1, 2, 5, 7, 11, 25, 29 but display them by question_no - which I would change at any time - to display things in the order I want.

 

I'm not sure if I need Arrays to do this?

 

I'm not sure if it would be easier to do this with ONE Query as described above, or if I need a "Questions" query and then need to LINK that to a second "Answers" query?!

 

Hope you follow my requirements, and what I am trying to do?!

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

you can put the questions you pull from your db into an array and have a class function or just a function make a form from the array like I did, so I dont have to keep typing out inputs. Example

<?php
        function CreateForm($title,$action,$method,$info) {
	$form = "<form action=\"{$action}\" method=\"{$method}\" name=\"{$title}\">";
	foreach ($info as $key => $val) {
		//check for submit key
		if ($val[0] == 'submit') {
			$form .= "<input type=\"{$val[0]}\" name=\"{$val[0]}\" value=\"{$val[1]}\"></div>";
		} elseif ($val[0] == 'hidden') {
			$form .= "<div class=\"FormLine\"><input type=\"{$val[0]}\" name=\"{$val[1]}\" value=\"{$val[1]}\">";
		} else {
			//first check if title is update
			if ($title == "Update") {
				//now we can echo their info into the values
				$form .= "<div class=\"FormLine\"><input type=\"{$val[0]}\" name=\"{$val[1]}\" value=\"{$val[2]}\" ";
				$form .= "onfocus=\"if (this.value==this.defaultValue) this.value = ''\"";
				$form .= "onblur=\"if (this.value=='') this.value = this.defaultValue\" />";
				$form .= "</div>";
			} else {
				$form .= "<div class=\"FormLine\"><input type=\"{$val[0]}\" name=\"{$val[1]}\" value=\"{$key}\" ";
				$form .= "onfocus=\"if (this.value==this.defaultValue) this.value = ''\" ";
				$form .= "onblur=\"if (this.value=='') this.value = this.defaultValue\" />";
				$form .= "</div>";
			}
		}
	}
	$form .= "</form>";
	return $form;
}

        $Larray = array (
	"Username" => array("text","LoginUsername"),
	"Password" => array("password","LoginPassword"),
	"Hidden" => array("hidden","SecureHidden"),
	"Login" => array("submit","Login"),
);
$CreateForm = CreateForm("Login","{$_SERVER['PHP_SELF']}","post",$Larray);
echo $CreateForm;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.