2011-03-07 45 views
0

這是我的代碼:如何在php中劃分網頁?

<?php 
function GetDays($sStartDate, $sEndDate){ 
    $sStartDate = gmdate("Y-m-d", strtotime($sStartDate)); 
    $sEndDate = gmdate("Y-m-d", strtotime($sEndDate)); 

    $aDays[] = $sStartDate; 
    $sCurrentDate = $sStartDate; 
    while($sCurrentDate < $sEndDate){ 
    $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate))); 
     $aDays[] = $sCurrentDate; 
    } 
    return $aDays; 
} 
?> 

<html> 
<head> 
<script language="JavaScript" src="calendar_us.js"></script> 

    <link rel="stylesheet" href="calendar.css"> 
</head> 

<body background="mainbg.JPG"> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST"> 
<? 
//If we submitted the form 
if(isset($_POST['submitMe'])) 
{ 
$sec=00; 
$sc=00; 
$name=$_POST['name']; 
$mm=substr($_POST['testinput'],0,2); 
$dd=substr($_POST['testinput'],3,2); 
$yy=substr($_POST['testinput'],6,4); 
$sdate = $yy."-".$mm."-".$dd; 

$mn=substr($_POST['test'],0,2); 
$dn=substr($_POST['test'],3,2); 
$yn=substr($_POST['test'],6,4); 
$edate = $yn."-".$mn."-".$dn; 

$host="10.207.100.10";// Host name 
$username="root";// Mysql username 
$password="######"; // Mysql password 
$db_name="wtems"; // Database name 
$tbl_name="constellation_reserve"; // Table name // Connect to server and selectdatabse 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
$sql="SELECT * FROM constellation_reserve where CID=$name and (start_date between '$sdate' and '$edate' or end_date between '$sdate' and '$edate')"; 
$result=mysql_query($sql); // Define$color=1 
if($result) 
    { 
$color="1"; 
echo '<table border="0" cellpadding="8" align="center">'; 
echo'<tr>'; 
     echo'</tr>'; 
     echo'<tr>'; 
     echo'</tr>'; 
    echo '<tr><h3 align="center"<font color="white">RESERVATION </font></h3></tr>'; 
    echo'<tr>'; 
     echo'</tr>'; 
    echo'<tr>'; 
echo '<table width="500" height="300" border="0" align="center" cellpadding="8" cellspacing="2">'; 
$fields_num = mysql_num_fields($result); 
    echo "<tr bgcolor='#0099FF'>"; 
    echo "<td><FONT COLOR='FFFFFF'>ID</FONT> </td>"; 
    echo "<td><FONT COLOR='FFFFFF'>START DATE</FONT></td>"; 
    echo "<td> <FONT COLOR='FFFFFF'>RESERVED BY </FONT></td>"; 
    echo "<td><FONT COLOR='FFFFFF'>END DATE</FONT></td>"; 


    echo "</tr>\n"; 
while($rows=mysql_fetch_array($result)) 
    { // If $color==1 table row color = #FFC600 
    if($color==1) 
    { 
     echo "<tr bgcolor='#FFFFDD'> 

     <td>".$rows['CID']."</td><td>".$rows['start_date']."</td><td bgcolor='#99AAFF'><FONT COLOR='FFFFFF'>".$rows['owner']."</FONT></td><td>".$rows['end_date']."</td></tr>"; 
     // Set$color==2, for switching to other color 
     $color="2"; 
     } // When$color not equal 1, use this table row color 
     else 
     { 
      echo "<tr bgcolor='#FFFFEE'><td>".$rows['CID']."</td><td>".$rows['start_date']."</td><td bgcolor='#99AAFF'><FONT COLOR='FFFFFF'>".$rows['owner']."</FONT></td><td>".$rows['end_date']."</td></tr>";// Set $color back to 1 
      $color="1"; 
      } 
      } 
      echo '</table>'; 

      mysql_close(); 
      echo '</tr>'; 
      echo '</table>'; 
    } 
    else 
    { 
     echo '<table border="0" cellpadding="8" align="center">'; 
      echo '<tr><h3 align="center"<font color="white">NoDataFound</font></h3></tr>'; 
      echo '</table>'; 
    } 
    } 

//If we haven't submitted the form 
else 
{ 
?> 

<h3 align="center"<font color="white">REPORT DETAILS</font></h3> 
<table border="1" cellpadding="8" align="center"> 
<br> 
</br> 
<tr><td align="left">Constellation ID</td><td>:</td><td> <input type="text" name="name" /></td></tr> 
<tr><td align="left">Start Date</td><td>:</td><td> <input type="text" name="testinput" /> 
    <script language="JavaScript"> 
    new tcal ({ 
     // form name 
     //'formname': 'testform' 
     // input name 
     'controlname': 'testinput' 
    }); 
    </script> 


    </td></tr> 
<tr><td align="left">End Date</td><td>:</td><td> <input type="text" name="test" /> 
    <script language="JavaScript"> 
    new tcal ({ 
     // form name 
     //'formname': 'testform' 
     // input name 
     'controlname': 'test' 
    }); 
    </script> 


</td></tr> 

</table> 

<br> 
</br> 

<br> 
</br> 
<p align="center"><input type="submit" value="REPORT" name="submitMe"></p> 
<?php 
} 
?> 
</form> 
</body> 
</html> 
當我執行的代碼工作正常,但在的數據都是不相同的頁面顯示

。 當數據顯示頁面正在刷新,所以我不能查看同一頁面中的數據。

+1

請使用代碼標記來設置代碼的格式。 – judda 2011-03-07 05:29:39

+5

此外,刪除您的root用戶名和密碼可能是一個好主意。 – kevmo314 2011-03-07 05:30:48

+0

我想你的代碼做的很好,只是幾個javascripts,只是嘗試縮進和格式化代碼,使其更具可讀性 – 2011-03-07 05:32:56

回答

1

當您開發應用程序,創建帳戶並使用時,您應該從不使用root用戶名和密碼。

如果您的頁面在加載後刷新,則問題必須來自JavaScript。 PHP是服務器端語言,這意味着它可以在用戶輸出之前重定向,但不能在之後重定向。在此發佈您的JS或指向此頁面的鏈接,以便我們可以提供更多幫助。

+0

我願意投注90%的新開發人員不僅使用root賬戶進行開發,而且還在後期進行生產。 MSSQL安裝在企業位置(SA/SA)尤其有趣。 – Jakub 2011-03-07 05:50:14

0

Seconding @ProNeticas - 從來沒有在開發應用程序時使用您的root密碼。

而且你的代碼是開放的SQL injection - 請閱讀mysql_real_escape_string

+0

mysql_ *函數折舊。使用PDO或至少mysqli – Diemuzi 2013-06-16 21:24:43

0

@user ..我只是格式化你的代碼......請認準並應用......然後我們會針對您的問題進行討論.. 。

<?php 
    function GetDays($sStartDate, $sEndDate){ 
     $sStartDate = gmdate("Y-m-d", strtotime($sStartDate)); 
     $sEndDate = gmdate("Y-m-d", strtotime($sEndDate)); 
     $aDays[] = $sStartDate; 
     $sCurrentDate = $sStartDate; 
     while($sCurrentDate < $sEndDate){ 
     $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate))); 
      $aDays[] = $sCurrentDate; 
     } 
     return $aDays; 
    } 
?> 
<html> 
<head> 
    <script type="javascript" src="calendar_us.js"></script> 
    <link rel="stylesheet" href="calendar.css" > 
</head> 
<body background="mainbg.JPG"> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST"> 
<? 
//If we submitted the form 
if(isset($_POST['submitMe'])) 
{ 
    $sec=00; 
    $sc=00; 
    $name=$_POST['name']; 
    $mm=substr($_POST['testinput'],0,2); 
    $dd=substr($_POST['testinput'],3,2); 
    $yy=substr($_POST['testinput'],6,4); 
    $sdate = $yy."-".$mm."-".$dd; 

    $mn=substr($_POST['test'],0,2); 
    $dn=substr($_POST['test'],3,2); 
    $yn=substr($_POST['test'],6,4); 
    $edate = $yn."-".$mn."-".$dn; 

    $host="#######";// Host name 
    $username="#####";// Mysql username 
    $password="######"; // Mysql password 
    $db_name="####"; // Database name 
    $tbl_name="constellation_reserve"; // Table name // Connect to server and selectdatabse 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 
    $sql="SELECT * FROM constellation_reserve where CID=$name and (start_date between '$sdate' and '$edate' or end_date between '$sdate' and '$edate')"; 
    $result=mysql_query($sql); // Define$color=1 
    $fields_num = mysql_num_fields($result); 
    if($result) 
    { ?> 
     <table border="0" cellpadding="8" cellspacing="5" align="center"> 
      <tr> 
       <td><h3 align="center"<font color="white">RESERVATION </font></h3></td> 
      </tr>   
      <tr> 
       <td> 
        <table width="500" height="300" border="0" align="center" cellpadding="8" cellspacing="2">    
        <tr style="color:#fff;background-color:#0099FF"> 
         <td>ID</td> 
         <td>START DATE</td> 
         <td>RESERVED BY</td> 
         <td>END DATE</td> 
        </tr> 
<?php 
      while($rows=mysql_fetch_array($result)) 
      { $color=1; 
       // If $color==1 table row color = #FFC600 
       if($color == 1){ ?> 
        <tr background-color='#FFFFDD'> 
         <td><?php echo $rows['CID']?></td> 
         <td><?php echo $rows['start_date'];?></td> 
         <td style="color:#FFF;background-color:#99AAFF'><?php echo $rows['owner'];?></td> 
         <td><?php echo $rows['end_date']?></td> 
        </tr> 
      <?php $color=2; 
       } else { ?> 
        <tr background-color='#FFFFEE'> 
         <td><?php echo $rows['CID'];?></td> 
         <td><?php echo $rows['start_date'];?></td> 
         <td style="color:#FFF;background-color:#99AAFF'><?php echo $rows['owner'];</td> 
         <td><?php echo $rows['end_date'];?></td> 
        </tr> 
      <?php $color=1; 
       } 
      } ?> 
        </table> 
<?php  mysql_close(); ?> 
       </td> 
      </tr> 
     </table> 
<?php } else { ?> 
     <table border="0" cellpadding="8" align="center"> 
      <tr><h3 align="center"style="color:#fff">NoDataFound</h3></tr> 
     </table> 
<?php } 
} //If we haven't submitted the form 
else { ?> 
<h3 align="center"<font color="white">REPORT DETAILS</font></h3> 
<table border="1" cellpadding="8" align="center"> 
    <tr> 
     <td align="left">Constellation ID</td> 
     <td>:</td> 
     <td> <input type="text" name="name" /></td> 
    </tr> 
    <tr> 
     <td align="left">Start Date</td> 
     <td>:</td> 
     <td> <input type="text" name="testinput" />   
     <script type="javascript"> 
     new tcal ({ 
      // form name 
      //'formname': 'testform' 
      // input name 
      'controlname': 'testinput' 
     }); 
     </script> 
      </td> 
    </tr> 
    <tr> 
     <td align="left">End Date</td> 
     <td>:</td> 
     <td> <input type="text" name="test" /> 
     <script typr"javascript"> 
     new tcal ({ 
      // form name 
      //'formname': 'testform' 
      // input name 
      'controlname': 'test' 
     });  
     </script> 
      </td> 
    </tr> 
</table> 
<p align="center"><input type="submit" value="REPORT" name="submitMe"></p> 
<?php } ?> 
</form> 
</body> 
</html>