Ir para conteúdo
Fórum Script Brasil

ezzcardoso

Membros
  • Total de itens

    2
  • Registro em

  • Última visita

Posts postados por ezzcardoso

  1. Saudações, segue código que acabei de fazer para meu projeto.

     

    <?php
    class Built
    {
        protected $tableClass;
        protected $tableId;
        public function table($array)
        {
            $class = $this->tableClass;
            $id    = $this->tableId;
            $table = "<table class='{$class}' id='{$id}'>";
            $table .= $this->tableHead($array);
            $table .= $this->tableBody($array);
            $table .= '</table>';
            return $table;
        }
        private function tableHead(array $array)
        {
            $tableHead = '<thead>';
            foreach ($array as $x) {
                $tableHead .= '<tr>';
                foreach ($x as $k => $y) {
                    $tableHead .= '<th>' . ucfirst($k) . '</th>';
                }
                $tableHead .= '</tr>';
                break;
            }
            $tableHead .= '</thead>';
            return $tableHead;
        }
        private function tableBody(array $array)
        {
            $tableBody = '<tbody>';
            foreach ($array as $x) {
                $tableBody .= '<tr>';
                foreach ($x as $y) {
                    $tableBody .= '<td>' . $y . '</td>';
                }
                $tableBody .= '</tr>';
            }
            $tableBody .= '</tbody>';
            return $tableBody;
        }
        public function table_set_class($class)
        {
            $this->tableClass = $class;
        }
        public function table_set_id($id)
        {
            $this->tableId = $id;
        }
    }
    
    
    ?>

    Esse código faz com que  seja criada  automática mente pela query

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        
      $args = array();
        while($row = $result->fetch_object()) {
            $args[]= $row;
        }
      
    } else {
        echo "0 results";
    }
    
    $conn->close();
    ?>

     agora usando o Script pra gerar a tabelas 

     

    // Modo de USAR
    $built        = new Built();
    $built->table_set_class("table table-striped");// class da tabela 
    $built->table_set_id("Id_table"); // IID... se quiser
    echo $built->table($args);// cria a query na já com a tabela

     

×
×
  • Criar Novo...