1

我想在 php 中显示我的 html 页面的主体,即条件为真或假 [即我使用 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标签内的许多地方将“to”和“to”转换为“来做很多修改,以使echo 语句的语法正确......有没有更好的方法来做到这一点??

4

1 回答 1

1

编写条件后,您应该关闭 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 的回声不会干扰您的报价,因此您可以将报价标准化为仅双引号或单引号(我没有更改您的)

于 2013-09-09T11:01:31.220 回答