Cara Menggunakan Tanggal dalam Pencarian menggunakan PHP

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg
August 29, 2018




Selamat malam sobat blogger kali ini kami akan share Cara Menggunakan Tanggal dalam Pencarian menggunakan PHP. Tutorial ini akan membantu kalian menggunakan  Date dalam pencarian.



Yang pertama kita buat adalah membuat databsenya terlebih dahulu dengan nama db_search, berikut databasenya.


 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Table structure for table `tbl_member`
--

CREATE TABLE `tbl_member` (
  `tbl_member_id` int(11) NOT NULL,
  `tbl_member_name` varchar(100) NOT NULL,
  `tbl_member_contact` varchar(100) NOT NULL,
  `tbl_member_added` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tbl_member`
--

INSERT INTO `tbl_member` (`tbl_member_id`, `tbl_member_name`, `tbl_member_contact`, `tbl_member_added`) VALUES
(1, 'Arman', '09213884349', '2018-08-20 06:46:12'),
(2, 'Abidzar', '09223424623', '2017-09-29 07:36:07'),
(3, 'Iad', '09992358236', '2015-09-28 02:16:31'),
(4, 'Ardi', '09234536824', '2016-09-29 02:20:10');

--
-- Indexes for table `tbl_member`
--
ALTER TABLE `tbl_member`
  ADD PRIMARY KEY (`tbl_member_id`);

--
-- AUTO_INCREMENT for table `tbl_member`
--
ALTER TABLE `tbl_member`
  MODIFY `tbl_member_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
 
Kedua adalah membuat file dengan nama index.php file ini berfungsi untuk menampilkan data database.

 
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Search Use Date</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>
  <br/>
  <div class="container">
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
       <div class="navbar-header">
        <a class="navbar-brand" href="search.php">Search Use Date</a>
      </div>
    </div>
  </nav>

  <form method="post" action="search.php" class="form-inline">
    <div class="form-group">
      <label for="Date_From">Insert Date</label>
      <input type="date" name="date_from" value="<?php echo date('Y-m-d'); ?>" class="form-control" />
    </div>
    <button type="submit" name="search" class="btn btn-primary">Search</button>
  </form>
  <br/>


  <div class="table-responsive">                
    <table id="mytable" class="table table-bordred table-striped">                 
      <thead>
        <th>Member Name</th>
        <th>Contact</th>
        <th>Date Added</th>
      </thead>
      <tbody>
       
        <?php
        include ('database.php');
        $result = $database->prepare ("SELECT * FROM tbl_member order by tbl_member_id DESC");
        $result ->execute();
        for ($count=0; $row_member = $result ->fetch(); $count++){
          $id = $row_member['tbl_member_id'];
          ?>
          <tr>
            <td><?php echo $row_member['tbl_member_name']; ?></td>
            <td><?php echo $row_member['tbl_member_contact']; ?></td>
            <td><?php echo date("M d, Y h:i:s A", strtotime ($row_member['tbl_member_added'])); ?></td>
          </tr>
        <?php } ?>
      </tbody>      
    </table>
  </div>
</div>
</body>
</html> 

Ketiga buat file dengan nama search.php disini kami menyusun query untuk fungsi pencarian menggunakan tanggal.

Ini adalah form setelah pengguna memilih tanggal tertentu untuk mencari data dalam tabel. berikut kodenya.

 
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Search Use Date</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>
  <br/>
  <div class="container">
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
       <div class="navbar-header">
        <a class="navbar-brand" href="search.php">Search Use Date</a>
      </div>
    </div>
  </nav>

  <form method="post" action="search.php"  class="form-inline">
    <div class="form-group">
      <label for="Date_From">Insert Date</label>
      <input type="date" name="date_from" value="<?php echo (isset ($_POST['date_from'])) ? $_POST['date_from']: ''; ?>" class="form-control" />
    </div>
    <button type="submit" name="search" class="btn btn-primary">Search</button>
  </form>
  <br />
  <a href="index.php" style="text-decoration:none;">
    <button type="submit" class="btn btn-danger">Back</button>
    <br/><p/>

    <div class="table-responsive">                
      <table id="mytable" class="table table-bordred table-striped">                 
        <thead>
          <th>Member Name</th>
          <th>Contact</th>
          <th>Date Added</th>
        </thead>
        <tbody>

          <?php
          include ('database.php');
          $result = $database->prepare ("SELECT * FROM tbl_member where (tbl_member.tbl_member_added BETWEEN '".$_POST['date_from']." 00:00:01' and '".$_POST['date_from']." 23:59:59') order by tbl_member_id DESC");
          $result ->execute();
          for ($count=0; $row_member = $result ->fetch(); $count++){
          $id = $row_member['tbl_member_id'];
          ?>
          <tr>
            <td><?php echo $row_member['tbl_member_name']; ?></td>
            <td><?php echo $row_member['tbl_member_contact']; ?></td>
            <td><?php echo date("M d, Y h:i:s A", strtotime ($row_member['tbl_member_added'])); ?></td>
          </tr>
          <?php } ?>
        </tbody>      
      </table>
    </div>
  </div>
</body>
</html> 

Yang terakhir adalah membuat file dengan nama database.php file ini berfungsi sebagai koneksi database.

 <?php
 $database = new PDO ("mysql:host=localhost;dbname=db_search", 'root', '' );
?> 



Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, Jika kalian ingin membuat pencarian menggunakan nama atau id itu bisa tinggal teman-teman memodifikasinya.


Related Post

SUBSCRIBE TO OUR NEWSLETTER

closed