Forum Moderators: coopster

Message Too Old, No Replies

Displaying PHP variable in HTML table

         

method115

12:59 am on Jul 3, 2008 (gmt 0)

10+ Year Member



I can't seem to get my variable to show up once I put them in the html table I've looked up many ways to do this and it wont work.

Here is part of the script:

while($row=mysql_fetch_array($result))
{
//the format is $variable = $r["nameofmysqlcolumn"];
//modify these to match your mysql table columns

$DED=$row["DED"];
$IP=$row["IP"];
$Colo=$row["Colo"];
$Company=$row["Company"];
$Owner=$row["Owner"];
$Email=$row["Email"];
$Phone=$row["Phone"];
$location=$row["Location"];
$Switch=$row["Switch"];
$combo=$row["combination"];
$Form=$row["Formfactor"];
$userpass=$row["userpass"];
$other=$row["otherlabel"];
$notes=$row["Notes"];

echo '<html>
<body bgcolor="#AAAAAA">

<TABLE width="100%" height="100%" border="0">
<tr width="100%" height="10%">
<td align="center" valign="center" width="90%">

<TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0><TR><TD><TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
<TR>
<TD ALIGN="right">DED:</TD>
<TD COLSPAN=3 BGCOLOR="#ffffff">
<? echo $DED;?>
</TD>
</TR>
<TR>
<TD ALIGN="right">IP</TD>
<TD COLSPAN=5 BGCOLOR="#ffffff"><?echo $IP;?></TD>
</td>

</TR>
</tr>
</table></table>

</body>
</html>';
}

If I echo the varaible anywhere outside ot the table they work. Not sure whats going on here

eelixduppy

1:51 am on Jul 3, 2008 (gmt 0)



Hello, and Welcome to WebmasterWorld!

It is because your syntax isn't correct. Since you are surrounding the entire string with single quotes (') everything is being sent to the browser as a string literal, including the 'variables' that you have in the string. In order to do this, you should be surrounding the string with double quotes (") and removing the excess PHP code that you have added.

Instead of adding the double quotes, though, I'm going to use a special syntax called Heredoc to make things a little easier, otherwise I'd have to add slashes before all of your double quotes in the string so that they don't terminate the string prematurely. So Heredoc syntax looks like the following:


echo <<<HTML
<html>
<body bgcolor="#AAAAAA">
<TABLE width="100%" height="100%" border="0">
<tr width="100%" height="10%">
<td align="center" valign="center" width="90%">
<TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0><TR><TD><TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=2>
<TR>
<TD ALIGN="right">DED:</TD>
<TD COLSPAN=3 BGCOLOR="#ffffff">
$DED
</TD>
</TR>
<TR>
<TD ALIGN="right">IP</TD>
<TD COLSPAN=5 BGCOLOR="#ffffff">$IP</TD>
</td>
</TR>
</tr>
</table></table>
</body>
</html>
HTML;

You can find more information on Heredoc, and other string information, at the String documentation [php.net] at [php.net...]

Good luck :)

method115

2:02 am on Jul 3, 2008 (gmt 0)

10+ Year Member



Ah thanks so much I thought it had something to do with me using single quotes. I just couldn't find anything to be positive. Much appreciated friend.

StoutFiles

2:08 am on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First off, you have the html and body tags in a loop. Get them out of the loop pronto. html and body before it, /html and /body after it.

Secondly, you need to break off the echo to use a variable.
//example
<?php
$IP =4;
echo 'The IP is '.$IP.' for your computer';
?>

//output
The IP is 4 for your computer

Some other small mistakes:

<TR>
<TD ALIGN="right">IP</TD>
<TD COLSPAN=5 BGCOLOR="#ffffff"><?echo $IP;?></TD>
</td> <--needs to be removed, you already closed it above
</TR><-needs to be removed, you have one below
</tr>

</table></table>
//needs to be </table></td></tr></table>

and so on.

[edited by: StoutFiles at 2:12 am (utc) on July 3, 2008]

method115

2:23 am on Jul 3, 2008 (gmt 0)

10+ Year Member



Yea your right about the tables but there working fine the way I have them. The main problem as the variable not showing up which they do now thanks to the above poster. I was in a rush closing all the TR/TD/Tables just to test if the variables actually showed up.

eelixduppy

2:23 am on Jul 3, 2008 (gmt 0)



Nice catch, StoutFiles; I wasn't actually paying attention to the content of the echo :)