how to echo an <ul> created in php script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robertlob
Forum Newbie
Posts: 3
Joined: Sat Sep 06, 2003 7:54 am
Location: OK, USA

how to echo an <ul> created in php script

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to echo an <ul> created in php script

Post 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>';
robertlob
Forum Newbie
Posts: 3
Joined: Sat Sep 06, 2003 7:54 am
Location: OK, USA

Re: how to echo an <ul> created in php script

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to echo an <ul> created in php script

Post 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 :)
Post Reply