<?php
require_once ("kontrolDB.php");

class kriteria_nilai
{
	private $db_handle;
	
    public function __construct()
    {
       $this->db_handle = new kontrolDB();
    }
    
    public function tampil($kriteria=0)
    {        
		$_sql=($kriteria==0) ? "" : "AND b.id_kriteria=$kriteria";
		$sql = "SELECT a.*, b.kriteria FROM knn_kriteria_nilai a, knn_kriteria b 
				WHERE a.id_kriteria=b.id_kriteria ".$_sql." ORDER BY a.id_kriteria_nilai";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
	
    public function tampilById($id)
    {        
		$query = "SELECT * FROM knn_kriteria_nilai WHERE id_kriteria_nilai=?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }
	
    public function cekkriteria_nilai($data, $pilih)
    {     
		$query=""; $paramType=""; $paramValue=array();  
		if($pilih==1) {
			$query = "SELECT * FROM knn_kriteria_nilai WHERE id_kriteria=? AND nilai=?";
			$paramType = "is";
			$paramValue = array(
				$data[0], $data[1], 
			);
		}elseif($pilih==2) {
			$query = "SELECT * FROM knn_kriteria_nilai WHERE id_kriteria=? AND nilai=? AND id_kriteria_nilai<>?";
			$paramType = "isi";
			$paramValue = array(
				$data[0], $data[1], $data[2]
			);
		}
        
        $result = $this->db_handle->cekData($query, $paramType, $paramValue);
        return $result;
    }
		
    public function tambah($data=array()) 
    {
        $query = "INSERT INTO knn_kriteria_nilai (id_kriteria, nilai, bobot) VALUES (?, ?, ?)";
        $paramType  = "isi";
        $paramValue = array();
		$paramValue = $data;
        
        $insertId   = $this->db_handle->insert($query, $paramType, $paramValue);
        
        if ($insertId>0) {
			return true;
        } else {
            echo 'Error: cannot execute the command';
            return false;
        }        
    }
	
    public function ubah($data=array(), $id) 
    {
		$query = "UPDATE knn_kriteria_nilai SET id_kriteria=?,nilai=?,bobot=? WHERE id_kriteria_nilai=?";
		$paramType  = "isii";
		$paramValue = array($data[0],$data[1],$data[2],$id);        
		
		$result = $this->db_handle->update($query, $paramType, $paramValue);
        if ($result==false) {
            echo 'Error: cannot execute the command';
            return false;
        } else {
            return true;
        } 
    }
		
    public function hapus($id) {
        $query = "DELETE FROM knn_kriteria_nilai WHERE id_kriteria_nilai= ?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        $result = $this->db_handle->update($query, $paramType, $paramValue);
		return $result;
    }
	
	public function escapeString($value)
    {
        return $this->db_handle->escape_string($value);
    }
    
}
?>