Page 1 of 1
how to echo an <ul> created in php script
Posted: Fri Oct 16, 2009 1:15 pm
by robertlob
In a php scrpt above the html tag on a page, I have the following code snippit that creates an unordered list:
Code: Select all
echo '<p>The following errors occurred:</p><ul class="error">';
// Print each error:
foreach ($errors as $err) {
echo "<li>Please enter a valid $err.</li>\n";
}
echo '</ul>';
Since the code is above the <html> tag, there is no point in echoing it as it is created. I would like to place the result of this snippit into a variable and echo the variable where it needs to be on the resulting page. Can someone show or tell me how to modify the code above to do that?
Thanks,
Robert
Re: how to echo an <ul> created in php script
Posted: Fri Oct 16, 2009 1:18 pm
by John Cartwright
String concatenation
Code: Select all
$output = '<p>The following errors occurred:</p><ul class="error">';
foreach ($errors as $err) {
$output .= "<li>Please enter a valid $err.</li>\n";
}
$output .= '</ul>';
Re: how to echo an <ul> created in php script
Posted: Fri Oct 16, 2009 2:07 pm
by robertlob
Thanks John..
I tried that before I posted, and probably had a syntax error eluding me so it didn't work. But since then I came up with a rather obvious solution that I should have thought of before I posted the question...
Simply eliminate the }else{ part of the php code outside of the <html>, and move the whole snippit to where it needs to be inside the <html> code. Then change the "echo" commands to "print" commands, all inside a <?php ....?> script. Working like a charm first time. Nothing like posting a dumb question to a forum to jog one's brain a bit.
Re: how to echo an <ul> created in php script
Posted: Fri Oct 16, 2009 2:57 pm
by John Cartwright
robertlob wrote:Thanks John..
I tried that before I posted, and probably had a syntax error eluding me so it didn't work. But since then I came up with a rather obvious solution that I should have thought of before I posted the question...
Simply eliminate the }else{ part of the php code outside of the <html>, and move the whole snippit to where it needs to be inside the <html> code. Then change the "echo" commands to "print" commands, all inside a <?php ....?> script. Working like a charm first time. Nothing like posting a dumb question to a forum to jog one's brain a bit.
Your not alone on that one. You'd be surprised how many times after describing my problem out-loud I would come up with the solution
