<?php session_start();
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  
  if (isset($_POST["filter1"])) $filter1 = @$_POST["filter1"];
  if (isset($_POST["filter_field1"])) $filterfield1 = @$_POST["filter_field1"];
  
  if (isset($_POST["filter2"])) $filter2 = @$_POST["filter2"];
  if (isset($_POST["filter_field2"])) $filterfield2 = @$_POST["filter_field2"];
  
  
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];

  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];
  
  if (!isset($filter1) && isset($_SESSION["filter1"])) $filter1 = $_SESSION["filter1"];
  if (!isset($filterfield1) && isset($_SESSION["filter_field1"])) $filterfield1 = $_SESSION["filter_field1"];
  
  if (!isset($filter2) && isset($_SESSION["filter2"])) $filter2 = $_SESSION["filter2"];
  if (!isset($filterfield2) && isset($_SESSION["filter_field2"])) $filterfield2 = $_SESSION["filter_field2"];

?>

<html>
<head>
<title>ceulac_sadce -- oferta_academica</title>
<meta name="generator" http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    background-color: #FFFFFF01;
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    background-color: #FFFFFF01;
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    background-color: #FFFFFF01;
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Gesti&oacute;n de Oferta Acad&eacute;mica</h2></td></tr></table>
<?php
  $conn = connect();
  $showrecs = 30;
  $pagerange = 30;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  
  if (isset($filter1)) $_SESSION["filter1"] = $filter1;
  if (isset($filterfield1)) $_SESSION["filter_field1"] = $filterfield1;
  
  if (isset($filter2)) $_SESSION["filter2"] = $filter2;
  if (isset($filterfield2)) $_SESSION["filter_field2"] = $filterfield2;
  
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $filter1;
  global $filterfield1;
  global $filter2;
  global $filterfield2;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
	$filter1 = "";
    $filterfield1 = "";
    $filter2 = "";
    $filterfield2 = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }
   mysql_query ("SET NAMES 'utf8'");
  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<form action="oferta_academica_admin.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Filtrar por</b>&nbsp;</td>
<td><input type="text" name="filter" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">Todos los Campos</option>
<option value="<?php echo "nperiodo" ?>"<?php if ($filterfield == "nperiodo") { echo "selected"; } ?>><?php echo htmlspecialchars("Período") ?></option>
<option value="<?php echo "nsede" ?>"<?php if ($filterfield == "nsede") { echo "selected"; } ?>><?php echo htmlspecialchars("Sede") ?></option>
<option value="<?php echo "id_seccion" ?>"<?php if ($filterfield == "id_seccion") { echo "selected"; } ?>><?php echo htmlspecialchars("Sección") ?></option>
<option value="<?php echo "id_carrera" ?>"<?php if ($filterfield == "id_carrera") { echo "selected"; } ?>><?php echo htmlspecialchars("Programa") ?></option>
<option value="<?php echo "nmateria" ?>"<?php if ($filterfield == "nmateria") { echo "selected"; } ?>><?php echo htmlspecialchars("Unidad Curricular") ?></option>
</select></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="text" name="filter1" value="<?php echo $filter1 ?>"></td>
<td><select name="filter_field1">
<option value="">Todos los Campos</option>
<option value="<?php echo "nperiodo" ?>"<?php if ($filterfield1 == "nperiodo") { echo "selected"; } ?>><?php echo htmlspecialchars("Período") ?></option>
<option value="<?php echo "nsede" ?>"<?php if ($filterfield1 == "nsede") { echo "selected"; } ?>><?php echo htmlspecialchars("Sede") ?></option>
<option value="<?php echo "id_seccion" ?>"<?php if ($filterfield1 == "id_seccion") { echo "selected"; } ?>><?php echo htmlspecialchars("Sección") ?></option>
<option value="<?php echo "id_carrera" ?>"<?php if ($filterfield1 == "id_carrera") { echo "selected"; } ?>><?php echo htmlspecialchars("Programa") ?></option>
<option value="<?php echo "nmateria" ?>"<?php if ($filterfield1 == "nmateria") { echo "selected"; } ?>><?php echo htmlspecialchars("Unidad Curricular") ?></option>
</select>
</td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Palabra Completa</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="text" name="filter2" value="<?php echo $filter2 ?>"></td>
<td><select name="filter_field2">
<option value="">Todos los Campos</option>
<option value="<?php echo "nperiodo" ?>"<?php if ($filterfield2 == "nperiodo") { echo "selected"; } ?>><?php echo htmlspecialchars("Período") ?></option>
<option value="<?php echo "nsede" ?>"<?php if ($filterfield2 == "nsede") { echo "selected"; } ?>><?php echo htmlspecialchars("Sede") ?></option>
<option value="<?php echo "id_seccion" ?>"<?php if ($filterfield2 == "id_seccion") { echo "selected"; } ?>><?php echo htmlspecialchars("Sección") ?></option>
<option value="<?php echo "id_carrera" ?>"<?php if ($filterfield2 == "id_carrera") { echo "selected"; } ?>><?php echo htmlspecialchars("Programa") ?></option>
<option value="<?php echo "nmateria" ?>"<?php if ($filterfield2 == "nmateria") { echo "selected"; } ?>><?php echo htmlspecialchars("Unidad Curricular") ?></option>
</select></td>
<td><input type="submit" name="action" value="Aplicar Filtro"></td>
<td><a href="oferta_academica_admin.php?a=reset">Quitar Filtro</a></td>

</td></tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr"><a class="hr" href="oferta_academica_admin.php?order=<?php echo "nperiodo" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Período") ?></a></td>
<td class="hr"><a class="hr" href="oferta_academica_admin.php?order=<?php echo "nsede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Sede") ?></a></td>
<td class="hr"><a class="hr" href="oferta_academica_admin.php?order=<?php echo "id_seccion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Sección") ?></a></td>
<td class="hr"><a class="hr" href="oferta_academica_admin.php?order=<?php echo "id_carrera" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("") ?></a></td>
<td class="hr"><a class="hr" href="oferta_academica_admin.php?order=<?php echo "ncarrera" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Programa") ?></a></td>
<td class="hr"><a class="hr" href="oferta_academica_admin.php?order=<?php echo "nmateria" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Unidad Curricular") ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?>"><a href="oferta_academica_admin.php?a=view&recid=<?php echo $i ?>">Ver</a></td>
<td class="<?php echo $style ?>"><a href="oferta_academica_admin.php?a=edit&recid=<?php echo $i ?>">Editar</a></td>
<td class="<?php echo $style ?>"><a href="oferta_academica_admin.php?a=del&recid=<?php echo $i ?>">Borrar</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nperiodo"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nsede"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_seccion"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_carrera"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["ncarrera"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nmateria"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("nperiodo")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nperiodo"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nsede")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nsede"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_seccion")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_seccion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ncarrera")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["ncarrera"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nmateria")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nmateria"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("id")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id" value="<?php echo str_replace('"', '&quot;', trim($row["id"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_periodo")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_periodo" value="<?php echo str_replace('"', '&quot;', trim($row["id_periodo"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nperiodo")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nperiodo" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["nperiodo"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("sede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="sede" maxlength="2" value="<?php echo str_replace('"', '&quot;', trim($row["sede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nsede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nsede" maxlength="20" value="<?php echo str_replace('"', '&quot;', trim($row["nsede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_seccion")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_seccion" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["id_seccion"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_carrera")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_carrera" maxlength="5" value="<?php echo str_replace('"', '&quot;', trim($row["id_carrera"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("ncarrera")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="ncarrera" maxlength="160"><?php echo str_replace('"', '&quot;', trim($row["ncarrera"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("semestre")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="semestre" value="<?php echo str_replace('"', '&quot;', trim($row["semestre"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_materia")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_materia" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nmateria")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="nmateria" maxlength="150"><?php echo str_replace('"', '&quot;', trim($row["nmateria"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("horario")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="horario" maxlength="50" value="<?php echo str_replace('"', '&quot;', trim($row["horario"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_profesor")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_profesor" value="<?php echo str_replace('"', '&quot;', trim($row["id_profesor"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nprofesor")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nprofesor" maxlength="30" value="<?php echo str_replace('"', '&quot;', trim($row["nprofesor"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cupos")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="cupos" value="<?php echo str_replace('"', '&quot;', trim($row["cupos"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("inscritos")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="inscritos" value="<?php echo str_replace('"', '&quot;', trim($row["inscritos"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("annio")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="annio" value="<?php echo str_replace('"', '&quot;', trim($row["annio"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("preinscritos")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="preinscritos" value="<?php echo str_replace('"', '&quot;', trim($row["preinscritos"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("turno")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="turno" maxlength="8" value="<?php echo str_replace('"', '&quot;', trim($row["turno"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="oferta_academica_admin.php?a=add">Agregar </a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="oferta_academica_admin.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Anterior</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="oferta_academica_admin.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="oferta_academica_admin.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="oferta_academica_admin.php?page=<?php echo $page + 1 ?>">Siguiente&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="oferta_academica_admin.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="oferta_academica_admin.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="oferta_academica_admin.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Siguiente Registro</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="oferta_academica_admin.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="oferta_academica_admin.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id" => "",
  "id_periodo" => "",
  "nperiodo" => "",
  "sede" => "",
  "nsede" => "",
  "id_seccion" => "",
  "id_carrera" => "",
  "ncarrera" => "",
  "semestre" => "",
  "id_materia" => "",
  "nmateria" => "",
  "horario" => "",
  "id_profesor" => "",
  "nprofesor" => "",
  "cupos" => "",
  "inscritos" => "",
  "annio" => "",
  "preinscritos" => "",
  "turno" => "");
showroweditor($row, false);
?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="oferta_academica_admin.php?a=add">Agregar </a></td>
<td><a href="oferta_academica_admin.php?a=edit&recid=<?php echo $recid ?>">Editar </a></td>
<td><a href="oferta_academica_admin.php?a=del&recid=<?php echo $recid ?>">Borrar </a></td>
</tr>
</table>
<?php
  mysql_free_result($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="oferta_academica_admin.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="oferta_academica_admin.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirmar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysql_connect("localhost", "ceula_sadce_admi", "j7BnJdCGzyQBGcud");
  mysql_select_db("ceulac_sadce");
   mysql_query ("SET NAMES 'utf8'");
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $filter1;
  global $filterfield1;
  global $filter2;
  global $filterfield2;
   mysql_query ("SET NAMES 'utf8'");

  $filterstr = sqlstr($filter);
  $filterstr1 = sqlstr($filter1); 
  $filterstr2 = sqlstr($filter2);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT `id`, `id_periodo`, `nperiodo`, `sede`, `nsede`, `id_seccion`, `id_carrera`, `ncarrera`, `semestre`, `id_materia`, `nmateria`, `horario`, `id_profesor`, `nprofesor`, `cupos`, `inscritos`, `annio`, `preinscritos`, `turno` FROM `oferta_academica`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
	if (isset($filterstr1) && $filterstr1!='' && isset($filterfield1) && $filterfield1!='') {
	     $sql .= " and " .sqlstr($filterfield1) ." like '" .$filterstr1 ."'";
		 if (isset($filterstr2) && $filterstr2!='' && isset($filterfield2) && $filterfield2!='') {
		    $sql .= " and " .sqlstr($filterfield2) ." like '" .$filterstr2 ."'";
		 }
	}
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`nperiodo` like '" .$filterstr ."') or (`nsede` like '" .$filterstr ."') or (`id_seccion` like '" .$filterstr ."') or (`id_carrera` like '" .$filterstr ."') or (`nmateria` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
   global $filter1;
  global $filterfield1;
  global $filter2;
  global $filterfield2;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  $filterstr1 = sqlstr($filter1);
  $filterstr2 = sqlstr($filter2);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `oferta_academica`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
      $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
      if (isset($filterstr1) && $filterstr1!='' && isset($filterfield1) && $filterfield1!='') {
         $sql .= " and " .sqlstr($filterfield1) ." like '" .$filterstr1 ."'";
		 if (isset($filterstr2) && $filterstr2!='' && isset($filterfield2) && $filterfield2!='') {
		    $sql .= " and " .sqlstr($filterfield2) ." like '" .$filterstr2 ."'";
		 }
	  } 
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`nperiodo` like '" .$filterstr ."') or (`nsede` like '" .$filterstr ."') or (`id_seccion` like '" .$filterstr ."') or (`id_carrera` like '" .$filterstr ."') or (`nmateria` like '" .$filterstr ."')";
	
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;

  $sql = "insert into `oferta_academica` (`id`, `id_periodo`, `nperiodo`, `sede`, `nsede`, `id_seccion`, `id_carrera`, `ncarrera`, `semestre`, `id_materia`, `nmateria`, `horario`, `id_profesor`, `nprofesor`, `cupos`, `inscritos`, `annio`, `preinscritos`, `turno`) values (" .sqlvalue(@$_POST["id"], false).", " .sqlvalue(@$_POST["id_periodo"], false).", " .sqlvalue(@$_POST["nperiodo"], true).", " .sqlvalue(@$_POST["sede"], true).", " .sqlvalue(@$_POST["nsede"], true).", " .sqlvalue(@$_POST["id_seccion"], true).", " .sqlvalue(@$_POST["id_carrera"], true).", " .sqlvalue(@$_POST["ncarrera"], true).", " .sqlvalue(@$_POST["semestre"], false).", " .sqlvalue(@$_POST["id_materia"], true).", " .sqlvalue(@$_POST["nmateria"], true).", " .sqlvalue(@$_POST["horario"], true).", " .sqlvalue(@$_POST["id_profesor"], false).", " .sqlvalue(@$_POST["nprofesor"], true).", " .sqlvalue(@$_POST["cupos"], false).", " .sqlvalue(@$_POST["inscritos"], false).", " .sqlvalue(@$_POST["annio"], true).", " .sqlvalue(@$_POST["preinscritos"], false).", " .sqlvalue(@$_POST["turno"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update `oferta_academica` set `id`=" .sqlvalue(@$_POST["id"], false).", `id_periodo`=" .sqlvalue(@$_POST["id_periodo"], false).", `nperiodo`=" .sqlvalue(@$_POST["nperiodo"], true).", `sede`=" .sqlvalue(@$_POST["sede"], true).", `nsede`=" .sqlvalue(@$_POST["nsede"], true).", `id_seccion`=" .sqlvalue(@$_POST["id_seccion"], true).", `id_carrera`=" .sqlvalue(@$_POST["id_carrera"], true).", `ncarrera`=" .sqlvalue(@$_POST["ncarrera"], true).", `semestre`=" .sqlvalue(@$_POST["semestre"], false).", `id_materia`=" .sqlvalue(@$_POST["id_materia"], true).", `nmateria`=" .sqlvalue(@$_POST["nmateria"], true).", `horario`=" .sqlvalue(@$_POST["horario"], true).", `id_profesor`=" .sqlvalue(@$_POST["id_profesor"], false).", `nprofesor`=" .sqlvalue(@$_POST["nprofesor"], true).", `cupos`=" .sqlvalue(@$_POST["cupos"], false).", `inscritos`=" .sqlvalue(@$_POST["inscritos"], false).", `annio`=" .sqlvalue(@$_POST["annio"], true).", `preinscritos`=" .sqlvalue(@$_POST["preinscritos"], false).", `turno`=" .sqlvalue(@$_POST["turno"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `oferta_academica` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id`";
  if (@$_POST["xid"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid"], false);
  };
  $pk .= ")";
  return $pk;
}
 ?>
