Delete Multiple Menggunakan PHP MySQL dan PDO

Kali ini kami akan share bagaimana membuat delete multiple menggunakan PHP MySQL dan PDO. Sistem ini terlihat simple tetapi mempunyai fungsi yang besar dan sangat berguna.



Ada beberapa yang harus yang harus dipersiapkan, di antaranya adalah membuat struktur database, buat database dengan nama pdo kemudian masukkan struktur database dibawah ini.

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

CREATE TABLE `members` (
  `id` int(11) NOT NULL,
  `nama_depan` varchar(100) NOT NULL,
  `alamat` varchar(100) NOT NULL,
  `kelas` varchar(10) NOT NULL,
  `umur` int(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `members` (`id`, `nama_depan`, `alamat`, `kelas`, `umur`) VALUES
(1, 'Andi Abidzar', 'Jl. Pelita', 'II', 22),
(2, 'Dede Ahmad', 'Jl. Alauddin', 'III', 22),
(3, 'Irsyad Khalid', 'Palopo, Sulawesi', 'IV', 22),
(4, 'Ahmad Noer', 'J. Perintis Kemerdekaan', 'V', 3);

ALTER TABLE `members`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `members`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT; 

Kedua buat file dengan nama dbConfig.php file ini berisi script untuk koneksi kedatabase.

 <?php
$db_host  = 'localhost';
$db_user  = 'root';
$db_pass  = '';
$db_database = 'pdo'; 

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?> 

Ketiga buat file dengan nama index.php file ini sebagai form untuk menampilkan data yang ada didalam database.

 <!DOCTYPE html>
<html>
<head>
  <title>Deleted Multiple</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>


  <script type="text/javascript">
    function delete_confirm(){
      var result = confirm("Are you sure to delete users?");
      if(result){
        return true;
      }else{
        return false;
      }
    }
  </script>
</head>
<body>
  <br /><br />
  <div class="container">
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="tutorialswb.com">Deleted Multiple</a>
    </div>
  </div>
</nav>
<br />
<h2 align="center">DELETED MULTIPLE USING PHP PDO</h2>
<br />

<form name="bulk_action_form" action="action.php" method="post" onSubmit="return delete_confirm();"/>
<table class="table">
  <thead>
    <tr>
      <th> &nbsp; </th>
      <th> Nama Lengkap </th>
      <th> Alamat Lengkap </th>
      <th> Umur </th>
      <th> Kelas </th>
    </tr>
  </thead>
  <tbody>
   <?php
   include('dbConfig.php');
   $result = $db->prepare("SELECT * FROM members ORDER BY id DESC");
   $result->execute();
   for($i=0; $row = $result->fetch(); $i++){
     ?>
     <tr class="record">
      <td><input name="selector[]" type="checkbox" value="<?php echo $row['id']; ?>"></td>
      <td><?php echo $row['nama_depan']; ?></td>
      <td><?php echo $row['alamat']; ?></td>
      <td><?php echo $row['umur']; ?></td>
      <td><?php echo $row['kelas']; ?></td>
    </tr>
    <?php
  }
  ?>
</tbody>
</table>
<input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="Delete"/>
</form>
</div>

<!--Script Javascript-->

</body>
</html> 

Keempat buat file dengan nama action.php file ini berisi script atau perintah untuk menghapus data yang ada di database.

 <?php
include('dbConfig.php');

$edittable=$_POST['selector'];
$N = count($edittable);
for($i=0; $i < $N; $i++)
{
 $result = $db->prepare("DELETE FROM members WHERE id= :memid");
 $result->bindParam(':memid', $edittable[$i]);
 $result->execute();
}
header("location: index.php");
mysql_close($con);
?> 

Kalian bisa mendownload file dan melihat langsung file demonya dibawah ini.

DEMO      DOWNLOAD

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, jika kalian belum paham atau ingin request post silahkan tinggalkan monetra dibawah dan mulai berdiskusi dengan kami.

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg

Membuat Contact Form Menggunakan SMTP

Selamat pagi kali ini kami akan share contoh contact form menggunakan PHP, dan pada post terakhir dibawah kalian bisa langsung melihat dan mendownload filenya secara langsung dan silahkan mencoba dan memodifikasi sesuai dengan yang anda perlukan.


Langsung saja yang perlu kalian lakukan adalah membuat formnya terlebih dahulu, buat file dengan nama index.php kemudian masukkan script form dan script validasi contact.

 <!DOCTYPE html>

<html lang="en">

<head>

 <title>Contact Us</title>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="fonts/iconic/css/material-design-iconic-font.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="vendor/noui/nouislider.min.css">

 <!--===============================================================================================-->

 <link rel="stylesheet" type="text/css" href="css/util.css">

 <link rel="stylesheet" type="text/css" href="css/main.css">

 <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>

 <!--===============================================================================================-->



 <script type="text/javascript">

  $(document).ready(function (e){

   $("#frmContact").on('submit',(function(e){

    e.preventDefault();

    var valid; 

    valid = validateContact();

    if(valid) {

     $.ajax({

      url: "mail.php",

      type: "POST",

      data:  new FormData(this),

      contentType: false,

      cache: false,

      processData:false,

      success: function(data){

       $("#mail-status").html(data);

      },

      error: function(){}          



     });

    } 

   }));



   function validateContact() {

    var valid = true; 

    $(".demoInputBox").css('background-color','');

    $(".info").html('');



    if(!$("#userName").val()) {

     valid = false;

    }

    if(!$("#userEmail").val()) {

     valid = false;

    }

    if(!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {

     $("#userEmail-info").html("(invalid)");

     valid = false;

    }

    if(!$("#subject").val()) {

     valid = false;

    }

    if(!$("#content").val()) {

     valid = false;

    }



    return valid;

   }



  });

 </script>

</head>

<body>

 <div class="container-contact100">

  <div class="wrap-contact100">

   <form id="frmContact" action="" method="post" class="contact100-form validate-form">


    <span class="contact100-form-title">

     Contact Us

    </span>

 

    <div class="alert alert-primary" role="alert">

     <div id="mail-status"></div>

    </div>

    

    <div class="wrap-input100 validate-input bg1" data-validate="Please Type Your Name">

     <span class="label-input100">FULL NAME *</span>

     <input class="input100" type="text" name="userName" id="userName" placeholder="Enter Your Name" required="">

    </div>



    <div class="wrap-input100 validate-input bg1 rs1-wrap-input100" data-validate = "Enter Your Email (e@a.x)">

     <span class="label-input100">Email *</span>

     <input class="input100" type="text" name="userEmail" id="userEmail" placeholder="Enter Your Email" required="">

    </div>



    <div class="wrap-input100 bg1 rs1-wrap-input100">

     <span class="label-input100">Phone</span>

     <input class="input100" type="text" name="subject" id="subject" placeholder="Enter Number Phone" required="">

    </div>



    <div class="wrap-input100 validate-input bg0 rs1-alert-validate" data-validate = "Please Type Your Message">

     <span class="label-input100">Message</span>

     <textarea class="input100" name="content" id="content" placeholder="Your message here..." required=""></textarea>

    </div>



    <div class="container-contact100-form-btn">

     <button class="contact100-form-btn" type="submit">

      <span>

       Submit

       <i class="fa fa-long-arrow-right m-l-7" aria-hidden="true"></i>

      </span>

     </button>

    </div>

   </form>

  </div>

 </div>

 <!--===============================================================================================-->

 <script src="vendor/jquery/jquery-3.2.1.min.js"></script>

 <!--===============================================================================================-->

 <script src="vendor/animsition/js/animsition.min.js"></script>

 <!--===============================================================================================-->

 <script src="vendor/bootstrap/js/popper.js"></script>

 <script src="vendor/bootstrap/js/bootstrap.min.js"></script>

 <!--===============================================================================================-->

 <script src="vendor/select2/select2.min.js"></script>

 <!--===============================================================================================-->

 <script src="vendor/daterangepicker/moment.min.js"></script>

 <script src="vendor/daterangepicker/daterangepicker.js"></script>

 <!--===============================================================================================-->

 <script src="vendor/countdowntime/countdowntime.js"></script>

 <!--===============================================================================================-->

 <script src="vendor/noui/nouislider.min.js"></script>

 <!--===============================================================================================-->

 <script src="js/main.js"></script>

</body>

</html>

 
Kemudian buat file dengan nama mail.php disini kami menggunakan jQuery Ajax, fungsinya adalah untuk memanggil skrip email PHP untuk mengirim email kontak menggunakan SMTP Gmail.

 <?php
require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port     = 587;  
$mail->Username = "SMTP Username";
$mail->Password = "SMTP Password";
$mail->Host     = "SMTP Host";
$mail->Mailer   = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("Email Penerima"); 
$mail->Subject = $_POST["subject"];
$mail->WordWrap   = 80;
$mail->MsgHTML($_POST["content"]);


$mail->IsHTML(true);

if(!$mail->Send()) {
 echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
 echo "<p class='success'>Contact Mail Sent.</p>";
} 
?> 

Kalian ubah username, password dan host sesuai yang anda punya pada hosting anda, Kemudian anda juga harus memasukkan email penerima. Kalian bisa melihat dan langsung mendownload filenya dibawah ini.

DEMO     DOWNLOAD

Sekian share kali ini jika kalian belum paham dengan penggunaan SMTP di atas bisa meninggalkan komentar dibawah dan mulai berdiskusi dengan kami.

//lh3.googleusercontent.com/-K5UKWzgJhmU/AAAAAAAAAAI/AAAAAAAACdI/CN3nwiBZsGE/s512-c/photo.jpg

Membuat Shopping Cart Simple Menggunakan PHP

Selamat pagi sobat blogger kali ini kami akan share bagaimana membuat shopping cart simple menggunakan PHP, disamping itu kami menggunakan bootsrap sebagai kerangka tampilan pada app shopping cart.



Langsung saa yang pertama kita diharuskan membuat database, buat database dengan nama shopping kemudian masukkan struktur database dibawah ini.

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


CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `price` double NOT NULL,
  `photo` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `products` (`id`, `name`, `price`, `photo`) VALUES
(1, 'DELL Inspiron 15 7000 15.6', 899, 'images/1.jpg'),
(2, 'MICROSOFT Surface Pro 4 & Typecover - 128 GB', 799, 'images/2.jpg'),
(3, 'DELL Inspiron 15 5000 15.6', 599, 'images/3.jpg'),
(4, 'LENOVO Ideapad 320s-14IKB 14\" Laptop - Grey', 399, 'images/4.jpg');


ALTER TABLE `products`
  ADD PRIMARY KEY (`id`);


ALTER TABLE `products`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
COMMIT;
 

Buat file dengan nama index.php, file ini akan berfungsi untuk menampilkan produk dan pada file ini terdapat form_modal untuk menambahkan produk.


 <?php
session_start();
 //initialize cart if not set or is unset
if(!isset($_SESSION['cart'])){
 $_SESSION['cart'] = array();
}

 //unset qunatity
unset($_SESSION['qty_array']);
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Hopping Cart</title>
 <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
 <style>
 .product_image{
  height:200px;
 }
 .product_name{
  height:80px; 
  padding-left:20px; 
  padding-right:20px;
 }
 .product_footer{
  padding-left:20px; 
  padding-right:20px;
 }
</style>
</head>
<body>
 <br/>
 <div class="container">
  <nav class="navbar navbar-default">
   <div class="container-fluid">
    <div class="navbar-header">
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
     </button>
     <a class="navbar-brand" href="#">Shopping Cart</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
     <ul class="nav navbar-nav">
      <!-- left nav here -->
     </ul>
     <ul class="nav navbar-nav navbar-right">
      <li><a href="view_cart.php"><span class="badge"><?php echo count($_SESSION['cart']); ?></span> Cart <span class="glyphicon glyphicon-shopping-cart"></span></a></li>
     </ul>
    </div>
   </div>
  </nav>
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  <p/>
  <?php
  //info message
  if(isset($_SESSION['message'])){
   ?>
   <div class="row">
    <div class="col-sm-6 col-sm-offset-6">
     <div class="alert alert-info text-center">
      <?php echo $_SESSION['message']; ?>
     </div>
    </div>
   </div>
   <?php
   unset($_SESSION['message']);
  }


  //connection
  $conn = new mysqli('localhost', 'root', '', 'shopping');

  $sql = "SELECT * FROM products";
  $query = $conn->query($sql);
  $inc = 4;
  while($row = $query->fetch_assoc()){
   $inc = ($inc == 4) ? 1 : $inc + 1; 
   if($inc == 1) echo "<div class='row text-center'>";  
   ?>
   <div class="col-sm-3">
    <div class="panel panel-default">
     <div class="panel-body">
      <div class="row product_image">
       <img src="<?php echo $row['photo'] ?>" width="80%" height="auto">
      </div>
      <div class="row product_name">
       <h4><?php echo $row['name']; ?></h4>
      </div>
      <div class="row product_footer">
       <p class="pull-left"><b><?php echo $row['price']; ?></b></p>
       <span class="pull-right"><a href="add_cart.php?id=<?php echo $row['id']; ?>" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-plus"></span> Cart</a></span>
      </div>
     </div>
    </div>
   </div>
   <?php
  }
  if($inc == 1) echo "<div></div><div></div><div></div></div>"; 
  if($inc == 2) echo "<div></div><div></div></div>"; 
  if($inc == 3) echo "<div></div></div>";
  
  //end product row 
  ?>

 </div>

   <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
   <form action="save_query.php" method="POST" enctype="multipart/form-data">
    <div class="modal-content">
     <div class="modal-body">
      <div class="col-md-2"></div>
      <div class="col-md-8">
       <div class="form-group">
        <label>Product Name</label>
        <input class="form-control" type="text" name="name">
       </div>
       <div class="form-group">
        <label>Product Price</label>
        <input class="form-control" type="number" name="price">
       </div>
       <div class="form-group">
        <label>Product Photo</label>
        <input class="form-control" type="file" name="photo">
       </div>
      </div>
     </div>
     <div style="clear:both;"></div>
     <div class="modal-footer">
      <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
      <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
     </div>
    </div>
   </form>
  </div>
 </div>
</body>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</html> 

Berikutnya buat file dengan nama add_cart.php file ini berisi script berfungsi sebagai add to chart.

 <?php
 session_start();

 //periksa apakah produk sudah ada dalam keranjang
 if(!in_array($_GET['id'], $_SESSION['cart'])){
  array_push($_SESSION['cart'], $_GET['id']);
  $_SESSION['message'] = 'Product added to cart';
 }
 else{
  $_SESSION['message'] = 'Product already in cart';
 }

 header('location: index.php');
?> 



Gambat diatas merupakan tampilan view cart, Berikutnya buat file dengan nama view_cart.php file ini berfungsi ini berisi cript yang berfungsi untuk menampilkan produk yang ada pada keranjangnya.

 <?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Shopping Cart</title>
 <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
 <br/>
 <div class="container">
  <nav class="navbar navbar-default">
   <div class="container-fluid">
    <div class="navbar-header">
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
     </button>
     <a class="navbar-brand" href="#">Shopping Cart</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
     <ul class="nav navbar-nav">
      <!-- left nav here -->
     </ul>
     <ul class="nav navbar-nav navbar-right">
      <li class="active"><a href="view_cart.php"><span class="badge"><?php echo count($_SESSION['cart']); ?></span> Cart <span class="glyphicon glyphicon-shopping-cart"></span></a></li>
     </ul>
    </div>
   </div>
  </nav>
  <div class="row">
   <div class="col-sm-8 col-sm-offset-2">
    <?php 
    if(isset($_SESSION['message'])){
     ?>
     <div class="alert alert-info text-center">
      <?php echo $_SESSION['message']; ?>
     </div>
     <?php
     unset($_SESSION['message']);
    }

    ?>
    <form method="POST" action="save_cart.php">
     <table class="table table-bordered table-striped">
      <thead>
       <th></th>
       <th>Name</th>
       <th>Price</th>
       <th>Quantity</th>
       <th>Subtotal</th>
      </thead>
      <tbody>
       <?php
      //initialize total
       $total = 0;
       if(!empty($_SESSION['cart'])){
      //connection
        $conn = new mysqli('localhost', 'root', '', 'shopping');
      //create array of initail qty which is 1
        $index = 0;
        if(!isset($_SESSION['qty_array'])){
         $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
        }
        $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
         ?>
         <tr>
          <td>
           <a href="delete_item.php?id=<?php echo $row['id']; ?>&index=<?php echo $index; ?>" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></a>
          </td>
          <td><?php echo $row['name']; ?></td>
          <td><?php echo number_format($row['price'], 2); ?></td>
          <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
          <td><input type="text" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>
          <td><?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></td>
          <?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
         </tr>
         <?php
         $index ++;
        }
       }
       else{
        ?>
        <tr>
         <td colspan="4" class="text-center">No Item in Cart</td>
        </tr>
        <?php
       }

       ?>
       <tr>
        <td colspan="4" align="right"><b>Total</b></td>
        <td><b><?php echo number_format($total, 2); ?></b></td>
       </tr>
      </tbody>
     </table>
     <a href="index.php" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-left"></span> Back</a>
     <button type="submit" class="btn btn-success" name="save">Save Changes</button>
     <a href="clear_cart.php" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Clear Cart</a>
     <a href="checkout.php" class="btn btn-success"><span class="glyphicon glyphicon-check"></span> Checkout</a>
    </form>
   </div>
  </div>
 </div>
</body>
</html> 

Buat file dengan nama save_query.php, file ini berisi script untuk menambahkan produk kedalam database.

 <?php
 $conn = new mysqli('localhost', 'root', '', 'shopping');
 
 if(ISSET($_POST['save'])){
  if(!empty($_POST['name']) && !empty($_POST['price'])){
   $name = addslashes($_POST['name']);
   $price = $_POST['price'];
 
   $file = explode(".", $_FILES['photo']['name']);
   $ext = array("png", "gif", "jpg", "jpeg");
 
   if(in_array($file[1], $ext)){
    $file_name = $file[0].'.'.$file[1];
    $tmp_file = $_FILES['photo']['tmp_name'];
    $location = "images/".$file_name;
    $new_location = addslashes($location);
 
    if(move_uploaded_file($tmp_file, $location)){
     $conn->query("INSERT INTO `products` VALUES('', '$name', '$price', '$new_location')");
     echo "<script>alert('Data Insert')</script>";
     echo "<script>window.location = 'index.php'</script>";
    }
 
   }else{
    echo "<script>alert('File not available')</script>";
    echo "<script>window.location = 'index.php'</script>";
   }
 
  }else{
   echo "<script>alert('Please complete the required field!')</script>";
  }
 
 
 }
?> 

Berikutnya buat file dengan nama delete_item.php file ini berfungsi untuk menghapus item dari keranjang yang sudah ditambahkan.

 <?php
 session_start();

 //hapus id dari larik keranjang kami
 $key = array_search($_GET['id'], $_SESSION['cart']); 
 unset($_SESSION['cart'][$key]);

 unset($_SESSION['qty_array'][$_GET['index']]);
 //mengatur ulang array setelah tidak disetel
 $_SESSION['qty_array'] = array_values($_SESSION['qty_array']);

 $_SESSION['message'] = "Product deleted from cart";
 header('location: view_cart.php');
?> 

Berikutnya buat file dengan nama save_cart.php file ini berisi script yang berfungsi untuk menyimpan produk yang ditambahkan atau produk yang telah di edit pada produk keranjang.

 <?php
 session_start();
 if(isset($_POST['save'])){
  foreach($_POST['indexes'] as $key){
   $_SESSION['qty_array'][$key] = $_POST['qty_'.$key];
  }

  $_SESSION['message'] = 'Cart updated successfully';
  header('location: view_cart.php');
 }
?>
 

Berikutnya buat file dengan nama clear_cart.php file ini berisi script yang berfungsi untuk membersihkan semua isi keranjang.

 <?php
 session_start();
 unset($_SESSION['cart']);
 $_SESSION['message'] = 'Cart cleared successfully';
 header('location: index.php');
?> 

Berikutnya buat file dengan nama checkout.php. Dalam fungsi umum checkout ini akan mengarahkan user atau pelanggan ke halaman login untuk mengisi identitas secara details. Tetapi dalam fungsi ini checkout ini akan menampilkan pesan bahwa belanjaan yang ada di keranjangnya.

 <?php
 session_start();
 //pengguna perlu login untuk checkout
 $_SESSION['message'] = 'You need to login to checkout';
 header('location: view_cart.php');
?> 

Kalian bisa langsung melihat demo dan bisa langsung dan mendownload filenya untuk bisa langsung mencoba dan mempelajarinya langsung.

DEMO       DOWNLOAD

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, Jika kalian belum paham bisa meningggalkan komentar dibawah dan mulai berdiskusi dengan kami.

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg

Menampilkan Data berdasarkan Bulan Menggunakan PHP

Selamat malam sobat blogger kali ini kami akan share bagaimana menampilkan data yang ada di database berdasarkan bulan yang dipilih. Aplikasi sederhana seperti ini sangat cocok bagi sebuah sistem yang mempunyai data yang sangat banyak.


Langsung saja mari kutai mulai dari tahap demi tahap, ada beberapa yang mesti kalian buat dan setiap file memilik script dan fungsi yang berbeda. Pada setiap file kami akan jelaskan secara detail.

config.php
index.php
filter.php
save.php

Tetapi sebelum itu buat databse terlebih dahulu, buat database dengan nama search.php kemudian masukkan struktur database dibawah ini.

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


CREATE TABLE `product` (
  `product_id` int(11) NOT NULL,
  `product_name` varchar(50) NOT NULL,
  `supplier` varchar(50) NOT NULL,
  `date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `product` (`product_id`, `product_name`, `supplier`, `date`) VALUES
(1, 'Iphone X (2018)', 'Iphone', '2018-09-15'),
(2, 'Iphone 8 Plus (2018)', 'Iphone', '2018-08-15'),
(3, 'Iphone 8 (2018)', 'Iphone', '2018-07-15'),
(4, 'Iphone 7 Plus (2018)', 'Iphone', '2018-06-15'),
(5, 'Iphone 7 (2018)', 'Iphone', '2018-05-15'),
(6, 'Iphone 6 Plus (2018)', 'Iphone', '2018-04-15'),
(7, 'Iphone 6 (2018)', 'Iphone', '2018-03-15'),
(8, 'Iphone 5 Plus (2018)', 'Iphone', '2018-02-15'),
(9, 'Iphone 5 (2018)', 'Iphone', '2018-01-15'),
(10, 'Iphone 4 (2018)', 'Iphone', '2018-10-05');


ALTER TABLE `product`
  ADD PRIMARY KEY (`product_id`);

ALTER TABLE `product`
  MODIFY `product_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
COMMIT; 

config.php
File ini berfungsi sebagai koneksi kedatabase.

 <?php
$conn = mysqli_connect('localhost', 'root', '', 'search');

if(!$conn){
 die("Error: Failed to connect to database!");
}
?> 

index.php
File ini berisi script untuk menampilkan data database, berisi script filter dan terdapat form data-model sebagai input data.

 <!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
</head>
<body>
 <nav class="navbar navbar-default">
  <div class="ocntainer-fluid">
   <a class="navbar-brand" href="https://www.tutorialswb.com/">TutorialsWB.com</a>
  </div>
 </nav>
 <div class="col-md-3"></div>
 <div class="col-md-6 well">
  <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  <a class="btn btn-info" href="index.php">Display All</a> 
  <br />
  <br />
  <form method="POST" class="form-inline" action="">
   <select name="date" class="form-control" required="required">
    <option value="">Select Month</option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
   </select>
   <button class="btn btn-primary" name="filter"><span class="glyphicon glyphicon-search"></span> Search</button>
  </form>
  <br style="clear:both;"/><br />
  <table class="table table-bordered">
   <thead class="alert-info">
    <tr>
     <th>Product Name</th>
     <th>Supplier Name</th>
     <th>Date</th>
    </tr>
   </thead>
   <tbody>
    <?php include 'filter.php'?>
   </tbody>
  </table>
 </div>
 <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
   <form action="save.php" method="POST">
    <div class="modal-content">
     <div class="modal-body">
      <div class="col-md-2"></div>
      <div class="col-md-8">
       <div class="form-group">
        <label>Product Name</label>
        <input type="text" class="form-control" name="product_name" required="required"/>
       </div>
       <div class="form-group">
        <label>Supplier Name</label>
        <input type="text" class="form-control" name="supplier_name" required="required"/>
       </div>
       <div class="form-group">
        <label>Date</label>
        <input type="date" class="form-control" name="date" required="required"/>
       </div>
      </div>
     </div>
     <div style="clear:both;"></div>
     <div class="modal-footer">
      <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
      <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
     </div>
    </div>
   </form>
  </div>
 </div>
 <script src="js/jquery-3.2.1.min.js"></script>
 <script src="js/bootstrap.js"></script>
</body>
</html> 

filter.php
Script ii berfungsi fungsi inti dari aplikasi sederhana di atas, dimana script ini akan memfilter data bulan sesuai yang kita pilih meudian menampilkannya kedalam tabel.

 <?php
date_default_timezone_set('Asia/Jakarta');
if(!ISSET($_POST['filter'])){
 ?>
 <?php
 require 'config.php';
 $query = mysqli_query($conn, "SELECT * FROM `product` ORDER BY `date` ASC") or die(mysqli_error());
 while($fetch = mysqli_fetch_array($query)){
  ?>
  <tr>
   <td><?php echo $fetch['product_name']?></td>
   <td><?php echo $fetch['supplier']?></td>
   <td><?php echo date('M d, Y', strtotime($fetch['date']))?></td>
  </tr>
  <?php
 }
 ?>
 
 <?php
}
?>


<?php
if(ISSET($_POST['filter'])){
 require 'config.php';
 $date = $_POST['date'];
 $query = mysqli_query($conn, "SELECT * FROM `product` WHERE MONTH(date) = '$date' ORDER BY `date` ASC") or die(mysqli_error());
 while($fetch = mysqli_fetch_array($query)){
  ?>
  <tr>
   <td><?php echo $fetch['product_name']?></td>
   <td><?php echo $fetch['supplier']?></td>
   <td><?php echo date('M d, Y', strtotime($fetch['date']))?></td>
  </tr> 
  <?php  
 }
}
?> 

save.php
Script ini mempunyai fungsi menginput data kedalam database melalui form data-model yang terdapat pada file index.php

 <?php
 require_once 'config.php';
 
 if(ISSET($_POST['save'])){
  $product_name = $_POST['product_name'];
  $supplier_name = $_POST['supplier_name'];
  $date = $_POST['date'];
  
  mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_name', '$supplier_name', '$date')") or die(mysqli_error());
  
  header('location: index.php');
 }
 
?> 

Kalian bisa langsung melihat demo aplikasi sederhana di atas, Kalian juga bisa langsung medownload file dibawah ini.

DEMO        DOWNLOAD

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, jika kalian belum paham atau kalian ingin request post dan project silahkan tinggalkan komentar dibawah.

//lh3.googleusercontent.com/-K5UKWzgJhmU/AAAAAAAAAAI/AAAAAAAACdI/CN3nwiBZsGE/s512-c/photo.jpg

Invoice Simple Menggunakan HTML/CSS

Selamat malam teman-teman kali ini kami akan share invoice simple menggunakan HTML dan CSS. Form invoice sangat dibutuhkan ketika kalian membuat aplikasi penjualan dan pembelian.

Langsung saja berikut source code HTML invoice yang sangat simple.

 <!DOCTYPE html>
<html lang="en" >
<head>
 <meta charset="UTF-8">
 <title>Example Invoice</title>
 <link rel="stylesheet" href="css/style.css">
</head>
<body>
 <html lang="en">
 <head>
  <meta charset='UTF-8'>
  <title>Editable Invoice</title>
 </head>
 <body>
  <div id="page-wrap">
   <textarea id="header">INVOICE</textarea>
   <div id="identity">
    <textarea id="address">Personal blogs contain many tutorials that are very useful hehehe ... 
Phone: (+62) 81524737292</textarea>
    <div id="logo">
     <div id="logoctr">
      <a href="javascript:;" id="change-logo" title="Change logo">Change Logo</a>
      <a href="javascript:;" id="save-logo" title="Save changes">Save</a>
      <a href="javascript:;" id="delete-logo" title="Delete logo">Delete Logo</a>
      <a href="javascript:;" id="cancel-logo" title="Cancel changes">Cancel</a>
     </div>
     <div id="logohelp">
      <input id="imageloc" type="text" size="50" value="" /><br />
      (max width: 540px, max height: 100px)
     </div>
     <img id="image" style ="height:50px"src="https://arman.my.id/Portfolio/img/ttd.png" alt="logo" />
    </div>
   </div>
   <div style="clear:both"></div>
   <div id="customer">
    <textarea id="customer-title">TutorialsWB Personal Website</textarea>
    <table id="meta">
     <tr>
      <td class="meta-head">Invoice #</td>
      <td><textarea>1A2CBS</textarea></td>
     </tr>
     <tr>

      <td class="meta-head">Date</td>
      <td><textarea id="date">October 05, 2018</textarea></td>
     </tr>
     <tr>
      <td class="meta-head">Amount Due</td>
      <td><div class="due">$1500000</div></td>
     </tr>

    </table>
   </div>
   <table id="items">

    <tr>
     <th>Item</th>
     <th>Description</th>
     <th>Unit Cost</th>
     <th>Quantity</th>
     <th>Price</th>
    </tr>

    <tr class="item-row">
     <td class="item-name"><textarea>Building Maintenance Management</textarea><a class="delete" href="javascript:;" title="Remove row"></a></td>

     <td class="description"><textarea>Very useful building repair application hehehe tetet...</textarea></td>
     <td><textarea class="cost">$1000.00</textarea></td>
     <td><textarea class="qty">1</textarea></td>
     <td><span class="price">$1000.00</span></td>
    </tr>

    <tr class="item-row">
     <td class="item-name"><textarea>Car rental Application</textarea><a class="delete" href="javascript:;" title="Remove row">X</a></td>

     <td class="description"><textarea>Car rental and rental application that is very useful hehehe ...</textarea></td>
     <td><textarea class="cost">$200.00</textarea></td>
     <td><textarea class="qty">3</textarea></td>
     <td><span class="price">$600.00</span></td>
    </tr>

    <tr class="item-row">
     <td class="item-name"><textarea>AI application building layout plan</textarea><a class="delete" href="javascript:;" title="Remove row"></a></td>

     <td class="description"><textarea>AI application for building arrangement is very useful hehehe .....</textarea></td>
     <td><textarea class="cost">$100.00</textarea></td>
     <td><textarea class="qty">1</textarea></td>
     <td><span class="price">$100.00</span></td>
    </tr>      

    <tr id="hiderow">
     <td colspan="5"><a id="addrow" href="javascript:;" title="Add a row">Add a row</a></td>
    </tr>

    <tr>
     <td colspan="2" class="blank"> </td>
     <td colspan="2" class="total-line">Subtotal</td>
     <td class="total-value"><div id="subtotal">$1700.00</div></td>
    </tr>
    <tr>

     <td colspan="2" class="blank"> </td>
     <td colspan="2" class="total-line">Total</td>
     <td class="total-value"><div id="total">$1700.00</div></td>
    </tr>
    <tr>
     <td colspan="2" class="blank"> </td>
     <td colspan="2" class="total-line">Amount Paid</td>

     <td class="total-value"><textarea id="paid">$100.00</textarea></td>
    </tr>
    <tr>
     <td colspan="2" class="blank"> </td>
     <td colspan="2" class="total-line balance">Balance Due</td>
     <td class="total-value balance"><div class="due">$1600.00</div></td>
    </tr>
   </table>
   <br><br><br><br>
   <div id="terms">
    <h5>TutorialsWB Personal Website</h5>
    <textarea  >Insert Your Description</textarea>
   </div>
  </div>
 </body>
 </html>
 <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
 <script  src="js/index.js"></script>
</body>
</html>
 

Kedua buat css untuk tampilan form invoice.

 #hiderow,
.delete {
    display: none;
}

* {
    margin: 0;
    padding: 0;
}

body {
    font: 14px/1.4 Georgia, serif;
}

#page-wrap {
    width: 800px;
    margin: 0 auto;
}

textarea {
    border: 0;
    font: 14px Georgia, Serif;
    overflow: hidden;
    resize: none;
}

table {
    border-collapse: collapse;
}

table td,
table th {
    border: 1px solid black;
    padding: 5px;
}

#header {
    height: 15px;
    width: 100%;
    margin: 20px 0;
    background: #222;
    text-align: center;
    color: white;
    font: bold 15px Helvetica, Sans-Serif;
    text-decoration: uppercase;
    letter-spacing: 20px;
    padding: 8px 0px;
}

#address {
    width: 250px;
    height: 150px;
    float: left;
}

#customer {
    overflow: hidden;
}

#logo {
    text-align: right;
    float: right;
    position: relative;
    margin-top: 25px;
    border: 1px solid #fff;
    max-width: 540px;
    max-height: 100px;
    overflow: hidden;
}

#logo:hover,
#logo.edit {
    border: 1px solid #000;
    margin-top: 0px;
    max-height: 125px;
}

#logoctr {
    display: none;
}

#logo:hover #logoctr,
#logo.edit #logoctr {
    display: block;
    text-align: right;
    line-height: 25px;
    background: #eee;
    padding: 0 5px;
}

#logohelp {
    text-align: left;
    display: none;
    font-style: italic;
    padding: 10px 5px;
}

#logohelp input {
    margin-bottom: 5px;
}

.edit #logohelp {
    display: block;
}

.edit #save-logo,
.edit #cancel-logo {
    display: inline;
}

.edit #image,
#save-logo,
#cancel-logo,
.edit #change-logo,
.edit #delete-logo {
    display: none;
}

#customer-title {
    font-size: 20px;
    font-weight: bold;
    float: left;
}

#meta {
    margin-top: 1px;
    width: 300px;
    float: right;
}

#meta td {
    text-align: right;
}

#meta td.meta-head {
    text-align: left;
    background: #eee;
}

#meta td textarea {
    width: 100%;
    height: 20px;
    text-align: right;
}

#items {
    clear: both;
    width: 100%;
    margin: 30px 0 0 0;
    border: 1px solid black;
}

#items th {
    background: #eee;
}

#items textarea {
    width: 80px;
    height: 50px;
}

#items tr.item-row td {
    border: 0;
    vertical-align: top;
}

#items td.description {
    width: 300px;
}

#items td.item-name {
    width: 175px;
}

#items td.description textarea,
#items td.item-name textarea {
    width: 100%;
}

#items td.total-line {
    border-right: 0;
    text-align: right;
}

#items td.total-value {
    border-left: 0;
    padding: 10px;
}

#items td.total-value textarea {
    height: 20px;
    background: none;
}

#items td.balance {
    background: #eee;
}

#items td.blank {
    border: 0;
}

#terms {
    text-align: center;
    margin: 20px 0 0 0;
}

#terms h5 {
    text-transform: uppercase;
    font: 13px Helvetica, Sans-Serif;
    letter-spacing: 10px;
    border-bottom: 1px solid black;
    padding: 0 0 8px 0;
    margin: 0 0 8px 0;
}

#terms textarea {
    width: 100%;
    text-align: center;
}

textarea:hover,
textarea:focus,
#items td.total-value textarea:hover,
#items td.total-value textarea:focus,
.delete:hover {
    background-color: #EEFF88;
}

.delete-wpr {
    position: relative;
}

.delete {
    display: block;
    color: #000;
    text-decoration: none;
    position: absolute;
    background: #EEEEEE;
    font-weight: bold;
    padding: 0px 3px;
    border: 1px solid;
    top: -6px;
    left: -22px;
    font-family: Verdana;
    font-size: 12px;
} 

Berikut adalah tampilan form invoice yang sangat sederhana di atas, Silahkan kembangkan sesuai dengan yang anda perlukan.


Jika kalian tertarik dengan invoice form di atas silahkan download file lengkapnya dibawah ini.


Sekian share kali ini semoga apa yang kami share di atas bermanfaat bagi kalian, Kalian juga bisa request artikel dengan cara tinggalkan komentar dibawah.

//lh3.googleusercontent.com/-K5UKWzgJhmU/AAAAAAAAAAI/AAAAAAAACdI/CN3nwiBZsGE/s512-c/photo.jpg

SUBSCRIBE TO OUR NEWSLETTER