Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble displaying table inside PHP echo statement

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hello,

I'm having some trouble displaying an HTML table when a condition is met. It appears as if the echo statment is ending prematurely and as a result some text is being sent to the brower which shouldn't. I'm trying to display the table using the echo statement using "here document" syntax. I also tried just enclosing the whole table statement in quotes. No luck. Here is the offending code.

Code:
<?php 

if($badUserName == 1)
{
	ECHO <<<END 
		<table border="0">
			<tr>
				<td align=center>
					Login Failure!  Username missing <br> Please try again
				</td>
			</tr>
		</table>
	END;
}		
?>

When this is run however this is displayed in the browser.

Login Failure! Username missing
Please try again END; } ?>


So it looks like the echo tag is ending too soon. It's also worth noting that when viewing the page in my firefox browser the color coding denoting the PHP section stops after the <table border="0"> tag. So I guess the first tag to not be inclluded in the echo is the <tr> tag. Any ideas as to why this is happening? Any and all help is apprecited.

Thanks
 
Thanks for the tip but unfortunately even after making sure that the closing tag was on it's own line etc the same problem exists. As I mentioned even doing an echo ".... " around the table tags causes the same behavior. Any more sugestions? Do I need to to make a declaration at the beginning of my document to say what version of PHP should parse this file etc?
 
How about...
Code:
<?php
if($badUserName == 1)
{
    [red]print("[/red]<table border=[red]\[/red]"0[red]\[/red]">
            <tr>
                <td align=[red]\"[/red]center[red]\"[/red]>
                    Login Failure!  Username missing <br [red]/[/red]> Please try again
                </td>
            </tr>
        </table>[red]")[/red];
}        
?>

Or better yet:
Code:
<?php

if($badUserName == 1)
{
   [red]print("<div style=\"text-align: center;\">[/red]Login Failure!  Username missing <br[red]/[/red]> Please try again[red].</div>")[/red];
}        
?>
Then move the style into a style sheet, and reuse it for all your errors, adding font changes to the style sheet so you don't have to repeat the style code for each error...

[plug=shameless]
[/plug]
 
Oh, you could also add the newline chars \n at the end of each line in a print or echo to make sure they happen. Also, in both my examples, I gave the br the "/" to make sure it's got a closing tag... Well, you can see the changes as they are marked in red.

[plug=shameless]
[/plug]
 
Thanks for the tips jstreish. I tried both and still am getting the same behavior I even tried this

Code:
print "<table border=0><tr><td align=center>Login Failure!  Username missing Please try again</td></tr></table>";

Putting everything on one line and it still thinks the PHP section stops at <table border="0"> It's worth noting that I'm using Eclipse to do my devlopment and things behave correctly in the preview pane in Eclipse. Seems to suggest something weird going on with the browser. This is what is displayed in the browser when looking at the page.

Login Failure! Username missing Please try again"; } ?>
 
Escape your quotes!!!
the \" aren't there for looks.

Here is the result of the second suggestion:
game-master.us/lala.php

And here, again is the method I used:
Code:
 print("<div style=[red]\[/red]"text-align: center;[red]\[/red]">Login Failure!  Username missing <br/> Please try again.</div>");

The slashes "\" need to be there. And this shouldn't require a table.

[plug=shameless]
[/plug]
 
Hi jsreich,

I took your suggestion and created a file with just this.

Code:
<?php
if (1)
 {
 	 print("<div style=\"text-align: center;\">Login Failure!  Username missing <br/> Please try again.</div>");
 }
?>

As I mentioned this look looks OK when viewed in the Eclipse environemnt but once I try opening this in Firefox I get this in the browser.

Login Failure! Username missing
Please try again."); } ?>

I looked at the page you created and it displayed fine. Any idea what what be happening here?
 
Perhaps it's an Eclipse problem? Have you tried looking creating the script using a simple text editor to see what happens?

Under what environment are you running PHP?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
The best way to output large quantities of html text and code, is to simply escapoe the PHP delimiters.
Taking your original coding...
Code:
<?php

if($badUserName == 1)
{

[COLOR=#ff0000]?>[/color]
  

        <table border="0">
            <tr>
                <td align=center>
                    Login Failure!  Username missing <br> Please try again
                </td>
            </tr>
        </table>



[COLOR=#ff0000]<?PHP[/color]
} //closed IF statement        
?>


Just before wiritng all the table coding, i closed the PHP brackets, and reopened them after the table. and then I closed the IF statement. with a "}".
In this way, the If statement is maintained, and you dont have to fiddle with double quotes and the such.

You can see an example of it here

If you check the checkbox, the table shows, if you uncheckit the table does not show.

the code looks like this:

Code:
<?

if($something==1){
?>
<table border="2">
<tr><td>This is a table within a PHP script</td></tr>
</table>
<?
}
else{
echo "Checked off";
}

?>

Hope this helps.
 
vacunita, the context switching requires a little more time and resources, so staying the longer you stay in one context the better... hence the PHP print. I use a div and css, instead of a large table in my snipets for 2 important reasons:
It's not a table of data
It's just used to center text... ergo it should just be a block level element.

[plug=shameless]
[/plug]
 
i suggest to give single quote.

like

<?php

if($badUserName == 1)
{
print'<table border="0">
<tr>
<td align=center>
Login Failure! Username missing <br> Please try again
</td>
</tr>
</table>';
} //closed IF statement
?>

hope this helps u
keats
 
His problem, I think, is that he's still escaping the last quote (the end of the string). I prefer double arround strings because I program in Java and C++ -- it just looks right to me. Granted that it means more escapes to keep valid html... but his isn't standards compliant anyway He could remove the "" from around the 0, and use the table as well... but it's not the best solution. Perhaps having him use single quotes isn't bad -- unless he plans to program in C/C++/JAVA later on; but, he should still learn when to escape a char and when it's not needed.

[plug=shameless]
[/plug]
 
His problem, I think, is that he's not updating the page on the server somehow. There have been numerous suggestions that should've worked 100%, but the user reports the same problem as in the beginning -- even if code is completely different.
 
Hello Guys,

I figured out the problem. I was trying to run the file by simply double clicking it in Windows Explorer. For some reason this does not allow the PHP parser to parse the document correctly. If instead I open a new browser session and navigate to the page using something like loclahost/aml/index.php everything works fine. I just guessed at this and it turns out I was right. Thanks again though for all your help.
 
As I understand it, PHP goes and parses the whole file before it hands it over to the browser. so if its already reading the entire file, its just skipping what it knows it can't parse, and parses what it can.
I've been doing it this way for oiver 7 years now. and have not seen any performacne degrade ever, even in very long pages that display alot of info from the DB.

For example what happens when you have a function you need to call at some point in your web page. You include the file with the function at the top. opening and closing the PHP delimiters. in one line. And then call it as needed elsewhere on the page.

Opening and closing PHP delimiters does nothing but tell the PHP parser where to parse and where not to.

When it finds a closing delimiter it just skips down until it finds the next openign delimiter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top