Solved

"Multiple" PHP return function?

Posted on 2014-10-03
2
186 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
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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