<?php 

include 'ipp.php';

$ID = checkID ();

// require an explicit project
if (! $ID['proj']) { projectform ($ID); }

$db = dbconnect($ID['proj']);

if ($ID['menu']) {
  $myMenu = $ID['menu'];
} else {
  $myMenu = "$MENU";
}

menu($myMenu, '$TITLE', '$STYLE', $ID['link'], $ID['proj']);

echo "<p> $TITLE </p>";

// set up the form
echo "<form action=\"$FILE\" method=\"POST\">\n";

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **

// get the result table count
if ($WHERE) {
  $sql = "SELECT count(*) FROM $TABLE WHERE $WHERE";
} else {
  $sql = "SELECT count(*) FROM $TABLE";
}
$qry = $db->query($sql);
if (DB::iserror($db)) {
  echo "<b>error reading $TABLE table count</b><br>\n";
  menu_end();
}
if (!$qry->fetchInto($row)) {
  echo "<b>error reading $TABLE table count</b><br>\n";
  menu_end();
}
// set up the row counter variables
if ($ID['from']) {
  $rowStart = $ID['from'];
} else {
  $rowStart = 0;
}
$rowLast = $rowStart + $dTABLE;
$rowTotal = $row[0];
if ($rowLast > $rowTotal) { $rowLast = $rowTotal; }
echo "<b> $rowStart to $rowLast of $rowTotal items</b><br>\n";

// query the database
if ($WHERE) {
  $sql = "SELECT $FIELDS FROM $TABLE WHERE $WHERE LIMIT $dTABLE OFFSET $rowStart";
} else {
  $sql = "SELECT $FIELDS FROM $TABLE LIMIT $dTABLE OFFSET $rowStart";
}

$qry = $db->query($sql);
if (DB::iserror($db)) {
  echo "<b>error reading $TABLE table</b><br>\n";
  menu_end();
}

// ** HEAD CODE **

if ($rowStart > 0) {
  $value = 0;
  $link = "$FILE" . "?" . $ID['link'] . "&from=$value";
  echo "<a width=10 class=\"button\" href=\"$link\">&#60&#60</a>\n";
} else {
  echo "&#60&#60\n";
}
if ($rowStart >= $dTABLE) {
  $value = $rowStart - $dTABLE;
  $link = "$FILE" . "?" . $ID['link'] . "&from=$value";
  echo "<a class=\"button\" href=\"$link\">&#60</a>\n";
} else {
  echo "&#60\n";
}

if ($rowLast < $rowTotal) {
  $value = $rowStart + $dTABLE;
  $link = "$FILE" . "?" . $ID['link'] . "&from=$value";
  echo "<a class=\"button\" href=\"$link\">&#62</a>\n";
} else {
  echo "&#62\n";
}

if ($rowLast < $rowTotal) {
  $value = $rowTotal - $dTABLE;
  $link = "$FILE" . "?" . $ID['link'] . "&from=$value";
  echo "<a class=\"button\" href=\"$link\">&#62&#62</a>\n";
} else {
  echo "&#62&#62\n";
}

echo "&nbsp; : &nbsp; or enter start row: <input type=\"text\" name=\"from\" size=\"5\" value=\"$rowStart\">\n";

// set up the table
echo "<table class=list>\n";
echo "<tr>\n";
// ** TABLE HEADER **
// echo "<th class=list> FIELD </th>\n";
echo "</tr>\n";

// query restriction form
echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// ** TABLE QUERY **
echo "</tr>\n";

// list the results
while ($qry->fetchInto($row)) {
  // $link = "$FILE" . "?expID=" . $row[0] . "&" . $ID['link'];

  echo "<tr>\n";
  // ** TABLE DATA **
  // echo "<td class=list><a href=\"$link\"> $row[0] </a></td>\n";
  // echo "<td class=list> $row[1] </td>\n";
  echo "</tr>\n";
}

// query restriction form
// echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// TABLE QUERY 
// echo "</tr>\n";

// close the table and form
echo "</table>\n";
echo "<input type=\"submit\" name=\"constraints\" value=\"refine search\">\n";
$pass = $ID['pass'];
$proj = $ID['proj'];
$menu = $ID['menu'];
echo "<input type=\"hidden\" name=\"pass\" value=\"$pass\">\n";
echo "<input type=\"hidden\" name=\"proj\" value=\"$proj\">\n";
echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
echo "</form>\n";

// ** TAIL CODE **

echo "<small> WHERE: $WHERE<br><br></small>\n";
echo "<small> SQL: $sql<br><br></small>\n";

menu_end();

?>
