Forum Moderators: coopster

Message Too Old, No Replies

Most Efficient Way to Load / Store Form Vars

         

Frank_Rizzo

3:40 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a form with about 100+ options on it. I'm looking for the most efficient way to store and retrieve the form variables from a mysql database.

The form consists mainly of checkboxes and drop down lists with a few radio buttons.

What I need to do is to determine if the form was loaded as new (a blank sheet), or was called via a 'previous saved form' option.

That bit is easy but it looks like it's going to be a huge job to code the form for loading preset values. I'm wondering if there is an easier way than this:


#
# Load form vars from db or set default for new page
#
if($_GET['savedform_id'] > 0) { #saved form was requested, select the form and it's vars from the database
$result = mysql_query("select * from savedforms where id=$_GET['savedform_id']");
while ($row = @mysql_fetch_assoc($result)) {
$fruit = $row["FRUIT"];
$colour = $row["COLOUR"];
$country = $row["COUNTRY"];
$weight_min = $row["WEIGHT_MIN"];
$weight_max = $row["WEIGHT_MAX"];
........
} else {
$fruit = 'Apples';
$colour = 'Red';
$country = 'Norway';
$weight_min = 100;
$weight_max = 500;
}
....

That shouldn't take to long to code. I just need to create 100+ lines extracting fields from the tables.

The next part is the form itself. And this is where it gets messy.

The standard form would look like this:


<SELECT NAME="fruit">
<OPTION VALUE="A">Apples</OPTION>
<OPTION VALUE="B">Bananas</OPTION>
<OPTION VALUE="P">Pears</OPTION>
<OPTION VALUE="M">Melons</OPTION>

But as I need to recall what a user may have saved earlier I need to set which option should be selected. The only way I can see of doing this is:


print '<SELECT NAME="fruit">';
print '<OPTION VALUE="Apples"' . ($fruit == 'Apples'? " SELECTED" : "") . '>Apples</OPTION>';
print '<OPTION VALUE="Bananas"' . ($fruit == 'Bananas'? " SELECTED" : "") . '>Bananas</OPTION>';
print '<OPTION VALUE="Pears"' . ($fruit == 'Pears'? " SELECTED" : "") . '>Pears</OPTION>';
print '<OPTION VALUE="Melons"' . ($fruit == 'Melons'? " SELECTED" : "") . '>Melons</OPTION>';

Before I embark on this humdrum task of modifying the form is there an easier way?

jatar_k

5:33 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not that I can think of, you have it all right

sorry ;)

Frank_Rizzo

8:18 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thought as much! Just counted the number of fields with Firefox Page Info = 168 :(

I have a much better idea for the database storage option.

Originally I was going to create 168 extra fields: FRUIT, COUNTRY, WEIGHT_MIN, WEIGHT_MAX etc.

But not all the fields will change from the defaults all of the time.

e.g. Of the 168 fields some users may choose to change one field, some may choose to change any 25 out of the 168 etc.

This means that creating 168 database fields is inefficient.

As the fields are ultimately going to be converted into php vars it would make sense to directly store the php var commands for only those which changed:

database field FORM_VARS (blob or large char) ->
$fruit='Bananas';
$country='Brazil';
$weight_min=200';


#set defaults for all
$fruit = 'Apples';
$country = 'Norway';
.
.
.
.
#
# Load form vars from db
#
if($_GET['savedform_id'] > 0) { #saved form was requested, select the form and it's vars from the database
$result = mysql_query("select FORM_VARS from savedforms where id=$_GET['savedform_id']");
$row = @mysql_fetch_row($result);
$form_vars = $row[0];
.....
}

All I need to do now is to work out how to incorporate the form_vars into that script. This would be exactly the same process as including a file which has vars in it:

e.g. include('form_vars.php')

#form_vars.php
<?php
$fruit = 'Bananas';
$country = 'Brazil';
?>

Saving the form vars should be easy. All I have to do is to test if each var is not the default and only save it if it is:

#
#routine to save vars into database
#
$form_vars = '';
if($fruit <> 'Apples') {
$form_vars .= '$fruit="' . $fruit . '";'
}
if($weight_min <> 100) {
$form_vars .= '$weight_min=' . $weight_min . ';'
}
....

The question is. How do I get php to read the retrieve vars from the database?

coopster

8:39 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you considered serializing [php.net] the form data instead?

Frank_Rizzo

9:06 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was thinking more on the lines of variable variables but serializing an array is an option.

I could build up the vars as an array, store them, retrieve the array, unserialize and extract.

Moosetick

9:19 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



This will work if your data is all yes/no or check/uncheck type of data and you don't need to compare it with other people's data.

Convert each answer into a binary bit and compile that into one binary value. That way you will have a single 100 bit value to save and retrieve.

jatar_k

9:48 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how many users are we talking about?

this made me think
>> Of the 168 fields some users may choose to change one field, some may choose to change any 25 out of the 168 etc.

maybe just an array in a text file? only store the changes

or like coop said, serialize the changed data.

from the sounds of it the data will most of the time be default.

variable variables

yeah, that would work too but might be more work originally and not save much process/time/code.

something I have done in the past, though not exactly like this but might work

if you used an array as below for the user choices ($myselection)

then initialized arrays for each select, i know they are not all selects but this is just an example

<? 
$myselection = array('Fruit'=>'Bananas','Colour'=>'Yellow');
$select1 = array('Fruit','Apples','Bananas','Pears','Melons');
$select2 = array('City','Oslo','Vancouver','Boston','London');
$select3 = array('Colour','Red','Blue','Green','Yellow');
echo '<form name="mysuperform" action="mysuperscript.php">';
$outer = 1;
$control = '';
while (isset(${'select' . $outer})) {
foreach(${'select' . $outer} as $key => $value) {
if ($key == 0) {
echo '<p><select name="',$value,"\">\n";
$control = $value;
} else {
echo '<option value="',$value,'"';
if (isset($myselection[$control]) && $myselection[$control] == $value) echo ' selected';
echo '>',$value,"</option>\n";
}
}
echo "</select>\n\n";
$outer++;
}
echo '</form>';
?>

jatar_k

9:58 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I hate editing code posts, it sometimes breaks them and it looks so pretty ;)

with this method is adding and removing things from the form elements is simple

your elements all have to be very logic based, for these selects the name of the form element is the first array element

you could also group your selects, radios, text on your page and using the same naming convention (radio1, text1 etc) output them all in similar small loops.

just a thought, it was fun to write either way, thanks ;)

Frank_Rizzo

12:33 am on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Moosetick that's something I've done in the past - works great with single on /off's but not feasible here.

jatar_k I like the style of looping through each. I use the same procedure for building dd-mm-yyyy option lists. I think that cool snippet came from here previously.

---

Here's a great time saver for populating the database:

insert .... $_POST

!

Simple. The $_POST var has all 168 form options already in one big array. I all have to do now is to convert to a string, serialize or whatever.

Obviously it's not as efficient as just storing the changes in the database - i.e. all 168 vars are stored in each record rather than those which deviate from the default. But this will make the code a lot simpler. I guess I could gzipcompress to make the strings smaller.

jatar_k

12:53 am on Mar 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> came from here previously

hehe, was it this one (sorry for OT)
Dynamic Date Selector [webmasterworld.com] msg 5

I like them ;)

Frank_Rizzo

3:06 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that's the one. A nifty script that.

One problem with $_POST is that it only populates the array with check boxes which are checked.

That should be fine in this case but it does mean that I'll have to initialise every variable at the start and then just pull the changes from the database (if requested).

I decided to not use serialize but to just create a csv type string using this method:


$string = '';
foreach ($_POST as $key => $value) {
$string .= $key . '¦' . $value . '~';
}

$string = addslashes($string);

...INSERT $string...

This creates a long string like
fruit¦Apples~country¦Norway~weight_min¦100~ .....

I find this method a bit faster and creates a smaller text string to start with.

At the other end I just extract each pair and build variables from it.