<?php
require("includes/global.php");

$id = @$_GET['id'];

if ($id) {
 $query = mysqli_query($connection,"SELECT * FROM `users` WHERE id='$id'");
 $q = mysqli_fetch_array($query);
 if (!$q) {
  header("Location: index.php");
	exit;
 }
 $pageInfo['title'] = "View Profile: ".$q['username'];
 $pageInfo['homelink'] .= "  <a href=\"members.php?id={$id}\">View Profile</a>";
 fetchTemplate("header");
 fetchTemplate("profile");
 fetchTemplate("footer");
}
else {
 $pageInfo['title'] = "Members List";
 $pageInfo['homelink'] .= "  <a href=\"members.php\">Members List</a>";
 fetchTemplate("header");

 $maxPerPage = $pageInfo['maxPerPage'];
 $p = @$_GET['p'] ? @$_GET['p'] : 1;
 $offset = ($p * $maxPerPage) - $maxPerPage;

 $oldquery = mysqli_query($connection,"SELECT * FROM `users`");
 $oldcount = mysqli_num_rows($oldquery)
 ?>
<div class="clearfix"></div>
<div class="pagenation">
<div class="pagenation_inner">
<?php
 $pagecount = ceil($oldcount / $maxPerPage);
 echo "<span class='page'>Page:</span> ";
 if ($p > 1) {
  $prev = $p - 1;
  echo "<span class='Previous'><a href=\"?p={$prev}\">Previous</a></span>\n";
 }
 for ($i=0; $i<$pagecount; $i++) {
  if (($i+1) == $p) {
   echo "<b>{$p}</b>\n";
  }
  else {
   echo "<a href=\"?p=" . ($i+1) . "\">" . ($i+1) . "</a>\n";
  }
 }
 if ($p < $pagecount) {
  $prev = $p + 1;
  echo "<span class='next'><a href=\"?p={$prev}\">Next</a></span>\n";
 }
 ?>
 </div>
</div>
<div class="clearfix"></div>

 <?php

 $query = mysqli_query($connection,"SELECT * FROM `users` ORDER BY id ASC LIMIT $offset,$maxPerPage");
 fetchTemplate("memberstop");
 while ($q = mysqli_fetch_array($query)) {
  fetchTemplate("memberlist");
 }
 fetchTemplate("membersend");
?>
<div class="clearfix"></div>
<div class="pagenation">
<div class="pagenation_inner">
 
 <?php
 $pagecount = ceil($oldcount / $maxPerPage);
 echo "<span class='page'>Page:</span>";
 if ($p > 1) {
  $prev = $p - 1;
  echo "<span class='previous'><a href=\"?p={$prev}\">Previous</a></span>\n";
 }
 for ($i=0; $i<$pagecount; $i++) {
  if (($i+1) == $p) {
   echo "<b>{$p}</b>\n";
  }
  else {
   echo "<a href=\"?p=" . ($i+1) . "\">" . ($i+1) . "</a>\n";
  }
 }
 if ($p < $pagecount) {
  $prev = $p + 1;
  echo "<span class='next'><a href=\"?p={$prev}\">Next</a></span>\n";
 }
 ?>
 
 
 </div>
</div>
<div class="clearfix"></div>
 
 <?php 

 fetchTemplate("footer");
}
?>