<?php
class banco{
//funcao para conectar ao banco de dados
//recebe uma string como parametro
function conecta($sql){
         
$conexao mysql_connect("CAMINHO DO BANCO","USER","PWD") or die ("Erro ao conectar ao Banco de Dados.");
         
$db mysql_select_db("NOME DO BANCO",$conexao);
         
$result mysql_query($sql,$conexao);
         
//echo "<br><strong> ".$sql;
         
mysql_close($conexao);
         return 
$result;
    }
    
//funcao para realizar consultas simples no banco de dados.
//recebe dois parametros, a tabela afetada e como deseja
//ordenar
function simpleSelect($table,$order){
    
$sql "SELECT * FROM ".$table." ORDER BY ".$order."";
    
$result = @$this->conecta($sql);
    
//echo $sql;
    
return $result;
}


//funcao para selecionar um registro em uma determinada table
//recebe 3 parametros
//@nome table, @idTable, @id
function selectUnico($table,$idTable,$id){
    
$sql    =    "SELECT * FROM ".$table." WHERE ".$idTable" = ".$id;
    
//echo $sql;
    
$result $this->conecta($sql);
    return 
$result;
}

//funcao para realizar insercoes em apenas uma tabela,
//recebe 3 paramentros, o nome da tabela afetada, um vetor com os
//dados a serem inseridos  e um vetor com os campos afetados na tabela.
function insertSimple($table,$vetDados,$vetCampos){
    
$into "";
    
$value "";
    
$virgula "";
    for(
$I 0$I sizeof($vetDados); $I++){
        
$into  .= $virgula.$vetCampos[$I];
        
$value .= $virgula."'".$vetDados[$I]."'";
        
$virgula ",";
    }
    
$insert "INSERT INTO ".$table."(".$into.") values (".$value.")";
    
//echo $insert."<br>";
    
$this->conecta($insert);
}

//funcao para realizar updates em apenas uma tabela,
//recebe 5 paramentros
function updateSimple($table,$vetDados,$vetCampos,$cmpId,$id){
    
$set "";
    
$value "";
    
$virgula "";
    for(
$I 0$I sizeof($vetDados); $I++){
        
$set  .= $virgula.$vetCampos[$I]."= '".$vetDados[$I]."'";
        
//$value .= $virgula."'".$vetDados[$I]."'";
        
$virgula ",";
    }
    
$up "UPDATE ".$table." SET ".$set." WHERE ".$cmpId" = '".$id."'";
    
//Echo $up."<br>";
    
$this->conecta($up);
}



//funcao que faz consula em 2 tables 
//recebe como parametro as tabelas e os id's para cruzamento
//tudo em um vetor
function selectTwoTable($vetorTable,$umRegistro){
    if(!
$umRegistro){
    
$select "SELECT * FROM ".$vetorTable['table1'].",".$vetorTable['table2']."
               WHERE "
.$vetorTable['table1'].".".$vetorTable['idTable1']." = ".$vetorTable['table2'].".".$vetorTable['idTable2']."";
    }
    if(
$umRegistro){
    
$select "SELECT DISTINCT * FROM ".$vetorTable['table1'].",".$vetorTable['table2']."
               WHERE "
.$vetorTable['table1'].".".$vetorTable['idTable1']." = ".$vetorTable['table2'].".".$vetorTable['idTable2']."
               AND "
.$vetorTable['table2'].".".$vetorTable['idIndexTable2']." = '".$vetorTable['valorIndex']."' ";
    }
    
    
$result $this->conecta($select);
    
//echo $select;
    
return $result;
}

//funcao para pegar o ultimo id da tabela em questao
function pegaUltimoId($table,$id){
    
$sql    "SELECT * FROM ".$table" ORDER BY  ".$id." DESC LIMIT 1";
    
//echo $sql."<br>";
    
$result    $this->conecta($sql);
    return 
$result;
}

//funcao para selecionar em uma unica tabela
//mas usando um filtro
function selectFiltro($table,$idTable,$id){
    
$sql "SELECT * FROM ".$table." WHERE ".$idTable" = '".$id."'";
    
$rs     $this->conecta($sql);
    
//echo $sql."<br>";
    
return $rs;
}


//funcoa para deleter um registro em uma tabela
//recebe tres  paramentros
//@table,@idTable, @id
function delRegistro($table,$idTable,$id){
    
$del    "DELETE FROM ".$table." WHERE ".$idTable" = ".$id;
    
$this->conecta($del);
    
//echo $del;
}

//funcao para pegar a quantidade do estoque de um determinadp
//produto, recebe o id como paramentro
function pegaTotalProduto($idProduto){
    
$total $this->selectUnico("produto","idProduto",$idProduto);
    
$total mysql_fetch_array($total);
    
$tProduto $total['estoque'];
    return 
$tProduto;
}
//funcao para subtrair a quantidade de estoque de um determinado
//produto, recebe o id e a quantidade a ser subtraida    
function subtraiQtdeEstoque($idProduto,$quantidade){
    
$total      $this->pegaTotalProduto($idProduto);
    
$totalAtu $total $quantidade;
    return 
$totalAtu;
}


    
}
?>