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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML in PHP 1

Status
Not open for further replies.

Quijian

ISP
Jul 9, 2004
32
US
I am having trouble to display generate HTML using PHP. Most of it is ok, but when I try to display a image with the image source as variable, it doesn't work out so well. I believe it is a syntax error. Here is the code:
Code:
<img src=\"$authoriconp\" width=\"34\" height=\"32\">

$authoriconp is a varible with the source path.
 
It is echo, and the path is /public_html/images/usericons/admin.jpg.

As for the whole code snippet:
Code:
echo"<table width=\"532\" height=\"200\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"$tablename\">"
  . "                           <tr> "
  . "                             <td width=\"532\" height=\"52\"><div align=\"center\"> "
  . "                                 <table width=\"532\" height=\"52\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" background=\"images/titlebar.jpg\">"
  . "                                   <tr> "
  . "                                     <td><div align=\"center\"><font color=\"F7EBAA\" size=\"6\" face=\"Verdana, Arial, Helvetica, sans-serif\">$newstitlep</div></td>"
  . "                                   </tr>"
  . "                                 </table>"
  . "                                 <font face=\"Verdana, Arial, Helvetica, sans-serif\"></font></div></td>"
  . "                           </tr>"
  . "                           <tr> "
  . "                             <td height=\"120\"><div align=\"justify\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><font size=\"3\">$newsconp</font></font></div></td>"
  . "                           </tr>"
  . "                           <tr> "
  . "                             <td height=\"54\"><div align=\"center\">"
  . "                                 <table width=\"532\" height=\"0\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
  . "                                   <tr>"
  . "                                     <td width=\"266\" height=\"19\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\"><img src=\"$authoriconp\" width=\"34\" height=\"32\">&nbsp;$newsauthorp</font></td>"
  . "                                     <td width=\"266\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\">$newsdatep</font></div></td>"
  . "                                   </tr>"
  . "                                 </table>"
  . "                                 <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\"></font></div></td>"
  . "                           </tr>"
  . "                           <tr>"
  . "                             <td height=\"25%\">&nbsp;</td>"
  . "                           </tr>"
  . "                         </table>"
 ."";
 
the code looks good, I see no problem. What do you get in the page? a blank square qith an X in it? what do you get as a result (source code in the page as the browser see it)?

is the path and/or file name the correct one?

Cheers.
 
It is a blank square with a x in it. When I view the source I get this:
Code:
<img src="" width="34" height="32">
The path is fine, unless my database tables are messed up, but I checked that already.
 
ok, your exit for img tag tells me that the variable $authoriconp is empty, are you sure you fill it before to display it? where the var $authoriconp is filled?
 
Oh, man you were right. Thanks you so much. The problem was that my variable wasn't filling with the data from my MySql table. Saved me a great deal of frustration.
 
Hope you don't mind a comment on your coding style...

I find escaped quotes in quoted strings very hard to read. I've started doing the following:
Code:
$tmp = array();
$tmp[] = '<table width="532" height="200" border="0" cellpadding="0" cellspacing="0" id="$tablename">';
$tmp[] = '                           <tr>';
$tmp[] = '                             <td width="532" height="52"><div align="center">';
$tmp[] = '                                 <table width="532" height="52" border="0" cellpadding="0" cellspacing="0" background="images/titlebar.jpg">';
$tmp[] = '                                   <tr>';
$tmp[] = '                                     <td><div align="center"><font color="F7EBAA" size="6" face="Verdana, Arial, Helvetica, sans-serif">' . $newstitlep. '</div></td>';
$tmp[] = '                                   </tr>';
$tmp[] = '                                 </table>';
$tmp[] = '                                 <font face="Verdana, Arial, Helvetica, sans-serif"></font></div></td>';
$tmp[] = '                           </tr>';
$tmp[] = '                           <tr>';
$tmp[] = '                             <td height="120"><div align="justify"><font face="Verdana, Arial, Helvetica, sans-serif"><font size="3">' . $newsconp . ';</font></font></div></td>';
$tmp[] = '                           </tr>';
$tmp[] = '                           <tr>';
$tmp[] = '                             <td height="54"><div align="center">';
$tmp[] = '                                 <table width="532" height="0" border="0" cellpadding="0" cellspacing="0">';
$tmp[] = '                                   <tr>';
$tmp[] = '                                     <td width="266" height="19"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><img src="' . $authoriconp. '" width="34" height="32">&nbsp;$newsauthorp</font></td>';
$tmp[] = '                                     <td width="266"><div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="3">' . $newsdatep . '</font></div></td>';
$tmp[] = '                                   </tr>';
$tmp[] = '                                 </table>';
$tmp[] = '                                 <font face="Verdana, Arial, Helvetica, sans-serif" size="3"></font></div></td>';
$tmp[] = '                           </tr>';
$tmp[] = '                           <tr>';
$tmp[] = '                             <td height="25%">&nbsp;</td>';
$tmp[] = '                           </tr>';
$tmp[] = '                         </table>';
echo implode("\n",$tmp)."\n";
and find it much easier to read and debug.

Ken
 
kenrbnsn

It's a great idea to keep scripts as readable as possible. Have you ever considered using the HEREDOC syntax?

Code:
<?php
# HEREDOC interpolates variables and you can use large chunks of text/HTML without leaving the PHP context.
echo <<<END
<html>
<head>
<title>This is a sample for heredoc</title>
</head>
<body>
<p>My name is: $name</p>
<table width="100px">
<!-- and so on-->
END;
?>

There is no need for escaping or quoting of variables etc.
Arrays can be just shown enclosed in curly braces as {$_POST['myVar']}

As a note:
<font> tags are obsolete, deprecated. You'd make your HTML more versatile and cleaner to the current standards using CSS markup with id or class attributes. It makes changes in the presentation also much easier. Instead of having to redundantly state the font face a few lines in an external CSS file allows quick and clean access to changes without having to hack the code.
However, do as you see fit.

Cheers
DRJ478
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top