Advertisement
cdsatrian

Search'n Document Example

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