PHP - Ide Pembuatan Keranjang Belanja Sederhana Dengan PHP

Dalam pembuatan keranjang belanja kali ini idenya adalah anggap saja kita akan membuat situs penjualan barang secara online, dimana kita memerlukan sebuah keranjang belanja untuk menampung semua data dan informasi, untuk kemudian diolah kembali sebelum dikirimkan ke database.

Rancangan sederhananya adalah pengunjung mengakses situs kita dan melihat katalog produk yang ditawarkan. Jika ada barang yang disenangi pengunjung dapat melakukan pembelian secara online dengan menge-klik tombol Beli/Pesan/Buy/Order. Barang yang dipesan muncul dalam keranjang belanja, dimana pengunjung dapat melakukan perubahan baik itu kuantitas jumlah produk, hapus produk maupun tambah produk lain, sebelum melakukan checkout.

Langsung kepraktisnya saja.
Berikut adalah rincian tabel yang akan dibuat :
CREATE TABLE `produk` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`nm` VARCHAR( 20 ) NOT NULL ,
`hrg` VARCHAR( 20 ) NOT NULL
) ENGINE = MYISAM

Berikut adalah isi file koneksi.php
<?
$host ="localhost";
$user = "root"; //isikan dengan nama user, defaultnya "root"
$passwd = ""; //isikan dengan password
$database= ""; //isikan dengan nama database
$connect=mysql_connect($host,$user,$passwd);
if (! $connect){
  echo " Database Tidak Terkoneksi ";
  }
mysql_select_db($database) or die ( " Database Tidak Ada ");
?>

Berikut adalah isi file katalog.php

<?
session_start();
include ('koneksi.php');
include ('fungsi.php');
$no=1;
echo tulisPro();
echo "<table border='1'>";
    echo "<tr>";
        echo "<td>#</td><td>Nama Produk</td><td>Harga</td><td>Opsi</td>";
    echo "</tr>";
    $sql=mysql_query("select*from produk order by id asc");
    while($data=mysql_fetch_array($sql)){
        echo "<tr>";
            echo "<td>$no</td><td>$data[nm]</td><td>$data[hrg]</td><td><a href='keranjang.php?action=add&id=$data[id]'>Pesan</a></td>";
        echo "</tr>";
        $no++;
    }
echo "</table>";
?>

Berikut adalah isi file keranjang.php

<?
session_start();
include ('fungsi.php');
$pro = $_SESSION['pro'];
$action = $_GET['action'];
switch ($action) {
   case 'add':
   if ($pro) {
      $pro .= ','.$_GET['id'];
   } else {
      $pro = $_GET['id'];
   }
   break;
   case 'delete':
   if ($pro) {
      $items = explode(',',$pro);
      $newpro = '';
      foreach ($items as $item) {
         if ($_GET['id'] != $item) {
            if ($newpro != '') {
               $newpro .= ','.$item;
            } else {
               $newpro = $item;
            }
         }
      }
   $pro = $newpro;
   }
   break;
   case 'update':
   if ($pro) {
      $newpro = '';
         foreach ($_POST as $key=>$value) {
            if (stristr($key,'qty')) {
               $id = str_replace('qty','',$key);
               $items = ($newpro != '') ? explode(',',$newpro) : explode(',',$pro);
               $newpro = '';
               foreach ($items as $item) {
                  if ($id != $item) {
                     if ($newpro != '') {
                        $newpro .= ','.$item;
                     } else {
                        $newpro = $item;
                     }
                  }
               }
            for ($i=1;$i<=$value;$i++) {
               if ($newpro != '') {
                  $newpro .= ','.$id;
               } else {
                  $newpro = $id;
               }
            }
         }
      }
   }
   $pro = $newpro;
   break;
}
$_SESSION['pro'] = $pro;
echo lihatPro();
?>

Berikut adalah isi file fungsi.php

<?
include ('koneksi.php');
function tulisPro() {
   $pro = $_SESSION['pro'];
   if (!$pro) {
      echo "<b>0 item</b>";
   } else {
      $items = explode(',',$pro);
      echo "<b><a href='keranjang.php'>".count($items)." item</a></b>";
   }
}
function lihatPro() {
   $no=1;
   $pro = $_SESSION['pro'];
   if ($pro) {
      $items = explode(',',$pro);
      $konten = array();
      foreach ($items as $item) {
         $konten[$item] = (isset($konten[$item])) ? $konten[$item] + 1 : 1;
      }
      $output[] = '<p>Daftar Keranjang</p>';
      $output[] = '<form action="keranjang.php?action=update" method="post">';
      $output[] = '<table border="1">';
      $output[] = '<tr>';
      $output[] = '<td>No</td>';
      $output[] = '<td>Opsi</td>';
      $output[] = '<td>Nama Produk</td>';
      $output[] = '<td>Harga</td>';
      $output[] = '<td>Kuantitas</td>';
      $output[] = '<td>Jumlah Harga</td>';
      $output[] = '</tr>';
      foreach ($konten as $id=>$jumlah) {
         $sql = 'select*from produk where id = '.$id;
         $sq = mysql_query($sql);
         $s = mysql_fetch_array($sq);
         extract($s);
            $output[] = '<tr>';
            $output[] = '<td>'.$no.'</td>';
            $output[] = '<td><a href="keranjang.php?action=delete&id='.$id.'">Hapus</a></td>';
            $output[] = '<td>'.$nm.'</td>';
            $output[] = '<td>'.$hrg.'</td>';
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$jumlah.'" size="2" maxlength="2" /></td>';
            $output[] = '<td>'.($hrg*$jumlah).'</td>';
            $no++;
            $tot += $hrg*$jumlah;
            $output[] = '</tr>';
      }
      $output[] = '</table>';
      $output[] = '<p>Total Harga : <b>'.$tot.'</b></p>';
      $output[] = '<p><input type="submit" value="Update"/></p>';
      $output[] = '</form>';
 $output[] = '<a href="katalog.php">Halaman katalog</a>';
   } else {
      $output[] = '<p>Anda tidak memiliki daftar</p>';
      $output[] = '<a href="katalog.php">Buat daftar</a>';
   }
return join('',$output);
}
?>
Skrip ini masih bisa dikembangkan lebih lanjut, baik konten maupun visual. Selamat mencoba :)


About this entry


0 comments: