0

What is the problem with my code? Why it doesn't work?

This is my code that i tried to use:

function extract_data($url){

    // Create DOM from URL
    $html = file_get_html($url);

    // initialize empty array to store the data array from each row
    $theData = array();

    // loop over rows
    foreach($html->find('p.ch_title') as $row) {

        // initialize array to store the cell data from each row
        $rowData = array();
        foreach($row->find('p.ch_spec') as $cell) {

            // push the cell's text to the array
            $rowData[] = $cell->innertext;
        }

        // push the row's data array to the 'big' array
        $theData[] = $rowData;
    }

    return $theData;

}

and this is the html data from the url;

<div class="holder-specificatii">
       <div class="box-specificatie">
          <div class="ch_group">Dimensiuni</div>
          <p class="ch_title">Latime (mm):</p>
          <p class="ch_spec">195</p>
          <p class="ch_title">Inaltime:</p>
          <p class="ch_spec">65</p>
          <p class="ch_title">Diametru (inch):</p>
          <p class="ch_spec">15</p>
          <div class="clear"></div>
       </div>
       <div class="box-specificatie">
          <div class="ch_group">Caracteristici tehnice</div>
          <p class="ch_title">Anotimp:</p>
          <p class="ch_spec">Iarna</p>
          <p class="ch_title">Indice sarcina:</p>
          <p class="ch_spec">91</p>
          <p class="ch_title">Indice viteza:</p>
          <p class="ch_spec">T</p>
          <p class="ch_title">Economie de carburant:</p>
          <p class="ch_spec">C</p>
          <p class="ch_title">Franare pe suprafete umede:</p>
          <p class="ch_spec">C</p>
          <p class="ch_title">Tip vehicul:</p>
          <p class="ch_spec">Turism</p>
          <p class="ch_title">DOT:</p>
          <p class="ch_spec">2014</p>
          <p class="ch_title">Nivel de zgomot (dB):</p>
          <p class="ch_spec">72dB</p>
          <div class="clear"></div>
       </div>
    </div>

The problem is the function that returns an empty array.

3
  • You really should try to narrow this down or be more specific. Commented Nov 1, 2014 at 9:48
  • 1
    Your using the object $table, in the for loop, which has not been defined? That should give you error messages! Commented Nov 1, 2014 at 9:49
  • Yes that was a minor problem. redamed $table to $html but it returns a empty array. Commented Nov 1, 2014 at 9:54

2 Answers 2

1

You're pointing to an undefined object, you should be using $html instead:

function extract_data($url){

    $html = file_get_html($url);
    $theData = array();
    // loop over rows
    foreach($html->find('div.box-specificatie') as $k => $row) { // loop each container
        $temp = array();
        // $main_title = $row->find('div.ch_group', 0)->innertext;
        foreach($row->find('p.ch_title') as $title) { // each title
            $spec = $title->next_sibling()->innertext(); // pair up with spec
            $temp[] = array('title' => $title->innertext, 'spec' => $spec);
        }
        $theData[$k] = $temp; // push inside
        // $theData[$main_title] = $temp; // optionally you can use a main title

    }

    return $theData;
}

echo '<pre>';
print_r(extract_data($url));
Sign up to request clarification or add additional context in comments.

4 Comments

is there a way to add a custom string for each title?
@m3tsys what do you mean custom string? since you already have the title $title->innertext, you can do whatever you need to do
@m3tsys you mean this element? <div class="ch_group">Dimensiuni</div> ?
yes i guess you are right! thank you very much! $title->innertext is more than enough
0

In the first foreach you are doing right, you use the html received from file_get_html but in the nested foreach you are using the returned $row, and it doesn't have the p.ch_spec cause isn't a children of p.ch_title.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.