<?php

class mysqlcategoria {
  private $usuario;
  private $servidor;
  private $password;
  private $base_datos;
  private $conexion;
  private $result;
  var $flag = false;
  static $_instance;
  
  function __construct(){
    require_once ("configdatos.php");
    $this->usuario= USER;
    $this->password= PASS;
    $this->servidor= HOST;
    $this->base_datos= DB;
    $this->conectar();
  }

public function getDetalle(){
     $querycontacto =$this->read("SELECT ID_CATEGORIA, CATEGORIA FROM tblcategorias");
     return $querycontacto;
  }

  public function getResponsableObra(){
   $querycontacto =$this->read("SELECT ID_RESPONSABLEOBRA, RESPONSABLEOBRA FROM tblresponsableobras");
     return $querycontacto;

  }



  public function updatecategoria($id_categoria,$categoria){
     $querycategoria = "UPDATE tblcategorias SET CATEGORIA ='$categoria' WHERE ID_CATEGORIA = '$id_categoria'";
     if(mysql_query($querycategoria)){
        return "1";
     }else{
      return mysql_errno();

     }
  }


  public function updateresponsable($id_responsable,$responsable){
     $querycategoria = "UPDATE tblresponsableobras SET RESPONSABLEOBRA='$responsable'
                               WHERE  ID_RESPONSABLEOBRA = '$id_responsable'";
     if(mysql_query($querycategoria)){
        return "1";
     }else{
      return mysql_errno();

     }
  }


  public function setresponsable($responsable){
      $queryresponsable ="INSERT INTO tblresponsableobras(RESPONSABLEOBRA) VALUES ('$responsable')";
    if(mysql_query($queryresponsable)){
      return "1";
    }else{
      return mysql_error();
    }
  }

   public function setdetalle($detalle){
      $querydetalle ="INSERT INTO tblcategorias(CATEGORIA) VALUES ('$detalle')";
    if(mysql_query($querydetalle)){
      return "1";
    }else{
      return mysql_error();
    }
  }


  public function getCategoria(){
    $querycategoria =$this->read("SELECT s.ID_SUBCATEGORIA, s.ID_CATEGORIA, s.SUBCATEGORIA, 
                                        s.ID_RESPONSABLEOBRA,c.CATEGORIA,r.RESPONSABLEOBRA
                                  FROM tblsubcategorias  s
                                  INNER JOIN tblcategorias c ON c.ID_CATEGORIA = s.ID_CATEGORIA
                                  INNER JOIN tblresponsableobras r ON r.ID_RESPONSABLEOBRA = s.ID_RESPONSABLEOBRA");
     return $querycategoria;

  }

    public function updatedetallecategoria($id_subcategoria,$subcategoria){
     $querycategoria = "UPDATE tblsubcategorias SET SUBCATEGORIA='$subcategoria'
                      WHERE ID_SUBCATEGORIA = '$id_subcategoria'";
     if(mysql_query($querycategoria)){
        return "1";
     }else{
      return mysql_errno();

     }
  }
  public function updatesubcategoria($id_subcategoria,$id_categoria){
     $querycategoria = "UPDATE tblsubcategorias SET ID_CATEGORIA='$id_categoria'
                      WHERE ID_SUBCATEGORIA = '$id_subcategoria'";
     if(mysql_query($querycategoria)){
        return "1";
     }else{
      return mysql_errno();

     }
  }
  public function updatedetalleresponsable($id_subcategoria,$responsable){
     $querycategoria = "UPDATE tblsubcategorias SET ID_RESPONSABLEOBRA='$responsable'
                      WHERE ID_SUBCATEGORIA = '$id_subcategoria'";
     if(mysql_query($querycategoria)){
        return "1";
     }else{
      return mysql_errno();

     }
  }

   public function setsubcategoria($id_categoria,$subcategoria,$id_responsableobra){
      $querysubcategoria ="INSERT INTO tblsubcategorias(ID_CATEGORIA, SUBCATEGORIA, ID_RESPONSABLEOBRA) 
                          VALUES  ('$id_categoria','$subcategoria','$id_responsableobra')";
    if(mysql_query($querysubcategoria)){
      return "1";
    }else{
      return mysql_error();
    }
  }



  




 private function read($sql){
  $resultado = mysql_query($sql); 
   if(mysql_num_rows($resultado)> 0){
     while($fila = mysql_fetch_array($resultado)){
         $arreglo[] =$fila;
       }
         return $arreglo;
   }else{
     return "0";
   }
  }

  private function conectar(){
  $this->conexion = mysql_connect($this->servidor,$this->usuario,$this->password);
  mysql_select_db($this->base_datos,$this->conexion);
  @mysql_query("SET NAMES 'utf8'");
 }


}
?>