Advertisement
cdsatrian

belajar query utk searching

Nov 5th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. //-- get parameter from previous POST
  3. $type=isset($_POST['type'])?$_POST['type']:'';
  4. $search=isset($_POST['search'])?$_POST['search']:'';
  5. ?>
  6. <!doctype html>
  7. <html>
  8.   <head>
  9.     <title>Belajar Query</title>
  10.   </head>
  11.   <body>
  12.     <fieldset>
  13.       <legend>Search Box</legend>
  14.       <form name='frm_search' id='frm_search' method='post'>
  15.         <div id='searchbox'>
  16.           Cari
  17.           <input type='text' name='search' value='<?php echo $search; ?>' />
  18.           Berdasarkan
  19.           <select name='type'>
  20.             <option value='news'<?php echo $type=='news'?' selected':''; ?>>News</option>
  21.             <option value='event'<?php echo $type=='event'?' selected':''; ?>>Event</option>
  22.           </select>
  23.           <input type='submit' value='cari'/>
  24.         </div>
  25.       </form>
  26.     </fieldset>
  27.     <fieldset>
  28.       <legend>Result Box</legend>
  29.       <table>
  30.       <tr>
  31.         <th>Title</th><th>Content</th><th>Date</th><th>Event<?/th>
  32.       </tr>
  33.       <?php
  34.       //-- database configuration
  35.       $dbhost='localhost';
  36.       $dbuser='root';
  37.       $dbpass='';
  38.       $dbname='pgi';
  39.       //-- database connection
  40.       $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  41.       $sql="
  42.            SELECT
  43.              a.pgi_news_title,
  44.              a.pgi_news_content,
  45.              a.pgi_news_date,
  46.              b.pgi_event_name
  47.            FROM
  48.              pgi_news a
  49.              JOIN pgi_event b ON a.pgi_news_event_id=b.pgi_event_id
  50.            WHERE
  51.              1
  52.            "
  53.             .($type=='news'?" AND a.pgi_news_title LIKE '%{$search}%' ":'')
  54.             .($type=='event'?" AND b.pgi_event_name LIKE '%{$search}%' ":'')
  55.             ;
  56.       echo "<tr><td colspan='4'>{$sql}</td></tr>";
  57.       if($result=$db->query($sql)){
  58.         while($row=$result->fetch_object()){
  59.           echo "
  60.          <tr>
  61.            <td>{$row->pgi_news_title}</td>
  62.            <td>{$row->pgi_news_content}</td>
  63.            <td>{$row->pgi_news_date}</td>
  64.            <td>{$row->pgi_event_name}</td>
  65.          </tr>\n";
  66.         }
  67.       }
  68.       ?>
  69.     </fieldset>
  70.   </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement