1

I have a html table

<table>
<tr>
<th>Type</th>
<th>Text</th>
<th>Time</th>
<th>Notification Time</th>
</tr>
<tr>
<td>Lab1</td>
<td>Some Text</td>
<td>Day of Week</td>
<td>Monday, Wednessday</td>
</tr>
<tr>
<td>Lab2</td>
<td>Some Text</td>
<td>Day of Week</td>
<td>Tuesday, Wednessday</td>
</tr>
</table>

Now I want to use those values of <td> and form an array in php or jquery. Any idea how to do it in the most simplest way?

5
  • jquery or php? 2 completely different things... what do you want to do with the array Commented Aug 7, 2015 at 9:56
  • also: how do you create that table? Is it coming from a php script or a jquery one or... Commented Aug 7, 2015 at 9:57
  • I am using PHP and need those values to save into database. I've populated that table using php and jquery. Commented Aug 7, 2015 at 10:06
  • Show please how you`ve populated it. It would be more easier to work with initial data i guess. Commented Aug 7, 2015 at 10:13
  • @ArnabHore, please check my high performance jQuery solution Commented Aug 7, 2015 at 10:13

3 Answers 3

2

In jquery:

var tdCollection = $("table").find("td");
var array = [];
$.each(tdCollection, function(key, el){    
     array.push($(el).text());     
});
console.log(array);

fiddle: http://jsfiddle.net/qqdwct7h/

But it would be better to set an id attribute for the table, beacuse using $(table) isn`t best way to select a certain table.

Sign up to request clarification or add additional context in comments.

6 Comments

Save the jsfiddle and then copy the url
The use of $.each is extremely slower compared to a reversed loop. Please check jsperf.com ;) double usage of var in a method is also considered as a code smell.
"Double usag of var" depends on code style.
@LazarevAlexandr, well that coding style makes the JavaScript file heavier. Congrats :)
Ok. The weight of a js file matters when it gets to production. When getting there, js file should pass different "uglify" processes to make code less "heavier", and such things are concerned there. Meanwhile when a js file is in development environment codestyle is more important than it`s weight.
|
2

Check this jQuery traversing and Ajax sample :)

<script type="text/javascript">
    var tableRows = $("table tr"),
        currentRow,
        tableData = [];

    for (var i = tableRows.length; i--;) {
        currentRow = $(tableRows[i]);

        tableData.push({
            field1: $(":nth-child(1)", currentRow),
            field2: $(":nth-child(2)", currentRow),
            field3: $(":nth-child(3)", currentRow),
            field4: $(":nth-child(4)", currentRow)
        });
    }

    //Sample Test to verify if data is fetched
    console.log(tableData);

    $.ajax({
        url: "sample-ajax-handler.php",
        type: "POST",
        data: tableData,
        success: function (e) {
            //do what you want here :)
        }
    });
</script>

1 Comment

"field1" index is repeated.
0

I wrote a fiddle that allows to generate an array based on the column:

http://jsfiddle.net/5vfm6k6q/2/

Works like this:

var parser = new ArrayParser(),
    result = parser.getArray('#table', 'Type');  // table selector, column

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.