<?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 ($MODE == "basic") {
  $sql = "SELECT count(*) FROM $TABLE $WHERE";
}
if ($MODE == "summary") {
  $sql = "SELECT count(*) FROM (SELECT $FIELDS FROM $TABLE $WHERE) as TEMP";
}

$qry = $db->query($sql);
if (DB::isError($qry)) {
  echo "<b>error reading $TABLE table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><br>\n";
  menu_end();
}
if (!$qry->fetchInto($row)) {
  echo "<b>error reading $TABLE table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><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 ($MODE == "basic") {
  $sql = "SELECT $FIELDS FROM $TABLE $WHERE LIMIT $dTABLE OFFSET $rowStart";
}
if ($MODE == "summary") {
  $sql = "SELECT $FIELDS FROM $TABLE $WHERE LIMIT $dTABLE OFFSET $rowStart";
}

$qry = $db->query($sql);
if (DB::isError($qry)) {
  echo "<b>error reading $TABLE table</b><br>\n";
  echo "<br><small><b> table query : $sql </b></small><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><td></td>\n"; // first field is a label set below for the query rows only
// ** 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><td></td>\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();

?>
