0

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!

Cassandra
  • 284
  • 4
  • 18
  • Poor you... Super long explanation for a tiny answer... well I prefer that over a too short question with no sense. – David Bélanger Oct 09 '13 at 23:43
  • Thanks David. However trying to be clear and concise does mean investing a little time and I've read enough of forum posts to know how it feels to be on the end where you wonder what the heck they are trying to do/ask! Seems I still missed a critical point too - the form field name value structures **cannot** be altered to support [] as this totally destroys the JS functionality. – Cassandra Oct 09 '13 at 23:51

2 Answers2

1

Just use name="texta[]" - this will automatically create an array $_POST['texta'][0..n] for you, with all the elements right there.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Sorry perhaps I did not clarify - I cannot modify the name system as this will heavily break the JS functionality that is already in place. The name/id structure it not able to be changed to do this... sadly. Hence needing the alternative methodology. – Cassandra Oct 09 '13 at 23:46
  • you can store count of rows in hidden field and then used like: $cnt = $_POST['cnt']; for ($i=0; $i<$cnt; $i++) $_POST['texta_' . $i] - this is field value – Iłya Bursov Oct 09 '13 at 23:55
  • Thank you Ilya, however I had some problems making this work. It sort of worked in some instances but results tended to be not accurately ordered. – Cassandra Oct 10 '13 at 03:46
1

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:

Community
  • 1
  • 1
David Bélanger
  • 7,400
  • 4
  • 37
  • 55
  • Thanks David - this does indeed arrange each table row into an array quite successfully and I can use this method for displaying the data before feeding it into the DB. However I actually need to know how to arrange it differently so that ALL textb_ items are in the one array, not one of each across a line in one array. Remember I'm limited for storage in the DB to 6 fields, so textb_1 + textb_2 + textb_3 etc all need to be in one array together where I will use explode() for manipulation on them later. Can you help (again)? :) – Cassandra Oct 10 '13 at 02:56
  • Hi again David. Huge thank you!! I've had a bit of a play with your code in my page and while I think it may start the ball rolling on a few new questions like how to format this data in different situations, it's certainly doing exactly what I need it to for the moment. Your help is very much appreciated! Thanks again. :) – Cassandra Oct 10 '13 at 22:22
  • @Cassandra My pleasure. Just be sure to mark your question has answered by click the little check mark next to my post. Thank you. – David Bélanger Oct 10 '13 at 23:12
  • Would it be an imposition if I asked you for a touch more help on how to manipulate the array's you've helped me with? I'm interested in knowing how to loop through both the first and second versions you've given me, so that I can do something like display the posted inputs into a resulting table and display the results on the screen. But I'm having great failures at trying to work it out on my own. – Cassandra Oct 14 '13 at 14:07
  • @Cassandra I do not understand your question "nowing how to loop through both the first and second versions" ? The loop I do is an infinite loop. It will never stop to loop until the scrip say to break it (when `$_POST[field_x]` doesn't exist). – David Bélanger Oct 14 '13 at 21:24
  • It's ok, I clearly didn't express my last query too well. I was experiencing syntatical issues with getting stuff to work, and was hoping for an example to work from. However I have not solved that problem on my own and it is functioning perfectly. I've actually used both versions of array creation in different areas of the process, for different results. So once again thank you! :) – Cassandra Oct 16 '13 at 04:18