-1

hi guys ,, i want all th and corresponding td value
like Beds=>7 , Baths=>8 , House Size=>12000 sq ft

<table cellspacing="0" class='abc'>
  <tbody>
    <tr>
      <th>Beds</th>
      <td>7 bed </td>
      <th>Baths</th>
      <td>8.5 bath </td>
    </tr>
    <tr>
      <th>House Size</th>
      <td>12000 sq ft </td>
      <th>Lot Size</th>
      <td>2.62                        Acres </td>
    </tr>
    <tr>
      <th>Price</th>
      <td>$1,199,000</td>
      <th>Price/sqft</th>
      <td>$100</td>
    </tr>
</table>
4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by mangel.murti
0

While I don't really understand why you got down voted, I will make an assumption it has to do with the lack of effort on your part.

If I was going to approach this, I would probably be looking at the php DOM extension (http://www.php.net/manual/en/book.dom.php).

Using an xPath query (http://www.php.net/manual/en/class.domxpath.php) to return all of my rows <tr></tr>

Then iterate over the collection of rows and checking if each elements nodeName (http://www.php.net/manual/en/class.domnode.php) property is equal to 'th'. If it is I would capture the nodeValue into an array. e.g. $attributes[$element->nodeValue].

Then to get it's value I would use the DOMNode::nextSibling property to get the next adjacent node which should be a td, so check its nodeName is equal to 'td' and then capture its nodeValue as the value of the previous node.

<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("filename.html");
// OR 
//$doc->loadHTML("<html><body>Test<br></body></html>");

$xpath = new DOMXpath( $doc );
$elements = $xpath->query("//tr");

$attributes = array();

//Loop over every row object
foreach( $elements as $row ){

  //Look over each cell in each row
  foreach( $row as $cell ){
    
    //We only care about TH cells so we only look for those specifically
    if( strtoupper($cell->nodeName) == 'TH' ){
      
      //If we find a TH cell then check if we have an adjacent TD cell.
      if( strtoupper($cell->nextSibling->nodeName == 'TD' ){
        
        //Create an array item using the TH's value as the key and the TD's value as the value.  e.g. $attributes['Beds'] = 7
        $attributes[$cell->nodeValue] = $cell->nextSibling->nodeValue;

      }

    }

  }

}

This is just an EXAMPLE and I make to guarantee this is efficient or even works. It is just to illustrate the concept I was trying to explain.

Read the documentation I linked too, looked at the example code, try to implement it in your use case and ask any additional questions you have, i'll do my best to help you.

Edited by mschroeder: n/a

0

what is the source of the data in the table, there may be a different approach that works,

i donn't know why i got down.. rate.... any way

that works for me ....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.