如何创建基于 PHP if 条件的 HTML 正文


how to create a html body basing on a php if condition

我想显示我的 html 页面的正文,与 php 中的条件是真或假 [即我使用 if, else 条件]...

<?php
session_start();
if(isset($_SESSION['user']))
{
    echo ".  Hello ".$_SESSION['user']." !";
}
else
{
    header("Location: login.php");
}
?> 
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<link href="slims.css" rel="stylesheet" type="text/css">
<title>Enter Attendance</title>
</head>
<body >
<a id="logout" href="logout.php">Logout </a>
<div id="wrapper">
<header>
<h1>Welcome to SLIMS</h1>
<nav id="mainnav">
      <ul>
        <li><a href="index.php">Home</a></li>
        <li><a href="secselect.php" class="thispage">
        attendance</a></li>
        <li><a href="#">marks</a></li>
        <li><a href="#">results</a></li>
        <li><a href="#">others</a></li>
      </ul>
</nav></header>
<div id=body>
<br/>
<?php
$num=1;
mysql_connect("localhost","root","");
mysql_select_db("attendance");
$_SESSION['tablename']=$_GET['dept']."-".$_GET['year']."-".$_GET['sec']."-".$_GET['sem'];
//echo $_SESSION['tablename'];
$_SESSION['subject']=$_GET['subject'];
$_SESSION['datepicker']=$_GET['datepicker'];
$qstr2="select * from `".$_SESSION['tablename']."-total` where date='".$_GET['datepicker']."' and subject='".$_GET['subject']."';";
//echo $qstr2;
$check=mysql_query($qstr2);
if($check)
{ echo "<font color='white' align=center face='Lucida Sans Unicode'><h2 class='centered'>Attendence for the selected date was already entered</h2></font><br><br><br><br>";
}
else
{
 $qstr="select * from `".$_SESSION['tablename']."`;";
 $result=mysql_query($qstr);
 if(!$result)
 {
  echo "Invalid details selected";
 }
 else
 {
   echo '<font color="white" align=center face="Lucida Sans Unicode"><h2>Enter Attendance</h2></font>
<form method="post" action="insertatt.php" ><p align="center">
<table  id="box-table-a";  >
<thead>
<tr>
<th scope="col"> Sl.No </th>
<th scope="col"> JNTU No. </th>
<th scope="col"> Name </th>
<th scope="col"> Classes </th>
</tr>
</thead>
<tbody>'; 
$num=1;
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row['Sl.No']."</td>
<td>".$row["JNTU No"]."</td>
<td>".$row["Name"]."</td>
<td> <input type='radio' name='rbtn".$num."' value=1 /> 1
     <input type='radio' name='rbtn".$num."' value=2 /> 2</td>
</tr>";
$num=$num+1;
}  
 echo "
</tbody>
</table>
<input type='submit' name='submit' value='Post Attendance' />
<input type='reset' name='reset' value='Clear Form' /></p><br><br>
</form>"; 
 } 
}?>
</div>
<footer><div id="footer">&copy; 2013 Team 17</div>
</footer>
</div>
</body>
</html>

我需要通过在 html 标签内的许多地方将"到 ' 和 ' 到 " 进行大量修改,以使 echo 语句的语法正确......有没有更好的方法可以做到这一点?

一旦写入条件,您应该关闭 PHP 标记。首先将数据库逻辑和其他内容分隔到另一个文件或文档顶部:

<?php
$num=1;
mysql_connect("localhost","root","");
mysql_select_db("attendance");
$_SESSION['tablename']=$_GET['dept']."-".$_GET['year']."-".$_GET['sec']."-".$_GET['sem'];
//echo $_SESSION['tablename'];
$_SESSION['subject']=$_GET['subject'];
$_SESSION['datepicker']=$_GET['datepicker'];
$qstr2="select * from `".$_SESSION['tablename']."-total` where date='".$_GET['datepicker']."' and subject='".$_GET['subject']."';";
//echo $qstr2;
$check=mysql_query($qstr2);
 $qstr="select * from `".$_SESSION['tablename']."`;";
 $result=mysql_query($qstr);
 ?>

然后从条件开始:

<?php if($check): ?>
    <font color='white' align=center face='Lucida Sans Unicode'><h2 class='centered'>Attendence for the selected date was already entered</h2></font><br><br><br><br>
<?php else: ?>
<?php if(!$result): ?>
    <p>Invalid details selected</p>
<?php else: ?>
    <font color="white" align=center face="Lucida Sans Unicode"><h2>Enter Attendance</h2></font>
    <form method="post" action="insertatt.php" ><p align="center">
    <table  id="box-table-a";  >
    <thead>
    <tr>
    <th scope="col"> Sl.No </th>
    <th scope="col"> JNTU No. </th>
    <th scope="col"> Name </th>
    <th scope="col"> Classes </th>
    </tr>
    </thead>
    <tbody>
    <?php $num=1;while($row=mysql_fetch_array($result)): ?>
    <tr><td><?= $row['Sl.No']; ?></td>
    <td><?= $row['Name']; ?></td>
    <td><input type='radio' name='rbtn<?=$num;?>' value=1 /> 1
    <input type='radio' name='rbtn<?=$num;?>' value=2 /> 2</td>
    </tr>
<?php $num=$num+1; ?>
<?php endwhile: ?>
    </tbody>
    </table>
    <input type='submit' name='submit' value='Post Attendance' />
    <input type='reset' name='reset' value='Clear Form' /></p><br><br>
    </form>
<?php endif; ?>
<?php endif; ?>
    </div>
    <footer><div id="footer">&copy; 2013 Team 17</div>
    </footer>

这样 php 的回声就不会干扰您的报价,因此您可以将报价标准化为仅双倍或仅单(我没有更改您的报价)