Page 1 of 1

help in separating html code and php code

Posted: Tue Jul 12, 2005 7:09 am
by ace4hire
i'm new in using php.. guys please help meh on how to separate my php code to html... what will i do??

here's an example code,this code is combined w/ html and php..

Code: Select all

<?php	
session_start();
require("cn.php"); 
$select = "select * from $table2 where username=('$uname') and password=('$pword')";
$rs = mysql_query($select) or die(mysql_error());
$count = mysql_num_rows($rs);
if($count==1){
	session_register("uname");
	session_register("pword");
}

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>
<?php
if($count==1){?>
<table width="51%" height="27" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" class="main">
  <tr bgcolor="#003366" class="label1">
    <td width="178"><div align="center" class="style4"><a href="<?php echo $PHP_SELF; ?>?order=name"><font color="#FFFFFF">Name</font></a></div></td>
    <td width="183"><div align="center" class="style4"><a href="<?php echo $PHP_SELF; ?>?order=Message_Date"><font color="#FFFFFF">Message Date</font></a></div></td>
  </tr>
  <?php
if($order==""){
	$order = "Msg_Id";
}
$select = "select * from $table3 order by $order";
$rs = mysql_query($select) or die(mysql_error());
$totalRows = mysql_num_rows($rs);
for($i=0;$i<$totalRows;$i++){
	$rows = mysql_fetch_array($rs);
	$Msg_Id = $rows["Msg_Id"];
	$name = $rows["name"];
	$Message = $rows["Message"];
	$Message_Date = $rows["Message_Date"];

  echo "<tr>
    <td><a href=\"viewmsg.php?Msg_Id=$Msg_Id\"> $name </a></td>
	<td>$Message_Date</td>
  </tr>";
}
?>
</table>

  <table width="876" height="37" border="0">
    <tr>
      <td height="33"><form name="form1" method="post" action="createmsg.php">
        <input name="create" type="submit" id="create" value="Create Message">
      </form></td>
    </tr>
  </table>
<p><?php  }?></p>
</body>
</html>

Posted: Tue Jul 12, 2005 9:49 am
by pickle
I'm not sure what you want. You're PHP code and HTML code is already separated a bit. Are you looking for a templating setup?

Posted: Tue Jul 12, 2005 10:25 am
by theda
Personally, I find coding in both HTML and PHP as the base for a .php file annoying, but have seen a few professionals do it (I've had someone on this website recommend it to me <_<). I program with php as the base, and "include" or "print" html parts such as this:

Code: Select all

<?php
include "blah.ext";
$variable = "something blah blah";
print "<HTML><head></head><body></body></html>";
?>
Maybe I'm just a big of a novice, but I find that way a lot easier, because then you can just edit out the HTML and save the html to another file and switch the 'print "";' to 'include = "file";'