Advertisement
enos

Search

Apr 11th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.53 KB | None | 0 0
  1. <?php
  2. //search.php
  3. use test;
  4.  
  5. -- --------------------------------------------------------
  6. -- Table structure for table `tbl_category`
  7. --
  8. DROP TABLE IF EXISTS `tbl_category`;
  9. CREATE TABLE IF NOT EXISTS `tbl_category` (
  10.   `id_category` smallint(4) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key for table tbl_catgeory',
  11.   `category` varchar(50) NOT NULL COMMENT 'category',
  12.   PRIMARY KEY (`id_category`)
  13. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table to store category datas';
  14. --
  15. -- Dumping data for table `tbl_category`
  16. --
  17. INSERT INTO `tbl_category` (`id_category`, `category`) VALUES
  18. (1, 'internal'),
  19. (2, 'external');
  20. -- --------------------------------------------------------
  21. -- Table structure for table `tbl_status`
  22. --
  23. DROP TABLE IF EXISTS `tbl_status`;
  24. CREATE TABLE IF NOT EXISTS `tbl_status` (
  25.   `id_status` tinyint(2) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key for table tbl_status',
  26.   `status` varchar(50) NOT NULL COMMENT 'status',
  27.   PRIMARY KEY (`id_status`)
  28. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table to store status datas';
  29. --
  30. -- Dumping data for table `tbl_status`
  31. --
  32. INSERT INTO `tbl_status` (`id_status`, `status`) VALUES
  33. (1, 'draft'),
  34. (2, 'publish');
  35. -- --------------------------------------------------------
  36. -- Table structure for table `tbl_document`
  37. --
  38. DROP TABLE IF EXISTS `tbl_document`;
  39. CREATE TABLE IF NOT EXISTS `tbl_document` (
  40.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key for table tbl_document',
  41.   `title` varchar(50) NOT NULL COMMENT 'document title',
  42.   `id_status` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT 'document status',
  43.   `id_category` smallint(4) unsigned NOT NULL DEFAULT '0' COMMENT 'document category',
  44.   PRIMARY KEY (`id`)
  45. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table to store document datas';
  46. --
  47. -- Dumping data for table `tbl_document`
  48. --
  49. INSERT INTO `tbl_document` (`id`, `title`, `id_status`, `id_category`) VALUES
  50. (1, 'Modul Pembelajaran PHP 1', 1, 1),
  51. (2, 'Ajax Unleashed', 1, 2),
  52. (3, 'Modul Pembelajaran MySQL', 2, 1),
  53. (4, 'PHP for Web Developer', 2, 2),
  54. (5, 'Modul Pembelajaran Javascript', 2, 1);
  55.  
  56. */
  57. //database configuration  
  58. $dbhost='localhost';  
  59. $dbuser='root';  
  60. $dbpass='';  
  61. $dbname='test';  
  62. //database connection  
  63. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);  
  64. //get input data
  65. $title=isset($_POST['title'])?$_POST['title']:'';
  66. $id_status=isset($_POST['id_status'])?$_POST['id_status']:'';
  67. $id_category=isset($_POST['id_category'])?$_POST['id_category']:'';
  68. ?>
  69. <!DOCTYPE html>
  70. <html>
  71.   <head>
  72.     <meta charset="utf-8">
  73.     <meta name="viewport" content="width=device-width, initial-scale=1">
  74.     <title>Search Document</title>
  75.     <style type="text/css">
  76.       *{font-family:calibri,arial,san-serif;font-size:12px;}
  77.       table {padding:2px;}
  78.       input,select {font-family:calibri,arial,sans-serif;font-size:12px;padding:3px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;border: solid 1 px #999;color:#333;background-color:#ff9;}
  79.       legend {color:#900;padding:3px 10px; border:1 solid #eee;background-color:#eee;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;-moz-box-shadow:2px 2px 8px rgba(0,0,0,0.5);-webkit-box-shadow:2px 2px 8px rgba(0,0,0,0.5);}
  80.       fieldset{-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;padding:3px;-moz-box-shadow:2px 2px 8px rgba(0,0,0,0.5);-webkit-box-shadow:2px 2px 8px rgba(0,0,0,0.5);margin-bottom:15px;border:none;}      
  81.     </style>
  82.   </head>
  83.   <body>
  84.     <div class="container">
  85.       <div id="search_box">
  86.         <form method="post">
  87.           <fieldset>
  88.             <legend>Search Document</legend>
  89.             title
  90.             <input type="text" id="title" name="title" />
  91.             status
  92.             <select id="id_status" name="id_status">
  93.               <option value=''>-- all --</option>
  94.               <?php
  95.               $sql="SELECT * FROM tbl_status";
  96.               $result=$db->query($sql);
  97.               while($row=$result->fetch_object()){
  98.                 echo "<option value='".$row->id_status."'".($id_status==$row->id_status?" selected='selected'":"").">".$row->status."</option>\n";
  99.               }
  100.               $result->free;
  101.               ?>
  102.             </select>
  103.             category
  104.             <select id="id_category" name="id_category">
  105.               <option value=''>-- all --</option>
  106.               <?php
  107.               $sql="SELECT * FROM tbl_category";
  108.               $result=$db->query($sql);
  109.               while($row=$result->fetch_object()){
  110.                 echo "<option value='".$row->id_category."'".($id_category==$row->id_category?" selected='selected'":"").">".$row->category."</option>\n";
  111.               }
  112.               $result->free;
  113.               ?>
  114.             </select>
  115.             <input type="submit" name="search" id="search" value="search" />
  116.           </fieldset>
  117.         </form>
  118.       </div>
  119.       <div id="grid_box">
  120.         <fieldset>
  121.           <legend>Result</legend>
  122.           <table>
  123.             <tr>
  124.               <th>No</th>
  125.               <th>Document</th>
  126.               <th>Status</th>
  127.               <th>Category</th>
  128.               <th>Action</th>
  129.             </tr>
  130.             <?php
  131.             $sql="SELECT a.id,a.title,b.status,c.category "
  132.                 ."FROM tbl_document a "
  133.                 ."JOIN tbl_status b USING(id_status) "
  134.                 ."JOIN tbl_category c USING(id_category) "
  135.                 ."WHERE 1"
  136.                 .(!empty($title)?" AND a.title LIKE '%$title%'":'')
  137.                 .(!empty($id_status)?" AND a.id_status='$id_status'":'')
  138.                 .(!empty($id_category)?" AND a.id_category='$id_category'":'');
  139.             $result=$db->query($sql);
  140.             $i=0;
  141.             if($result){
  142.               while($row=$result->fetch_object()){
  143.                 echo "<tr>\n"
  144.                     ."<td>".++$i."</td>\n"
  145.                     ."<td>".$row->title."</td>\n"
  146.                     ."<td>".$row->status."</td\n>"
  147.                     ."<td>".$row->category."</td>\n"
  148.                     ."<td>\n"
  149.                     ."<a href='editdoc.php?id=".$row->id."'>edit</a> | \n"
  150.                     ."<a href='deldoc.php?id=".$row->id."'>delete</a>\n"
  151.                     ."</td>\n"
  152.                     ."</tr>\n";
  153.               }
  154.               $result->free();
  155.             }else{
  156.               echo "<tr><td>No data found</td></tr>\n";
  157.             }
  158.             ?>
  159.           </table>
  160.         </fieldset>
  161.       </div>
  162.     </div>
  163.   </body>
  164. </html>
  165. <?php $db->close();?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement