Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- USE test;
- DROP TABLE IF EXISTS data;
- CREATE TABLE IF NOT EXISTS data(
- id INT AUTO_INCREMENT PRIMARY KEY,
- nama VARCHAR(10),
- alamat VARCHAR(10),
- jurusan VARCHAR(20)
- );
- INSERT INTO data(nama,alamat,jurusan)
- VALUES
- ('riris','gresik','matematika'),
- ('mala','surabaya','pertanian'),
- ('lily','sidoarjo','informatika'),
- ('dimas','lamongan','akuntansi'),
- ('hany','jepara','biologi');
- */
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- $sql="SELECT ".(isset($_POST['field'])?'id,'.implode(',',$_POST['field']):'*')
- ." FROM data"
- .(isset($_POST['id'])?" WHERE id IN(".implode(',',$_POST['id']).")":'');
- $result=$db->query($sql);
- if($result->num_rows>0){
- $no=0;
- $data=array();
- while ($row = $result->fetch_assoc())
- $data[]=$row;
- }
- ?>
- <!doctype html>
- <html>
- <head>
- <title>Tampil Data</title>
- </head>
- <body>
- <h2>Data </h2>
- <form method='post'>
- <table border='1'>
- <caption>QUERY: "<?php echo $sql;?>"</caption>
- <thead>
- <tr>
- <th>pilih</td>
- <th>No</th>
- <?php echo (!isset($data[0]['nama']) )?"":"<th>Nama<input type='checkbox' name='field[]' value='nama'></th>"; ?>
- <?php echo (!isset($data[0]['alamat']))?"":"<th>Alamat<input type='checkbox' name='field[]' value='alamat'></th>"; ?>
- <?php echo (!isset($data[0]['jurusan']))?"":"<th>Jurusan<input type='checkbox' name='field[]' value='jurusan'></th>"; ?>
- </tr>
- </thead>
- <tbody>
- <?php
- if($result->num_rows>0){
- foreach($data as $row)
- echo "
- <tr>
- <td><input type='checkbox' name='id[]' value='{$row['id']}'></td>
- <td>".++$no."</td>
- ".(isset($row['nama'])?"<td>{$row['nama']}</td>":"")."
- ".(isset($row['alamat'])?"<td>{$row['alamat']}</td>":"")."
- ".(isset($row['jurusan'])?"<td>{$row['jurusan']}</td>":"")."
- </tr>
- ";
- }else{
- echo "<tr><td colspan='4'>Tidak ada data terpilih</td></tr>";
- }
- ?>
- </tbody>
- </table>
- <input type='submit' name='submit' value='lihat'>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment