<?php require_once('Connections/conn.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysqli_real_escape_string($conn,$theValue) : mysqli_escape_string($conn,$theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


$currentPage = $_SERVER["PHP_SELF"];
$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;

$Recordset1 =$queryresult;
$row_Recordset1 =$fetch;
$totalRows_Recordset1 =$num_rows;

if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysqli_select_db($conn,$database_conn);
$query_Recordset1 = "select * from gbook";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysqli_query($conn,$query_limit_Recordset1 ) or die(mysqli_error());
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysqli_query($conn,$query_Recordset1);
  $totalRows_Recordset1 = mysqli_num_rows($all_Recordset1);
}
//這句有誤$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1 = 10;
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1);							  
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

//----------------------------------------------------------------
mysqli_select_db($conn,$database_conn);
$query_Recordset1 = "select * from gbook";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysqli_query($conn,$query_limit_Recordset1) or die(mysqli_error());
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysqli_query($conn,$query_Recordset1);
  $totalRows_Recordset1 = mysqli_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>

<style type="text/css">
<!--
@import url("CSS/headcss.css");
-->
	
</style>
<script src="JS/jquery-1.12.4.min.js"></script>	
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>	
	
</head>


	
	
	
	
<body>
	
	<?php
    echo "<center>";
    echo "<table border = 1 cellspacing = '0' cellpadding = '10' width='100%' bgcolor='#FFFFFF' id='guestForm'>";
	echo "<tr><td colspan='8' class='headcss'>留言板</td></tr>";
	echo "<tr><td>編號</td><td>姓名</td><td>圖像</td><td>主旨</td><td>留言內容</td><td>日期</td><td>";
	echo "留言者IP</td><td></td></tr>";
	$limit=10;
	$Select_db=mysqli_select_db($conn,$database_conn) or die($dbError2);
    $queryresult=mysqli_query($conn,$SqlStr);
    $num_rows=mysqli_num_rows($queryresult);//記錄資料數
	$pagecount = ceil($num_rows/$limit);//記錄分頁數
	
	//$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
	// Check if the page number is specified and check if it's a number, if not return the default page number which is 1.
	
    $page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
	$start = ($page - 1) * $limit; // set the page to 0
	
	if (isset($_GET['pageNum_Recordset1'])) {
        $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
       }
    $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
	$PageSQLStr="SELECT * FROM gbook ORDER BY msg_ID,userDate DESC limit $startRow_Recordset1  , $limit ";
	//$PageSQLStr="SELECT * FROM gbook ORDER BY msg_ID,userDate DESC limit ? , ? ";
	//$stmt=$conn->prepare($PageSQLStr);
	//$startVal=$startRow_Recordset1 + 1;
	//$endVal=min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1);
	//$stmt->bind_param("ii",$startVal,$endVal);
	//$stmt->execute();
    $newquery = mysqli_query($conn, $PageSQLStr);
	//echo $start." ".$startRow_Recordset1." ".$PageSQLStr." ".mysqli_fetch_assoc($stmt);
    while($row_Recordset1 = mysqli_fetch_assoc($newquery)){
            echo "<tr>";
            echo "<td id='IDnum'>".$row_Recordset1['msg_ID']."</td>";
            echo '<td>'.$row_Recordset1['userName'].'</td>';
            echo '<td>'.$row_Recordset1['userIcon'].'</td>';
            echo '<td>'.$row_Recordset1['msgSubject'].'</td>';
            echo '<td>'.$row_Recordset1['msgContent'].'</td>';
            echo '<td>'.$row_Recordset1['userDate'].'</td>';
	        echo '<td>'.$row_Recordset1['userIP'].'</td>';
		    echo "<td><input class='SelB' type='button' value='修改'/> / <input class='DelB' type='button' value='刪除'/></td>";
            echo "</tr>";
        }
	
        //echo "<a href='guestForm.php?page=1'>".'|<'."</a> "; // 第一页
	    //for ($i=1; $i<=$pagecount; $i++) { 
            //echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
           //}; 
        //echo "<a href='guestForm.php?page=$pagecount'>".'>|'."</a> "; // 最后一页
	    
	?>
	<div class="myClass">div class="myClass"</div>
	<input type="button" class="myClass" value="修改"/>
	<tr valign="baseline">
    <td nowrap="nowrap" align="right">&nbsp;</td>
    <td><input type="button" value="更新" onclick='myrefresh()'/></td><td colspan="6"> </td></tr>
	
	<table border="0">
  <tr>
    <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>"><img src="image/First.gif" border="0" /></a>
        <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>"><img src="image/Previous.gif" border="0" /></a>
        <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>"><img src="image/Next.gif" border="0" /></a>
        <?php } // Show if not last page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>"><img src="image/Last.gif" border="0" /></a>
        <?php } // Show if not last page ?>
    </td>
	<td> 
		Records <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?>
	</td>  
  </tr>
		
</table>
	
	
	
	
	
	
	<script language="JavaScript">	
	function myrefresh(){
      window.location.reload();
      }
    //setTimeout('myrefresh()',1000); //指定1秒刷新一次
	
    </script>
	<script>
		$( ".myClass" ).css( "border", "3px solid red" );	
		$(".SelB").click(function(){
		
		alert "";
		echo "1111111111";
		var seltr=$(this).closest("tr");
		var targetID=seltr.find("td:eq(0)").text();
		window.alert (targetID);
	});
	$('.DelD').click(function(){
		window.alert "";
		var seltr=$(this).closest("tr");
		var targetID=seltr.find("td:eq(0)").text();
		alert (targetID);
	});
	</script>
    

</body>
</html>
<?php
	//釋放MYSQLI內存
mysqli_free_result($Recordset1);
?>