Jonathan Stevens
Jonathan Stevens

Reputation: 357

Return unordered list contents from function PHP

I'd really appreciate anyone's help in this. Basically I want to make an array of lists made from other arrays in order to get round the limitations of wordpress' do_shortcode function. I'm doing this using a number of functions.

LONG VERSION OF THE PROBLEM:

The code currently looks like this:

    /* These are the functions contained in a functions file */

    function output_entry_data() {
   $postdate = get_the_date('j/n/y');
   $entrytitle = get_the_title();

return ('<li><p class="postmeta">'. $postdate. '</p><h3>'. $entrytitle. '</h3></li>');
    }



    function output_month_data($entrynomax = '', $month = '', $entrydata = '') {
   $entryno = 1;

   while($entryno <= $entrynomax) {
    echo $entrydata[$entryno];
    $entryno++;
   }

    }


    function output_year_data($monthlynomax = '', $year = '', $monthlydata = '') {
   $monthno = 1;

   while($monthno <= $monthnomax) {
    echo do_shortcode('<h4>[slider title="'. $month. '"]</h4><ul>'. $monthlydata[$monthno]. '</ul>[/slider]');
    $monthno++;
   }

    }

    /* This is from a loop that determines whether you have reached the end of a month or a year */

    $entrydata[$entryno] = output_entry_data();
    $entrynomax = $entryno;

    $monthlydata = array($monthno => $monthno);
    $monthlydata[$monthno] = return(output_month_data($entrynomax, $month, $entrydata));
    $monthlynomax = $monthno;

    $annualdata[$yearno] = array($yearno => $yearno);
    $annualdata[$yearno] = return(output_year_data($monthlynomax, $year, $monthlydata));

    $entryno = 1;
    $monthno = 1;
    $yearno++;
    $yearo = get_the_date('Y');

    /* The idea is that all the data gets outputted at the end of the loop like this: */

    $yearnomax = $yearno;

    echo ('<ul>');

   $yearno = 1;

   if($yearno <= $yearnomax) {
    echo do_shortcode('<h3>[expand title ="'. $year. '"]</h3><ul>'. $annualdata[$yearno]. '</ul>[/expand]');
    $yearno++;
   }

    echo('</ul>');

At the moment the code is successfully creating the $entrydata[$entryno] array because the function output_entry_data() simply returns a line of code each time.

However, when I try to create the array $monthlydata[$monthno] for each month, it simply runs the function output_month_data() and makes a big list of all the monthly entries, rather than passing the data to the array to be used by the other functions.

I can see that this is because I used 'return' in output_entry_data() and 'echo' in output_month_data()

SHORT VERSION OF THE PROBLEM

Each item in the array $entrydata[$entryno] is a string containing a list item tag, I want output_monthly_data() to return one big string of all the items in $entrydata[$entryno] to be used by other functions, rather than echo them as the code currently does. Can this be done when there's a while loop involved?

Many thanks, I'd appreciate any input here.

Upvotes: 1

Views: 406

Answers (1)

knittl
knittl

Reputation: 265271

Yes, this is (easily) possible. There are at least two ways

  1. Concatenate strings and return the resulting string:

    function output_month_data($entrynomax = '', $month = '', $entrydata = '') {
      $entryno = 1;
    
      $return = '';
      while($entryno <= $entrynomax) {
        $return .= $entrydata[$entryno]; # concatenate strings
        $entryno++;
      }
    
      return $return;
    }
    
  2. Store results in an array and use implode to return a string containing all items:

    function output_month_data($entrynomax = '', $month = '', $entrydata = '') {
      $entryno = 1;
    
      $return = array();
      while($entryno <= $entrynomax) {
        $return[] = $entrydata[$entryno]; # append to array
        $entryno++;
      }
    
      return implode('', $return); # change '' to any glue you want
    }
    

Upvotes: 1

Related Questions