0

This is a code i used to prase a data through html dom php and echo a data in table form all data came in perfect table form. but i want to show only 5 out of all data echo in table.at last i used table to echo a content or data i want only to echo 5 data out of all showing out but i am not getting any idea to code this.. any one can help me to do this?

<?php
include('simple_html_dom.php');

$html         = file_get_html('http://www.discoverhongkong.com/us/see-do/events-festivals/events-calendar/index.jsp');
$eventRowData = array();

class eventRecord
{
    public $dates;
    public $eventDescription;
}
;
foreach ($html->find('#event_table tr') as $eventItem) {
    $eventDateArray = array();
    $oneEventData   = new eventRecord;

    foreach ($eventItem->find('.date') as $eventDate) {
        $oneDateData = array();
        $dom         = new domDocument('1.0', 'utf-8');
        $dom->loadHTML($eventDate);

        $dom->preserveWhiteSpace = true;
        $hTwo                    = $dom->getElementsByTagName('div')->item(0);
        foreach ($hTwo->childNodes as $dateElement) {
            array_push($oneDateData, $dateElement->nodeValue);

        }
        //print_r ($oneDateData);
        array_push($eventDateArray, $oneDateData);

    }

    //
    $oneEventData->dates = $eventDateArray;

    $eventData = null;
    foreach ($eventItem->find('.event_name') as $eventName) {

        $dom = new domDocument('1.0', 'utf-8');

        $dom->loadHTML($eventName);

        $dom->preserveWhiteSpace = true;
        $baseLink                = "http://www.discoverhongkong.com";

        $img = $dom->getElementsByTagName('a')->item(0);

        $relativeLink = $img->attributes->getNamedItem("href")->value;
        $eventLink    = $baseLink . $relativeLink;

        $eventData = '<strong><a href="' . $eventLink . '" title="' . $img->textContent . '">' . $img->textContent . '</a></strong><br />';

        $oneEventData->eventDescription = $eventData;
        //echo $oneEventData->eventDescription;
    }

    array_push($eventRowData, $oneEventData);
}
$arr = array_values($eventRowData);
//Print_r ($eventRowData);
//echo "----------";
//echo $arr[0]->eventDescription;
//echo "----------";
echo '</br>';




echo '<h2>Events In HongKong</h2>';
echo '<table class="table-bordered">';
echo '<tr>';
echo '<th class="abc">EVENT NAME</th>';
echo '<th>START DATE</th>';
echo '<th>END DATE</th>';
echo '</tr>';

foreach ($arr as $xyz) {
    //print_r ($xyz);
    echo '<tr>';
    echo '<td>';
    echo ($xyz->eventDescription);
    echo '</td>';


    //$mydates = array_values($xyz->dates);
    //print_r ($mydates);
    foreach ($xyz->dates as $datevalue) {
        //echo '<tr>';
        echo '<td>';
        foreach ($datevalue as $datevalueitem) {
            echo $datevalueitem;
            echo '/';
        }

        echo '</td>';

    }
    echo '</tr>';



}

echo '</table>';
?>
5
  • 1
    Do you mean you only want to display the first 5 elements in the loop, regardless of how many there are? Keep a count in an integer variable. When it reaches 5, break out of the loop. Commented Sep 22, 2017 at 11:22
  • yes i want to do same can you help me with a code or example i am new here.. Commented Sep 22, 2017 at 11:30
  • do a for loop from 0 to 4 over your array, instead of a foreach which processes everything in the array Commented Sep 22, 2017 at 11:32
  • @ADyson Using a for loop would break if the HTML happened to have less than 5 elements. Commented Sep 22, 2017 at 11:38
  • @GordonM in that case from 0 to the size of the array, or 4, whichever is less Commented Sep 22, 2017 at 11:38

1 Answer 1

2

Which of these loops is the one you want to restrict? Whichever one it is, the structure would be the same. Something like this:

$i = 0;
foreach ($collection as $item) {
    $i++;
    if ($i > 5) {
        break;
    }
    // the rest of your loop code
}

So you're basically storing in $i (or whatever you want to call the variable) a count of how many times the loop has executed. After 5 times, you use break; to exit the loop, regardless of how many records remain.

Sign up to request clarification or add additional context in comments.

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.