Guest User

php search

a guest
Sep 12th, 2014
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['sess_user_id']) || (trim($_SESSION['sess_user_id']) == '')) {
  4.     header("location: login.php");
  5.     exit();
  6. }
  7. if(!($_SESSION['sess_status'] > 0)) {
  8.     header("location: sorry.php");
  9. }
  10.  
  11.  
  12. $whereClause = array();
  13. $countyList = array();
  14. $bindArray = array();
  15.  
  16. $fields = array( 'bhv','mrc','general','evv','allCounties','fillmore','johnson','richardson', 'otoe','seward','butler','gage','lancaster','pawnee','saline','thayer','cass','jefferson','nemaha','polk','saunders','york','aro','clergy','intskills','bus','child','law','cpr','data','security','emergency','computer','mechanical','administration','firstaid','translation','construction','basicclean','foodprep','animalcare','heavy','license1','license2','license3','license4','license5','dist1','dist2','dist3','dist4','dist5');
  17. $counties = array('county1','county2','county3','county4','countr5','county6');
  18. foreach($fields as $field)
  19. {
  20.     if($san = filter_input(INPUT_POST, $field, FILTER_SANITIZE_STRING)){
  21.         $whereClause[] = "`$field` = :$field";
  22.         $bindArray[":$field"] =  $san;  
  23.     }
  24. }
  25. foreach($counties as $county)
  26. {
  27.     if($san = filter_input(INPUT_POST, $county, FILTER_SANITIZE_STRING)){
  28.          $countyList[] = ":$county";
  29.          $bindArray[":$county"] = $san;
  30.     }
  31. }
  32. $sql = "SELECT * FROM VolunDB";
  33. if(!empty($countyList)) $sqlC = '`county` IN(' . implode(',', $countyList) . ')';
  34. if(!empty($whereClause)) $sqlW = implode(' AND ', $whereClause);
  35. $stmtString = $sql;
  36. if(isset($sqlC) && isset($sqlW)){
  37.     $stmtString .= ' WHERE ' . $sqlC . ' AND ' . $sqlW;
  38. }elseif(isset($sqlC)){
  39.     $stmtString .= ' WHERE ' . $sqlC;
  40. }elseif(isset($sqlW)){
  41.     $stmtString .= ' WHERE ' . $sqlW;
  42. }
  43.  
  44.  
  45.  
  46. $dbh = new PDO('mysql:host=localhost;dbname=petrzilk_test;charset=utf8', 'petrzilk_dbAdmin', '');
  47. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  48. $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  49. $stmt = $dbh->prepare($stmtString);
  50. $stmt->execute($bindArray);
  51. $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  52. //foreach($data as $person)
  53. //{
  54. //    echo $person['lname'] . ', ' . $person['fname'] . '<br />';
  55. //}
  56. ?>
  57. <!DOCTYPE HTML>
  58. <html>
  59.     <head>
  60.         <title>Search Results</title>
  61.         <script type="text/javascript" src="js/functions.js"> </script>
  62.         <link rel="stylesheet" href="css/bootstrap-theme.css" type="text/css" />
  63.         <link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
  64.         <link rel="stylesheet" href="css/styles.css" type="text/css">
  65.         <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
  66.     </head>
  67.     <div class="sidebar">
  68.             <ul class="nav nav-pills nav-stacked">
  69.                 <li><br/></li>
  70.                 <li><img class='logo' src='images/Large RVS.png' alt='Broken' style='width:230px; padding-left: 5%;'/></li>
  71.                 <li><br/></li>
  72.                 <li><span class="welcome" style="color:white;font-weight:Bold;font-size:20px;margin-left:5px;padding-left:5px;"> Welcome  <?php echo $_SESSION['sess_name'] ?></span></li>
  73.                 <li><a href="login.php">Log out</a></li>
  74.                 <li><a href="index.php">Home</a><li>
  75.                 <li><a href="volunForm.php">Add Volunteer</a></li>
  76.                 <li><a href="search.php">Search/Report</a></li>
  77.                 <li class="active"><a href="allVolun.php">View Volunteers</a></li>
  78.                 <li><a href="updateVolunteer.php">Update Volunteer</a></li>
  79.                  <?php
  80.                 if ($_SESSION['sess_status'] == 2) {
  81.                     echo "<li><a href='admin.php'>Admin CP</a></li>";
  82.                     }
  83.                 ?>
  84.             </ul>
  85.         </div>
  86.         <div class="content">
  87.             <?php
  88.             foreach($data as $person)
  89.                 {
  90.                     echo $person['lname'] . ', ' . $person['fname'] . '<br />';
  91.                 }
  92.             ?>
  93.         </div>
  94.        
  95.     </body>
  96. </html>
RAW Paste Data