Question

Here is the code:

<?php 

    require_once 'simple_html_dom.php';
    $url = "website.com";

    $data = file_get_html($url);


    $search = $data->find("div.box_rezult");

    if($data->innertext!="" and $search ){
        foreach($search as $box_rezult){

            // Get lottery name
            echo $box_rezult->find("td.lottery_name", 0)->plaintext;
            echo "<br/>";

            // Get date
            echo $box_rezult->find("td.lottery_text", 0)->plaintext;
            echo "<br/>";

            // get the table of all results
            $table = $box_rezult->find("table.last-draw-table", 0);

            // get winning numbers
            echo $table->children(2)->children(0)->plaintext;
            echo "<br/>";

            if (!isset( $table->children(2)->children(1)->plaintext )) {
            }
            else

            echo $table->children(2)->children(1)->plaintext, '<br/>';
            echo "<br/>";

        }
    }



    $data->clear();
    unset($data);

    ?>

The result is something like this:

Headline
Data
Text1
Text2

Headline
Data
Text1
Text2

And so on. What I need is the unique div id's to each result in order to apply the custom styling. Something like this:

<div id="header_1" class="headings">
Headline
Data
Text1
Text2

Headline Data Text1 Text2

I'm using this:

if($data->innertext!="" and $search ){
    foreach($search as $box_rezult){
    $index = 0;

        // Get lottery name
        echo  "<div id=\"header_".$index."\" class=\"headings\">".$box_rezult->find("td.lottery_name", 0)->plaintext."</div>";
        echo "<br/>";
....
....

$index++;

But everytime I get div id="header**_0**" class="headings">. Correct the code please.

And the additional question is how to hide children(1) text of the 4th result (if exist).

Thanks!

Était-ce utile?

La solution

You want to enumerate $search. Remove your current increment and initialization of $index and use

foreach($search as $index => $box_rezult){

Autres conseils

If i look at your last codeblock i see that "$index++;" is outside foreach.

Place "$index = 0;" outside foreach and "$index++;" inside foreach to increment it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top