0

I need your help! I'm banging my head against a brick wall since two days without success. I'm new with PHP (I started several months ago).

I use the class Simple HTML Dom Parser to get some information's from a web page:

1°) a number after "library-". Example: 199459

couple with a series of links:

2°) "/index.php/link-to-get-878220"

I try to get those information's in tab, that way:

    Array
    (
    [Movies] => Array
    (
    [199459] => /index.php/link-to-get-878219
    [199459] => /index.php/link-to-get-878220
    [198210] => /index.php/link-to-get-878452
    [198210] => /index.php/link-to-get-878453
    [198210] => /index.php/link-to-get-878454
    [198210] => /index.php/link-to-get-878455
    )
    )

As you can see, each number (ie:198210) is the key of several values (all the values contained in the div with id="library-??????" have the same key)

Problem:

  • I got some waste (ie: movies_1) when I try to get the number.
  • I do not succeed to parallel numbers and links in the array (that's why I didn't added an example in the code)

English is not my native language so do not hesitate to ask insights on some words I used!

my code:

include('simple_html_dom.php');$movies = array();
$html = new simple_html_dom();
$html->load_file("pt.html");

foreach($html->find('div.libraries div[id*=library-]') as $library):
$movies['idMovie'][] = str_replace('library-', '', $library->id);
foreach($html->find('.list-movies tr') as $links):
$movies['links'][] = $links->find('.nom a',0)->href;
endforeach;
endforeach;

And the HTML I try to get information's from:

<div class="libraries library_movie-tabs red tabs">
<div class="libraries library_element-tabs red tabs">
<div id="library-199459" class="tabs-content">
<div class="inner">
<p class="info"><strong>Library 1:</strong>Fantasy Movies</p>
</div>

<div class="inner">
<table width="100%" class="library-table list-movies">
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Conan"
href= "/index.php/link-to-get-878219"></td>
</tr>

<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Lord-Of-The-Ring" 
href= "/index.php/link-to-get-878220"></td>
</tr>
</table>
</div>
<div id="library-movies_1" class="library-movies">
...
</div> 
</div>

<div id="library-198210" class="tabs-content">
<div class="inner">
<p class="info"><strong>Library 2 :</strong>S-F Movies</p>
</div>

<div class="inner">
<table width="100%" class="library-table list-movies">
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Tron" 
href= "/index.php/link-to-get-878452"></td>
</tr>
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Starwars" 
href= "/index.php/link-to-get-878453"></td>
</tr>
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Star-Trek" 
href= "/index.php/link-to-get-878454"></td>
</tr>
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Predator" 
href= "/index.php/link-to-get-878455"></td>
</tr>
</table>
</div>
<div id="library-movies_1" class="library-movies">
...
</div> 
</div>
</div>
</div>

1 Answer 1

0

Your problem is that in the inner foreach you want to iterate $library, mot $html

foreach($html->find('.tabs-content') as $library){
  $item = array('links' => array());
  $key = str_replace('library-', '', $library->id);
  foreach($library->find('a') as $a){
    $item['links'][] = $a->href;
  }
  $movies[$key] = $item;
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.