0

I am trying to educate myself more on arrays and foreach statements.

In one file I have the following class and function:

class Onboarding_Dashboard {

static function dashboard_onboarding_content() {
    $items = array(
        'stage-1' => array(
            'title' => 'Hello',
            'content' => 'This is some content text',
            'show-next' => 'true',
            'show-prev' => 'true',
        ),
        'stage-2' => array(
            'title' => 'Hello',
            'content' => 'This is some content text',
            'show-next' => 'true',
            'show-prev' => 'true',
        )


    )
}    
}

In another file I'd like to use the content of this to create a foreach statement. Something like this:

static function action_get_content() {
    $items = Onboarding_Dashboard::dashboard_onboarding_content();
    foreach ($items as $item) {
        echo '<h2>'.$item['title'].'</h2>';
        echo '<p>'.$item['content'].'</p>';
    }

}

But I'm not sure what else I need to add in order to get the data from my Class function dashboard_onboarding_content

UPDATE: I've updated my foreach statement, and wrapped it in a function. How do I cho the reults of this function at another point in my code - is it as simple as doing

echo action_get_content();

2 Answers 2

1

Edited to answer the OP's comment

As the op asked how to make another function which handles the foreach which he can then echo, i have added the dashboard_foreach_example() function.

class Onboarding_Dashboard {

    static function dashboard_onboarding_content(){
        $items = array(
            'stage-1' => array(
                'title' => 'Hello',
                'content' => 'This is some content text',
                'show-next' => 'true',
                'show-prev' => 'true',
            ),
            'stage-2' => array(
                'title' => 'Hello',
                'content' => 'This is some content text',
                'show-next' => 'true',
                'show-prev' => 'true',
            )
        );

        return $items;
    }    

    static function dashboard_foreach_example(){
        $items = self::dashboard_onboarding_content();
        $output = '';
        foreach ($items as $item) {
            $output .= '<h2>'.TITLE ATTRIBUTE.'</h2>';
            $output .= '<p>'.TITLE ATTRIBUTE.'</p>';
        }

        return $output;
    }
}

You can now simply echo the dashboard_foreach_example() as it will generated the output in the function

echo Onboarding_Dashboard::dashboard_foreach_example();
Sign up to request clarification or add additional context in comments.

7 Comments

This is close to where I was getting to actually. Quick question, say I want to wrap my foreach statement in a function (action_get_content) how do I echo it out elsewhere - I'll update my question so you can see what I mean
Awesome, that works - do I have to keep dashboard_foreach_example in the same file as the above function?
As it is right now it's a function in the same class, therefore cant really be split up, but you can make a new function which does the same as long as you change the $items to actually reach the dashboard_onboarding_content() function
So if I change $items to be $items = Onboarding_Dashboard::dashboard_onboarding_content(); then it should work in another class right?
Exactly :) you got it
|
0

Change your function to return the array:

class Onboarding_Dashboard {
   static function dashboard_onboarding_content() {
      // your $items array stays here
      return $items;
   }
}

function action_get_content() {
   $html = '';
   foreach (Onboarding_Dashboard::dashboard_onboarding_content() as $item) {
      $html .= '<h2>'.$item['title'].'</h2>';
      $html .= '<p>'.$item['title'].'</p>';
   }
   return $html;
}

Then you output it this way:

echo action_get_content();

Alternatively you can have this function as part of your class:

class Onboarding_Dashboard {
   static function dashboard_onboarding_content() {
      // your $items array stays here
      return $items;
   }

   static function action_get_content() {
      $html = '';
      foreach (self::dashboard_onboarding_content() as $item) {
         $html .= '<h2>'.$item['title'].'</h2>';
         $html .= '<p>'.$item['title'].'</p>';
      }
      return $html;
   }
}

Then you output it this way:

echo Onboarding_Dashboard::action_get_content();

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.