Jump to content

[SOLVED] Foreach in function only returns 1 value


colombian

Recommended Posts

Here is my problem, I need this function to be part of a string of outputs.

$output = "Html" //etc

$output .= "select";

$output .= attending($total_people);

However, it only returns 1 of the 9 supposed values for the select box.

 

<?php
function attending($var) {
$hour = range(1,9);
foreach ($hour as $h) { 
	if($var == $h) { $selected = "selected"; }
	else { $selected = ""; }
	return "<option {$selected} value=\"{$h}\"> {$h} </option>";
}
}	
?>

 

If I use "echo" instead of "return" it does give me all 9 outputs, but then I cannot make the form dynimically through the $output variable.

 

Any help is appreciated.

 

Thank you.

Thank you!

 

That did the trick - quick question though.

Why does doing $html = (no .= ) cause it to break?

 

More over, why does declaring $html before that loop cause it to break too?

 

 

I had to declare $html up with the $hour array, and leave as is to make PHP happy.

 

Just trying to understand what PHP is doing here.

 

Thanks again.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.