?
Solved

"Multiple" PHP return function?

Posted on 2014-10-03
2
Medium Priority
?
209 Views
Last Modified: 2014-10-03
Hello, please note I am beginner at PHP.

I have a function that I would like to return, instead of echo it (echo breaks code at certain point). However, I don't have a clue hot to build and output that as I have a foreach there. Pls. see an example, you will understand it easily.


function list_articles($condition, $number = 3, $indentation = 3)
	{
	if ($condition == "home")
		{
		$sql = "SELECT Date,Time,Article FROM Articles WHERE PUBLISHED = 1 ORDER BY Date DESC, Time DESC LIMIT " . $number;
		}
		//
	else if ($condition == "archive")
		{
		$sql = "SELECT Date,Time,Article FROM Articles WHERE PUBLISHED = 1 ORDER BY Date DESC, Time DESC";
		}
		//
	else if ($condition == "admin")
		{
		$sql = "SELECT Date,Time,Article FROM Articles ORDER BY Date DESC, Time DESC";
		}
		//
	else
		{
		return FALSE;
		}
	//
	//DB fetch, returns $result
	//
	$result = retrieve_from_database($sql); //Add another control here
	//
	// Print list
	//
	
	echo str_repeat("\t", $indentation) . "<ul>\n";
	foreach($result as $value)
		{
		echo str_repeat("\t", $indentation) . "\t<li>" . datetime_converter($value["Date"]) . " - " . $value["Article"] . "</li>\n";
		}
	echo str_repeat("\t", $indentation) . "</ul>";
	}

Open in new window

0
Comment
Question by:mrmut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40360306
So put it in a string and return it when finished
...
...
	$my_string = str_repeat("\t", $indentation) . "<ul>\n";
	foreach($result as $value)
		{
		$my_string .= str_repeat("\t", $indentation) . "\t<li>" . datetime_converter($value["Date"]) . " - " . $value["Article"] . "</li>\n";
		}
	$my_string .= str_repeat("\t", $indentation) . "</ul>";
	return $my_string;
	}

Open in new window

0
 

Author Closing Comment

by:mrmut
ID: 40360314
Yeah... The worst part of questions like these is that I know it is simple, but don't have a clue how to do it. Thanks a lot.
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question