Return value from function with loop in 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:

[php]
/* 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 (’

    ’);
$yearno = 1;

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

echo(’’);
[/php]

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

I want the function output_month_data() to generate the contents of an unordered list based on the array $entrydata[$entryno], but simply to return the contents for use by other functions rather than echo them. Can this be done when there’s a while loop involved?

Many thanks, I’d appreciate any input here.

Sponsor our Newsletter | Privacy Policy | Terms of Service