Solved

"Multiple" PHP return function?

Posted on 2014-10-03
2
177 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now