Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-- get parameter from previous POST
- $type=isset($_POST['type'])?$_POST['type']:'';
- $search=isset($_POST['search'])?$_POST['search']:'';
- ?>
- <!doctype html>
- <html>
- <head>
- <title>Belajar Query</title>
- </head>
- <body>
- <fieldset>
- <legend>Search Box</legend>
- <form name='frm_search' id='frm_search' method='post'>
- <div id='searchbox'>
- Cari
- <input type='text' name='search' value='<?php echo $search; ?>' />
- Berdasarkan
- <select name='type'>
- <option value='news'<?php echo $type=='news'?' selected':''; ?>>News</option>
- <option value='event'<?php echo $type=='event'?' selected':''; ?>>Event</option>
- </select>
- <input type='submit' value='cari'/>
- </div>
- </form>
- </fieldset>
- <fieldset>
- <legend>Result Box</legend>
- <table>
- <tr>
- <th>Title</th><th>Content</th><th>Date</th><th>Event<?/th>
- </tr>
- <?php
- //-- database configuration
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='pgi';
- //-- database connection
- $con=mysql_connect($dbhost,$dbuser,$dbpass);
- mysql_select_db($dbname);
- $sql="
- SELECT
- a.pgi_news_title,
- a.pgi_news_content,
- a.pgi_news_date,
- b.pgi_event_name
- FROM
- pgi_news a
- JOIN pgi_event b ON a.pgi_news_event_id=b.pgi_event_id
- WHERE
- 1
- "
- .($type=='news'?" AND a.pgi_news_title LIKE '%{$search}%' ":'')
- .($type=='event'?" AND b.pgi_event_name LIKE '%{$search}%' ":'')
- ;
- echo "<tr><td colspan='4'>{$sql}</td></tr>";
- if($result=mysql_query($sql)){
- while($row=mysql_fetch_object($result)){
- echo "
- <tr>
- <td>{$row->pgi_news_title}</td>
- <td>{$row->pgi_news_content}</td>
- <td>{$row->pgi_news_date}</td>
- <td>{$row->pgi_event_name}</td>
- </tr>\n";
- }
- }
- ?>
- </fieldset>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement