Forum Moderators: coopster

Message Too Old, No Replies

Best way to retrieve data from database to fill form?

         

NooK

4:37 pm on Nov 1, 2007 (gmt 0)

10+ Year Member



What would be the best way to retrieve data from a database to fill form fields (Including radio buttons and checkboxes) without having to put a php "if" check on every single field in the form code.

Would there be an easy way to loop through all the fields and write each one in it's respective place, I mean, how can I traverse through the page DOM with php and fill such data?

Best Regards

NooK

Demaestro

4:46 pm on Nov 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The best way would be to limit what is returned by the database with "WHERE" clauses rather then parsing out data as you display it.

Is that something that will work?

NooK

5:04 pm on Nov 1, 2007 (gmt 0)

10+ Year Member



Sorry maybe I expressed it wrong. Getting the data from the database is not the problem, I currently limit the data retrieved with a 'WHERE' clause.

The problem is how to put the right data into the right fields without having to do a check in every field for the fieldname and having to hardcode the fieldname in the check.

For example:


<INPUT type="checkbox" name="somename" <?php if("somename" is included in array) echo 'checked'?>>

I am wondering if there is a nicer solution to traverse through certain INPUT fields with a loop, check for their type and do something with them such as checking them or something of the sort.

Best Regards

NooK

[edited by: NooK at 5:07 pm (utc) on Nov. 1, 2007]

Demaestro

5:29 pm on Nov 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmm... not much of a PHP guy so I wouldn't know but the way you describe doesn't seem that bad.

An Ajax implementation would work well though.

PHP_Chimp

5:30 pm on Nov 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you creating the complete form via a script?
i.e.

// assuming $op is the output from the database
$form = '<form action=".." method="...">';
foreach ($op as $k => $v) {
if (is_string($v) === true) {
// assuming all strings are going to be text inputs
$form = '<input type="text" name="'.$k.'" id="'.$k.'" value="'.$v.'" />';
}
elseif (is_bool($v) === true) {
// something with radio/checkboxes
}
...

Or are you entering the values in the html code?

You may be able to use a script to do it all for you. It depends on how complex as to how easy that would be.

NooK

9:39 am on Nov 6, 2007 (gmt 0)

10+ Year Member



Unfortunatelly the design of the form(s) are quite different from each other making a general script nerly impossible to write (Unless I write a code that can handle neary all possible combinatins off css styling and such) so the form is turning out to be one long php code with lot's of echos of html components.

I am trying to come up with a way to turn it into a script to minimize code and make readability easier but it just does not seem possible.

Thanks for the input.

Best Regards

NooK

PHP_Chimp

9:45 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you not write a function to produce a drop down list, one for radio buttons, another for check boxes, etc?
At least if you then had the shells of those functions already scripted you could then supply the data from another function to it, i.e. turn this into OOP?

NooK

9:46 am on Nov 20, 2007 (gmt 0)

10+ Year Member



Due to the use of many css tags such as float and so on in different ones (With the need for an external css function rather than having it in the code) I think it is probably easier to write the htlm tags themselves but I'll have to revise whether that's the best course of action later.

jatar_k

1:39 pm on Nov 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



initialize the vars you are going to use

pull data from db into vars

echo vars in form elements

if the vars are blank then your element will be blank, no need for an if on every field

for radios, checkboxes and selects you need ifs anyway to make them select the proper one. Again you don't need to check whether they are blank or not, it will default to nothing selected/checked if the values aren't set.