Advertisement
cdsatrian

versi mysql

Nov 5th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 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.       $con=mysql_connect($dbhost,$dbuser,$dbpass);
  41.       mysql_select_db($dbname);
  42.       $sql="
  43.            SELECT
  44.              a.pgi_news_title,
  45.              a.pgi_news_content,
  46.              a.pgi_news_date,
  47.              b.pgi_event_name
  48.            FROM
  49.              pgi_news a
  50.              JOIN pgi_event b ON a.pgi_news_event_id=b.pgi_event_id
  51.            WHERE
  52.              1
  53.            "
  54.             .($type=='news'?" AND a.pgi_news_title LIKE '%{$search}%' ":'')
  55.             .($type=='event'?" AND b.pgi_event_name LIKE '%{$search}%' ":'')
  56.             ;
  57.       echo "<tr><td colspan='4'>{$sql}</td></tr>";
  58.       if($result=mysql_query($sql)){
  59.         while($row=mysql_fetch_object($result)){
  60.           echo "
  61.          <tr>
  62.            <td>{$row->pgi_news_title}</td>
  63.            <td>{$row->pgi_news_content}</td>
  64.            <td>{$row->pgi_news_date}</td>
  65.            <td>{$row->pgi_event_name}</td>
  66.          </tr>\n";
  67.         }
  68.       }
  69.       ?>
  70.     </fieldset>
  71.   </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement