1
resposta

Meu Padrão II (Final)

Arquivo objGrid1.php

<?

class obj_grid{

  var $grd_sql;
  var $grd_titulo;
  var $grd_campos;

  function setSQL($inSQL){
    $this->grd_sql = $inSQL;
  }

  function getTitulo(){
    return $this->grd_titulo;
  }

  function getCampo($inInd){
    return $this->grd_campos[$inInd];
  }

  function setTitulo($inTit){
    $this->grd_titulo = $inTit;
  }

  function Grid(){
    $grid = "";

    $db = new obj_db();

    $db->setResult($this->grd_sql);
    if ($db->result){

      $grid = "<SCRIPT>\r\n";
      $grid .= "var strInd = '';\r\n";

      $grid .= "function Marca(inID){\r\n";
/* --------------------------
    Percorre a tabela toda 
   --------------------------
      $grid .= "  var tabela = document.getElementById('myTab');\r\n";
      $grid .= "  var lin;\r\n";
      $grid .= "  var cell;\r\n";
      $grid .= "  for (var i = 0; i <= tabela.rows.length - 1; i++){\r\n";
      $grid .= "    lin = tabela.rows[i];\r\n";
      $grid .= "    for (var j = 1; j <= tabela.rows[i].cells.length - 1; j++){\r\n";
      $grid .= "      cell = lin.cells[j];\r\n";
      $grid .= "      if (cell.style.background == '#5873f9'){\r\n";
      $grid .= "        cell.style.background = '#ffffff';\r\n";
      $grid .= "        cell.style.color = '#000000';\r\n";
      $grid .= "      }\r\n";
      $grid .= "    }\r\n";
      $grid .= "  }\r\n";
   -------------------------- 
 */
      $grid .= "  if (strInd != ''){";
      $grid .= "    ele = document.getElementById(strInd);";
      $grid .= "    ele.style.background = '#ffffff';";
      $grid .= "    ele.style.color = '#000000';\r\n";
      $grid .= "  }";

      $grid .= "  ele = document.getElementById(inID);\r\n";
      $grid .= "  ele.style.background = '#5873f9';\r\n";
      $grid .= "  ele.style.color = '#ffffff';\r\n";
      $grid .= "  strInd = inID;\r\n";

      $grid .= "}\r\n";
      $grid .= "</SCRIPT>\r\n";


      $grid .= "<TABLE CLASS='Grid' ID='myTab'><TR><TD CLASS='Titulo' COLSPAN='".$db->getNumFields()."'>".$this->getTitulo()."</TH></TR>\r\n";

      if (isset($this->grd_campos)){
        $grid .= "<TR>";
        for ($i=1; $i < $db->getNumFields(); $i++){
          $grid .= "<TH>".$this->getCampo($i)."</TH>\r\n";
        }
        $grid .= "</TR>\r\n";
      }

      $ind = 0;
      $primo = "";

      while ($query_data = $db->getRegistro()){  

        $grid .= "<TR>\r\n";

        $grid .= "<TD><A HREF='#' onClick='";

        $sele = "Marca(\"C".$ind."\");Sele(\"";

        for ($i = 0; $i < $db->getNumFields(); $i++){
          $sele .= $query_data[$db->getNomeCampo($i)];
          if ($i < ($db->getNumFields() - 1)){
            $sele .= "\", \"";
          }
        }
        $sele .= "\");";

        if ($ind == 0){
          $primo = $sele;
        }

        $grid .= $sele."'>";


        //$grid .= ");'>";


        $grid .= "<IMG SRC='../images/bu.gif' BORDER='no' onMouseOver=\"this.src='../images/bd.gif'\" onMouseOut=\"this.src='../images/bu.gif'\"></A></TD>";

        for ($i = 1; $i < $db->getNumFields(); $i++){
          if ($i == 1){
            $grid .= "<TD CLASS='Campo' ID='C".$ind."'>".$query_data[$db->getNomeCampo($i)]."</TD>\r\n";
          } else {
            $grid .= "<TD CLASS='Campo'>".$query_data[$db->getNomeCampo($i)]."</TD>\r\n";
          }
        }

        $grid .= "</TR>\r\n";

        $ind ++;

      }
      $grid .= "</TABLE>\r\n";

      // ----------------------------------------
      //  Executa Sele() com o primeiro Registro
      // ----------------------------------------
      $grid .= "<SCRIPT LANGUAGE='javascript' TYPE='text/JavaScript'>".$primo."</SCRIPT>\r\n";
    }
    return $grid;
  }
}
?>
Arquivo objDB.php
<?

class obj_db{

  var $conn;
  var $result;
  var $numFields;
  var $reg;

  function getResult(){
    return $this->result;
  }

  function getNumFields(){
    return $this->numFields;
  }

  function getRegistro(){
    return mysql_fetch_array($this->result);
  }

  function getNomeCampo($inInd){
    return mysql_field_name($this->result, $inInd);
  }

  function obj_db(){
    $this->conn = mysql_connect("RP-15615", "root") or die ("Conexão: ".mysql_error());
    mysql_select_db("obj", $this->conn) or die ("Banco de Dados: ".mysql_error());
  }

  function setResult($inSQL){
    $this->result = mysql_query($inSQL, $this->conn) or die ("DML DB: ".mysql_error());
    if ($this->result){
      $this->numFields = mysql_num_fields($result);
    }
  }

}
?>
1 resposta

duplicada https://cursos.alura.com.br/forum/topico-meu-padrao-i-continua-69290