<?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
$sql = "SELECT count(*) FROM $TABLE $WHERE";
// echo "<br><small><b> count query : $sql </b></small><br>\n";

$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
$sql = "SELECT $FIELDS FROM $TABLE $WHERE LIMIT $dTABLE OFFSET $rowStart";
// echo "<br><small><b> table query : $sql </b></small><br>\n";

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

// ** HEAD CODE **

// ** BUTTON RESTRICTIONS **
navigate_buttons ($rowStart, $rowLast, $dTABLE, $rowTotal, $buttonLink, $ID, '$FILE');

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 "</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'];

  $class = "list";
  // ** TD CLASS **

  echo "<tr>\n";
  // ** TABLE DATA **
  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();

?>
