How can I get data from a table using phpQuery in a foreach loop

2.4k views Asked by At

I would like to get the text on tr 2, td 4, first a, from every article (table), I must not be linking to the text right as when I print_r I don't get anything displayed back.

// table 1
      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table> 

// table 2
      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>

// more tables etc.

      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>  

My phpQuery code has no errors but displays nothing, I'm not sure what I'm doing wrong.

<?php
require "phpQuery/phpQuery-onefile.php";


        // Load betting page
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://example.net/');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html = curl_exec($ch);
        curl_close($ch);

        // Create phpQuery document with returned HTML
        $doc = phpQuery::newDocument($html);

        $articleDate = array();


        $surroundingTheArticles = $doc->find('table.articles');

            foreach( $surroundingTheArticles as $eachArticle)
            { 
             // get table rows    
             $articleDate[]  .= pq($eachArticle)->find('tbody:eq(0) tr:eq(1) td:eq(4)')->text();  // maybe first:a or something - don't know

            }

         print_r($articleDate[1]); 
         // find a way to print all article dates  

?> 
1

There are 1 answers

4
Tuxes3 On BEST ANSWER

this solution requires you to the simple_html_dom. you can get it here

<?php
require_once 'simple_html_dom.php';

$html = '
<table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table> 

      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>


      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>  
';

$html = str_get_html($html);

foreach($html->find('table[class=articles]') as $element){
    $result = $element->find('tr');
    $result = $result[1]->find('td');
    echo($result[4]); echo('<br>');
}


?>