Reputation: 284
First of all, hello and thank you for reading my question. This may seem like a slightly weird combination of requirements - and I am already aware that the problem is not beginning in the most efficient manner from the start, however this is a a prameter I am unable to change - at least at this time.
I have an html form that contains two html tables. There is a range of JS calculation going on on each of these tables to calculate values in various readonly input fields from other (user) input fields. The final total of the first table is used after a calculation as the first value of the second table. The second table remains static and so has a defined number of inputs. This is not part of the problem but does rely on data in the first table.
The first table has 6 input fields (3 text, one select and two textboxes) on one physical table row. There is also a button on this row which clones the entire row and then appends it below, adding an incremental value to each of the newly generated 6 fields. This allows the user to add as many rows as they require, each with the same 6 fields. The first (default) field has values such as:
"texta_1", "textb_1", "textc_1", "textboxa_1" textboxb_1", "select_1"
Any additional JS generated rows will follow the naming format to be:
"texta_2", "textb_2", "textc_2", "textboxa_2" textboxb_2", "select_2"
"texta_3", "textb_3", "textc_3", "textboxa_3" textboxb_3", "select_3"
Etc
There is no defined limit to how far/many this could be expanded to, although for the purposes this is intended to be used for mean that the small group who will use it and have access to it, will not need to input any more than 10 rows at a time.
Everything up to this point functions perfectly. The issue is that once this form is posted, I need to collect ALL the data from the above mentioned table and be able to redisplay it to the user before it is saved into a mysql database.
I need to know how, within php5 (5.3.3 being used on a LAMP stack - Debian/Apache2/MySQL 5.1.66/PHP 5.3.3) to collect all of the inputs for each of the 6 fields, and throw them into both a List and an Array. I'm not really sure which function I will require (my php abilities are both not top form and what there is of it, is 3 years out of date). The DB has correct provision for allowing all data in one area to be saved together - that is to say texta_1, texta_2, texta_3, texta_5 etc... will all want to be saved together in the DB in a column that is, lets say named "db_texta".
I'll also need to throw data intended for/within each of these 6 DB columns into a foreach loop for displaying before submission to the DB and of course for later on when extracting it again from the DB.
It might be important to point out that this part of the process is a step within an overall larger process. It is the second part of data collection and will not be the last part. It's preceded by the user entering in their personal details and this is submitted already into the DB before the above mentioned step commences, so there is already a DB entry in place awaiting this info, and once this is submitted to the DB there will be a third step which is required to allow the user to upload scanned images to associate with this entry. However I don't believe any of that is critical to this immediate problem. If I'm wrong about that, please just let me know!
How do I take what is posted within the initial form and list it nicely for review in correct format, and then glomp the unknown number of each of the 6 fields together to save into the DB, while also permitting me to extract and reuse the data later on?
I shall try to paste in there the PHP code I have at the moment. Please note it is not fully complete, but you can see the intention and where it is going - or trying to go!
CODE:
I've pasted the code at a paste service as it appears it slightly exceeds the maximum character space in here. I feel the overall code is useful to understand the full desired functionality. I'm trying to cover all the bases here before I post my first question. Apologies if this is the "not to do" thing here, though. :) The full code is visible at: http://pastie.org/8391018#
Just to reiterate - the use of texta_[ ] style naming BREAKS the JS form functionality so this question is looking for an ALTERNATIVE method to achieve the requirements.
Replies and time appreciated, thank you!
Upvotes: 0
Views: 378
Reputation: 7438
You can use this to start if you already know the prefix of each field (texta, textb, textc, etc...). This will loop until no more field are found. Please note that if you have let's say texta_12
and texta_14
but no texta13
, well it will break at 12 and stop there.
# This array will contain the data.
$Res = array();
# This variable will contain our current count. Start at 0.
$i=0;
# We loop to the infinite !
while(TRUE){
# +1
$i++;
# Are we done ?
if(isset($_POST['texta_'.$i]) === FALSE){
break;
}
# We create our sub-array
$Res[$i] = array(
'text_a' => $_POST['texta_'.$i], // Already tested...
'text_b' => (isset($_POST['textb_'.$i]) === TRUE ? $_POST['textb_'.$i] : ''),
'text_c' => (isset($_POST['textc_'.$i]) === TRUE ? $_POST['textc_'.$i] : ''),
'text_box_a' => (isset($_POST['textboxa_'.$i]) === TRUE ? $_POST['textboxa_'.$i] : ''),
'text_box_b' => (isset($_POST['textboxb_'.$i]) === TRUE ? $_POST['textboxb_'.$i] : ''),
'select' => (isset($_POST['select_'.$i]) === TRUE ? $_POST['select_'.$i] : '')
);
}
# We print our results
print_r($Res);
This wasn't tested but the concept will work.
EDIT:
Ok - I think this will suit your need :
# This array will contain the data.
$Res = array(
'text_a' => array(),
'text_b' => array(),
'text_c' => array(),
'text_box_a' => array(),
'text_box_b' => array(),
'select' => array()
);
# This variable will contain our current count. Start at 0.
$i=0;
# We loop to the infinite !
while(TRUE){
# +1
$i++;
# Are we done ?
if(isset($_POST['texta_'.$i]) === FALSE){
break;
}
# We create our sub-array
$Res['text_a'][$i] = $_POST['texta_'.$i];
$Res['text_b'][$i] = (isset($_POST['textb_'.$i]) === TRUE ? $_POST['textb_'.$i] : '');
$Res['text_c'][$i] = (isset($_POST['textc_'.$i]) === TRUE ? $_POST['textc_'.$i] : '');
$Res['text_box_a'][$i] = (isset($_POST['textboxa_'.$i]) === TRUE ? $_POST['textboxa_'.$i] : '');
$Res['text_box_b'][$i] = (isset($_POST['textboxb_'.$i]) === TRUE ? $_POST['textboxb_'.$i] : '');
$Res['select'][$i] = (isset($_POST['select_'.$i]) === TRUE ? $_POST['select_'.$i] : '');
}
# Here you can use a delimiter to merge them into one string
$TextA = implode('^', $Res['text_a']);
# However, if you just want to store the data into one field in a more complex but more stable way, I suggest:
# JSON
$TextA = json_encode($Res['text_a']);
# Serialization
$TextA = serialize($Res['text_a']);
# Now, $TextA is one big string that you can save into one MySQL field (make sure the field type is big enough for the length of the string).
# You can read back the json using json_decode or unserialize
# We print our results
print_r($Res);
MySQL Storage Field Length Limit:
TINYTEXT 256 bytes
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB
In your case, 1 caracter will take 1 byte.
Documentations:
while(TRUE)
mean in PHP?Upvotes: 1
Reputation: 324630
Just use name="texta[]"
- this will automatically create an array $_POST['texta'][0..n]
for you, with all the elements right there.
Upvotes: 1