IMB
IMB

Reputation: 15889

Traversing each element

Right now I access each row and cells of a table like this:

$f = phpQuery::newDocumentFile('test.html');

// access row #1
$o = $f['#id tr:eq(0)'];
$d = phpQuery::newDocument($o);

// get cells from row #1
$arr[0]['c1'] = $d['td:eq(0)'];
$arr[0]['c2'] = $d['td:eq(1)'];

// access row #2
$o = $f['#id tr:eq(1)'];
$d = phpQuery::newDocument($o);

// get cells from row #2
$arr[1]['c1'] = $d['td:eq(0)'];
$arr[1]['c2'] = $d['td:eq(1)'];

I was wondering if there's a more efficient way of doing this? Like maybe if there's a way to find out the last index number then I can probably do something like this:

$f = phpQuery::newDocumentFile('test.html');

$last_index = 10;

for ($i = 0; $i <= $last_index; $i++)
{
    $o = $f['#id tr:eq($i)'];
    $d = phpQuery::newDocument($o);     

    $arr[$i]['c1'] = $d['td:eq(0)'];
    $arr[$i]['c2'] = $d['td:eq(1)'];        
}

Anybody knows how to find the last index (total number of rows in a table) ?

Upvotes: 3

Views: 453

Answers (1)

MOleYArd
MOleYArd

Reputation: 1268

You can use the size() method.

$last_index = pq('#id tr')->size() - 1; 

Upvotes: 4

Related Questions