Jump to content

Need help adding search functionality


jedblack

Recommended Posts

Hi guys/gals

 

I'm a seasoned shell scripter and can hack together some perl code, but my php knowledge is limited.

 

I have a page that polls a database containing server patch records -- I'm not the original author.

 

when the page loads, it basicly just dumps all row from the table in our database.  I'd like to change two things

 

1) stop the page from loading all records by defaut

2) add search boxes for "patchid, age, flags, host, date" (flags can be a drop down menu since there will only be 4 possiblities).  The search boxes can be in the same area as the "custom filter" field

 

I was going to try and code a new page, but I dont want to re-invent the wheel, and the current layout looks nice.

 

Thanks for any help guys...I really appreciate it...

 

<?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"];
  $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"];

?>

<html>
<head>
<title>Patch Reporting System</title>
<meta name="generator" http-equiv="content-type" content="text/html">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #5F605F;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #5F605F;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #CFC890;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #5F605F;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #5F605F;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Patch Reporting System</h2></td></tr></table>
<?php
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;

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

  switch ($a) {
    case "view":
      viewrec($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($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

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

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


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $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);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Table: patch</td></tr>
<tr><td>Records shown <?php echo $startrec + 1 ?> - <?php echo $reccount ?> of <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="patchdev.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Custom Filter</b> </td>
<td><input type="text" name="filter" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">All Fields</option>
<option value="<?php echo "Patchid" ?>"<?php if ($filterfield == "Patchid") { echo "selected"; } ?>><?php echo htmlspecialchars("Patchid") ?></option>
<option value="<?php echo "InstalledRev" ?>"<?php if ($filterfield == "InstalledRev") { echo "selected"; } ?>><?php echo htmlspecialchars("InstalledRev") ?></option>
<option value="<?php echo "CurrentRev" ?>"<?php if ($filterfield == "CurrentRev") { echo "selected"; } ?>><?php echo htmlspecialchars("CurrentRev") ?></option>
<option value="<?php echo "Age" ?>"<?php if ($filterfield == "Age") { echo "selected"; } ?>><?php echo htmlspecialchars("Age") ?></option>
<option value="<?php echo "Synopsis" ?>"<?php if ($filterfield == "Synopsis") { echo "selected"; } ?>><?php echo htmlspecialchars("Synopsis") ?></option>
<option value="<?php echo "Flags" ?>"<?php if ($filterfield == "Flags") { echo "selected"; } ?>><?php echo htmlspecialchars("Flags") ?></option>
<option value="<?php echo "host" ?>"<?php if ($filterfield == "host") { echo "selected"; } ?>><?php echo htmlspecialchars("host") ?></option>
<option value="<?php echo "date" ?>"<?php if ($filterfield == "date") { echo "selected"; } ?>><?php echo htmlspecialchars("date") ?></option>
</select></td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Whole words only</td>
</td></tr>
<tr>
<td> </td>
<td><input type="submit" name="action" value="Apply Filter"></td>
<td><a href="patchdev.php?a=reset">Reset Filter</a></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"> </td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "Patchid" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Patchid") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "InstalledRev" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("InstalledRev") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "CurrentRev" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("CurrentRev") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "Age" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Age") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "Synopsis" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Synopsis") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "Flags" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Flags") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "host" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("host") ?></a></td>
<td class="hr"><a class="hr" href="patchdev.php?order=<?php echo "date" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("date") ?></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="patchdev.php?a=view&recid=<?php echo $i ?>">View</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Patchid"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["InstalledRev"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["CurrentRev"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Age"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Synopsis"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Flags"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["host"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["date"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Patchid")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["Patchid"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("InstalledRev")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["InstalledRev"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("CurrentRev")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["CurrentRev"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Age")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["Age"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Synopsis")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["Synopsis"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Flags")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["Flags"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("host")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["host"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("date")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["date"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<?php if ($page > 1) { ?>
<td><a href="patchdev.php?page=<?php echo $page - 1 ?>"><< Prev</a> </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="patchdev.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="patchdev.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td> <a href="patchdev.php?page=<?php echo $page + 1 ?>">Next >></a> </td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="patchdev.php">Index Page</a></td>
<?php if ($recid > 0) { ?>
<td><a href="patchdev.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Prior Record</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="patchdev.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Next Record</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?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) ?>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysql_connect("fxssun", "pca", "pca");
  mysql_select_db("pca");
  return $conn;
}

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

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT `Patchid`, `InstalledRev`, `CurrentRev`, `Age`, `Synopsis`, `Flags`, `host`, `date` FROM `patch`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`Patchid` like '" .$filterstr ."') or (`InstalledRev` like '" .$filterstr ."') or (`CurrentRev` like '" .$filterstr ."') or (`Age` like '" .$filterstr ."') or (`Synopsis` like '" .$filterstr ."') or (`Flags` like '" .$filterstr ."') or (`host` like '" .$filterstr ."') or (`date` 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 $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `patch`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`Patchid` like '" .$filterstr ."') or (`InstalledRev` like '" .$filterstr ."') or (`CurrentRev` like '" .$filterstr ."') or (`Age` like '" .$filterstr ."') or (`Synopsis` like '" .$filterstr ."') or (`Flags` like '" .$filterstr ."') or (`host` like '" .$filterstr ."') or (`date` like '" .$filterstr ."')";
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
} ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.