Advertisement
cahyadsn

select data

Nov 10th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. /*
  3. USE test;
  4.  
  5. DROP TABLE IF EXISTS data;
  6. CREATE TABLE IF NOT EXISTS data(
  7. id INT AUTO_INCREMENT PRIMARY KEY,
  8. nama VARCHAR(10),
  9. alamat VARCHAR(10),
  10. jurusan VARCHAR(20)
  11. );
  12.  
  13. INSERT INTO data(nama,alamat,jurusan)
  14. VALUES
  15. ('riris','gresik','matematika'),
  16. ('mala','surabaya','pertanian'),
  17. ('lily','sidoarjo','informatika'),
  18. ('dimas','lamongan','akuntansi'),
  19. ('hany','jepara','biologi');
  20. */
  21.  
  22. $dbhost='localhost';
  23. $dbuser='root';
  24. $dbpass='';
  25. $dbname='test';
  26. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  27. $sql="SELECT ".(isset($_POST['field'])?'id,'.implode(',',$_POST['field']):'*')
  28.     ." FROM data"
  29.     .(isset($_POST['id'])?" WHERE id IN(".implode(',',$_POST['id']).")":'');
  30. $result=$db->query($sql);
  31. ?>
  32. <!doctype html>
  33. <html>
  34.   <head>
  35.     <title>Tampil Data</title>
  36.   </head>
  37.   <body>
  38.     <h2>Data </h2>
  39.     <form method='post'>
  40.       <table border='1'>
  41.         <caption>QUERY: "<?php echo $sql;?>"</caption>
  42.         <thead>
  43.           <tr>
  44.             <th>pilih</td>
  45.             <th>No</th>
  46.             <th>Nama<input type='checkbox' name='field[]' value='nama'></th>
  47.             <th>Alamat<input type='checkbox' name='field[]' value='alamat'></th>
  48.             <th>Jurusan<input type='checkbox' name='field[]' value='jurusan'></th>
  49.           </tr>
  50.         </thead>
  51.         <tbody>
  52.           <?php
  53.           if($result->num_rows>0){
  54.             $no=0;
  55.             while ($row = $result->fetch_assoc())
  56.               echo "
  57.              <tr>
  58.                <td><input type='checkbox' name='id[]' value='{$row['id']}'></td>
  59.                <td>".++$no."</td>
  60.                <td>".(isset($row['nama'])?$row['nama']:"")."</td>
  61.                <td>".(isset($row['alamat'])?$row['alamat']:"")."</td>
  62.                <td>".(isset($row['jurusan'])?$row['jurusan']:"")."</td>
  63.              </tr>
  64.              ";
  65.           }else{
  66.             echo "<tr><td colspan='4'>Tidak ada data terpilih</td></tr>";
  67.           }
  68.           ?>
  69.         </tbody>
  70.       </table>
  71.       <input type='submit' name='submit' value='lihat'>
  72.     </form>
  73.   </body>
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement