1

I am having trouble displaying data from a PHP array. My array looks like this:

    Array
    (
        [July] => Array
            (
                [0] => Array
                    (
                        [ETitle] => Launch Party
                        [EStart] => 2017-07-26T09:00:00
                        [EEnd] => 2017-04-28T17:00:00
                        [ELink] => http://example.dev/events/launch-party
                        [EStartMonth] => July
                        [EStartYear] => 2017
                    )

            )

        [August] => Array
            (
                [1] => Array
                    (
                        [ETitle] => Open Day
                        [EStart] => 2017-08-10T00:00:00
                        [EEnd] => 2017-08-11T00:00:00
                        [ELink] => http://example.dev/events/open-day
                        [EStartMonth] => August
                        [EStartYear] => 2017
                    )

                [2] => Array
                    (
                        [ETitle] => Dama Google Event
                        [EStart] => 2017-08-20T02:00:00-07:00
                        [EEnd] => 2017-08-20T03:00:00-07:00
                        [EStartMonth] => August
                        [EStartYear] => 2017
                        [ELink] => https://www.google.com/calendar/event?eid=NjdsZW9ydmE4bWlmOHRnZ3J0dGw3MTVlamIgcnY3YTZyMTlmMjQyMHZvcmFkcWNrbW1zdG9AZw
                    )

            )

    )

What i need to display on the page a list of items grouped under headings like this, formatted with html:

    <h2>July</h2>
    <ul>
    <li><a href="http://example.dev/events/launch-party">Launch Party</a></li>
    </ul>

    <h2>Augast</h2>
    <ul>
    <li><a href="http://example.dev/events/open-day">Open Day</a></li>
    <!-- if link contains "google" string, link needs target attribute -->
    <li><a target="_blank" href="https://www.google.com/calendar/event?eid=NjdsZW9ydmE4bWlmOHRnZ3J0dGw3MTVlamIgcnY3YTZyMTlmMjQyMHZvcmFkcWNrbW1zdG9AZw">Dama Google Event</a></li>
    </ul>

I have tried all sorts of solutions from stack overflow but I keep ending up with invalid index warnings when I try loop through recursively. How do I solve this, and build the HTML list as described?

EDIT: updated expected result with real HTML

3
  • Do you know foreach Loop? Commented Jul 23, 2017 at 9:49
  • Yes, I have tried lots of foreach loops and recursive functions but none of them work. I am just beginning to learn PHP. Commented Jul 23, 2017 at 9:52
  • Please provide the "HTML" result that you want. Commented Jul 23, 2017 at 9:59

2 Answers 2

1

Take a look at foreach loops in PHP.

Let's say your array is stored in a variable called $events:

foreach ($events as $monthName => $month) {
  echo '<h2>' . $monthName . '</h2>';
  echo '<ul>';
  foreach ($month as $event) {
    if (strpos(parse_url($event['ELink'], PHP_URL_HOST), 'google') !== false) {
      $target = ' target="_blank"';
    } else {
      $target = '';
    }
    echo '<li><a' . $target . ' href="' . $event['ELink'] . '">' . $event['ETitle'] . '</a></li>';
  }
  echo '</ul>';
}

I also added the target="_blank" attribute if the url host contains "google".

Try it out here.

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

6 Comments

Yes, your code works , but he didn't provide the HTML he wants before your answer. He needs to change the HTML elements according to his needs
This was enough of a hint, and i've edited the original post with real HTML.
@Accountant yes, I edited my answer now after he edited his question.
@PeterMader Thanks for updating with the target attribute. What's the advantage of using PHP_URL_HOST? I simply did (strpos($event['ELink'], 'google') !== false) which works well enough, but please explain the advantages of your way. Is it safer?
Your solution will detect example.dev?a=google as a URL to a Google domain, although the domain is actually example.dev. Is that what you want?
|
1

How did you try the foreach loops? Here is a short example of how your array should look like in php to iterate through ist.

The outer foreach iterates the months and prints their name. The second/inner foreach iterates through all events(?) in the month. The HTML output is just as an example.

<?php
$data = array (
    'July' => array(
            array(

                    'ETitle' => 'Launch Party',
                    'EStart' => '2017-07-26T09:00:00',
                    'EEnd' => '2017-04-28T17:00:00',
                    'ELink' => 'http://example.dev/events/launch-party',
                    'EStartMonth' => 'July',
                    'EStartYear' => 2017
                )

        ),

    'August' => array
        (
            array(

                    'ETitle' => 'Open Day',
                    'EStart' => '2017-08-10T00:00:00',
                    'EEnd' => '2017-08-11T00:00:00',
                    'ELink' => 'http://example.dev/events/open-day',
                    'EStartMonth' => 'August',
                    'EStartYear' => 2017
                ),

            array (

                    'ETitle' => 'Dama Google Event',
                    'EStart' => '2017-08-20T02:00:00-07:00',
                    'EEnd' => '2017-08-20T03:00:00-07:00',
                    'EStartMonth' => 'August',
                    'EStartYear' => 2017,
                    'ELink' => 'https://www.google.com/calendar/event?eid=NjdsZW9ydmE4bWlmOHRnZ3J0dGw3MTVlamIgcnY3YTZyMTlmMjQyMHZvcmFkcWNrbW1zdG9AZw'
                )

        )

);

//echo '<pre>';
//var_dump($data);

$month = null;
foreach($data as $key => $dates) {
    if ($month !== $key) {
        $month = $key;
        echo '<h1>'.$month.'<h1>';
    }

    if (!empty($dates)) {
        foreach($dates as $data) {
            $title = (isset($data['ETitle'])) ? $data['ETitle'] : 'no title ???';
            $link = (isset($data['ELink'])) ? $data['ELink'] : '#';
            echo '<a href="'.$link.'">' . $title . '</a>';
        }
    }
}
?>

You can test this example in php sandbox.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.