Johan Fourie
Johan Fourie

Reputation: 179

xPath, complicated foreach loop

I am using xPath to get a table , the table's first column has a link to another page, I want that link to link on the name column (first column). Any help would be appreciated.

Here is what I have:

echo '<table>';
foreach($xpath->query('//*[@id="CRConcernedPersonel1_DataGridCRPersonel"]/tr') as $row) { 
 echo '<tr>';
   foreach($xpath->query('td[position() > 0]', $row) as $col) {
    echo '<td>'.trim($col->textContent).'</td>'; 
     foreach($xpath->query('a/@href', $col) as $link) 
        echo '<a href="'.trim($link->textContent).'"</a>'.'Link text'."\n";          
 }
 echo '</tr>';
 }
 echo '</table>';

Here is the output:

http://pastebin.com/5PjCae74

Thanks!

Upvotes: 0

Views: 84

Answers (1)

matthias_h
matthias_h

Reputation: 11416

It should work when you change

 echo '<a href="'.trim($link->textContent).'"</a>'.'Link text'."\n"; 

to

 echo '<a href="'.trim($link->textContent).'">Link text</a>'."\n";

Currently your output reads e.g. like

<a href="CRDetails.aspx?PID=84539&Disp=1&inq=1"</a>Link text  

therefore the link's not working. Changing the line as suggested should result in

<a href="CRDetails.aspx?PID=84539&Disp=1&inq=1">Link text</a>

Upvotes: 1

Related Questions