Solved

"Multiple" PHP return function?

Posted on 2014-10-03
2
192 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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.

732 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