0

This had me stuck for a while so I thought I would post it. My issue was that my find-> was not working correctly and $product_name was coming up null

    require 'mysql_con.php';
    require 'simple_html_dom.php';

    $html = file_get_html('http://www.xxxx.com/index.php?main_page=index&cPath=23');

    /* foreach($html->find('img') as $element) {
           echo $element->src . '<br>'; 
        }    */

    $find = $html->find('#specialsListing .specialsListBoxContents .indent a');

    $i=0;

    foreach ($find as $test) {

        $link = html_entity_decode($test->href);

        $linkgrab = file_get_html($link);

        $product_name = $linkgrab->find('#productName')->innertext;

        echo $product_name;

        break;

    }
Melbourne2991
  • 11,707
  • 12
  • 44
  • 82

1 Answers1

1

As much as I am posting this for others, I am still unsure why the below is the case and would love if someone could point it out to me.

I worked out the issue was that :

$product_name = $linkgrab->find('#productName')->innertext;

should be

$product_name = $linkgrab->find('#productName', 0)->innertext;

Basically product name has to be numbered. However I am still bewildered because here: $find = $html->find('#specialsListing .specialsListBoxContents .indent a'); the find command worked for me find without need for numbering, also I thought the fact that the selector was an ID as well would have made it unnecessary, if someone could point out what i'm missing that would be great.

Melbourne2991
  • 11,707
  • 12
  • 44
  • 82
  • 2
    the find method returns an array of objects. if you specify the second parameter (`0` in your case) then it returns the object at that key from the array. Its in the documentation http://simplehtmldom.sourceforge.net/manual_api.htm innertext is not set for the array, but is for the individual objects. – Jonathan Kuhn May 02 '13 at 23:41