سؤال

I am getting tr of tables and then in loop i want get text of all td fields, look here:

<?
    $lines = $xpath->query("//table[@id='cab_table'] //tr");
       var_dump($lines);// Give me object(DOMNodeList)#11 (1) { ["length"]=> int(6) }


            for( $i = 0; $i < count($lines); $i++) {
                if($i != 0){
                    $tds = $xpath->query('//td', $lines[$i]);
                    $result[$i - 1]['number'] = trim($tds->item(0)->nodeValue);
                    $result[$i - 1]['volume'] = trim($tds->item(1)->nodeValue);
                    $result[$i - 1]['sum'] = trim($tds->item(2)->nodeValue);
                }
            }

            var_dump($result); //Give me NULL
            die();

?>

Why i get NULL?

Now i have:

$lines = $xpath->query("//table[@id='cab_table'] //tr");


            foreach($lines as $line) {
             $tds = $xpath->query('//td', $line);
             $count = $tds->length;

                for($i=0; $i<$count; $i++){

                    echo $tds->item($i)->nodeValue.'<br>';
                    //echo $i.'<br>';


                }

            }

But i want make the next for each tr at loop $result[0] = td[0]; $result[1] = td[1]; $result[2] = td[2]; Can you advise me?

هل كانت مفيدة؟

المحلول 2

foreach($lines as $line) {

                    for($j=0; $j<=3; $j++) {

                     $tds_{$j} = $xpath->query('//td['.$j.']', $line);
                     $tds_{$j} = $xpath->query('//td['.$j.']', $line);
                     $tds_{$j} = $xpath->query('//td['.$j.']', $line);

                     $count = $tds_{$j}->length;


                        for($i=0; $i<$count; $i++){

                            $this->result['number'][] = $tds_{$j}->item($i)->nodeValue;
                            $this->result['volume'][] = $tds_{$j}->item($i)->nodeValue;
                            $this->result['code'][] = $tds_{$j}->item($i)->nodeValue;
                            $this->result['start_date'][] = $startDate;
                            $this->result['end_date'][] = $endDate;

                        }

                    }

                }

نصائح أخرى

->query() returns a DOMNodeList object. It can be count()ed and foreach()ed, but you can't USE it as an array as you are.

$tds = $xpath->query('//td', $lines[$i]);
                             ^^^^^^^^^^---incorrect

Try

$lines = $xpath->query("//table[@id='cab_table'] //tr");
foreach($lines as $line) {
    $tds = $xpath->query('//td', $line);
    ...
}

instead.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top