Welcome Guest, Not a member yet? Register   Sign In
Parse HTML table data and insert_batch effectively?
#1

[eluser]BernardoLima[/eluser]
I've created a register page, where a user can register as many addresses addresses as he wants, each time he clicks in the button 'Add', it calls a javascript method to insert the inputs data to the row.

So only when he clicks on 'Submit' button, it will insert all the adresses rows from the table to the database.

I'm having trouble to do that, I've been able to parse HTML table data with jQuery:
Code:
$(document).ready(function(){
    $('#output').html('OUTPUT: '+tableToJson('#mytable'));
});

function tableToJson(table){
    var AoA = $(table+' tr').map(function(){
        return [
            $('td',this).map(function(){
                return $(this).text();
            }).get()
        ];
    }).get();
    return JSON.stringify(AoA);
}

Output: [["Juan","23","1990"],["Jonhatan","25","1980"],["Pete","20","1991"]]
But the problem is that it doesn't return the value with keys.

I've also tried to parse the table, to be an array of dictionaries, with keys and values, but it didn't make easier to insert to the database, because it when the json was decoded, the format of the keys : values wasn't right for PHP.

JsFiddle example: http://jsfiddle.net/j4x7pr2w/3/

I guess the simplest,most efficient solution would be insert_batch but without specifying the fields, like:
INSERT INTO TABLE VALUES(myArray);

It would be good to do that without having to manipulate data a lot, like having to create an array and then format this array with another method, it would be something too extensive.

Does anyone have a better idea to keep table rows registries on a variable and only send it when Ajax post is called?

Maybe an approach with only two steps, generating the array and then inserting the array.

Thank you very much.


Messages In This Thread
Parse HTML table data and insert_batch effectively? - by El Forum - 08-19-2014, 07:09 PM
Parse HTML table data and insert_batch effectively? - by El Forum - 08-19-2014, 08:12 PM
Parse HTML table data and insert_batch effectively? - by El Forum - 08-20-2014, 05:08 AM
Parse HTML table data and insert_batch effectively? - by El Forum - 08-20-2014, 08:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB