hercioneto

PW2Contatos

Oct 11th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2.     /*
  3.         Arquivo Contatos.class.php
  4.         Irá criar uma classe chamada Contatos que terá como atributos
  5.         nome, email e telefone
  6.     */
  7.    
  8.     class Contatos {
  9.         //criação dos atributos
  10.         private $idContato;
  11.         private $nome;
  12.         private $telefone;
  13.         private $email;
  14.        
  15.         /*criação do método construtor
  16.             é chamado ao criar new (parametros)
  17.         */
  18.         public function __construct($nome,$telefone,$email){
  19.             $this->nome=$nome;
  20.             $this->telefone=$telefone;
  21.             $this->email=$email;
  22.         }
  23.        
  24.         public function getIdContato(){
  25.             return $this->idContato;
  26.         }
  27.        
  28.         public function setIdContato($idContato){
  29.             $this->idContato = $idContato;
  30.         }
  31.        
  32.         public function getNome(){
  33.             return $this->nome;
  34.         }
  35.        
  36.         public function setNome($nome){
  37.             $this->nome = $nome;
  38.         }
  39.        
  40.         public function getTelefone(){
  41.             return $this->telefone;
  42.         }
  43.        
  44.         public function setTelefone($telefone){
  45.             $this->telefone = $telefone;
  46.         }
  47.        
  48.         public function getEmail(){
  49.             return $this->email;
  50.         }
  51.        
  52.         public function setEmail($email){
  53.             $this->email = $email;
  54.         }
  55.        
  56.         public function salvarDados() {
  57.             include "acessa.php";
  58.             $nome = $this->nome;
  59.             $telefone = $this->telefone;
  60.             $email = $this->email;         
  61.             $sql="INSERT INTO contatos (id, nome,telefone,email)
  62.            VALUES ('','$nome','$telefone','$email')";
  63.             if($conexao->query($sql) === false) {
  64.                 trigger_error('Erro na SQL: '.$sql.' Erro: '.
  65.                 $conexao->error, E_USER_ERROR);
  66.             }
  67.             else {
  68.                 echo "Cadastro de novo contato efetuado com sucesso";
  69.                 return $conexao->insert_id;
  70.             }
  71.             $conexao->close();
  72.         }
  73.        
  74.         public function imprimeDados($id){
  75.             include "acessa.php";
  76.             $sql = "Select * from contatos where id = $id";        
  77.             if(!$resultado = $conexao->query($sql)) {
  78.                 trigger_error('Erro na SQL: ' . $sql    . ' Erro: ' .
  79.                 $conexao->error, E_USER_ERROR);
  80.             }
  81.             else { 
  82.                 //vamos listar o cadastro do banco
  83.                 while($linha = $resultado->fetch_assoc()) {
  84.                     $this->id=$linha["id"];
  85.                     $this->nome=$linha["nome"];
  86.                     $this->telefone=$linha["telefone"];
  87.                     $this->email=$linha["email"];
  88.                 }
  89.             }
  90.             $conexao->close();         
  91.         }
  92.        
  93.     }
  94.    
  95.    
  96.     /*
  97.        
  98.         CREATE TABLE IF NOT EXISTS `contatos` (
  99.         `id` int(11) NOT NULL,
  100.         `nome` varchar(200) NOT NULL,
  101.         `telefone` varchar(50) NOT NULL,
  102.         `email` varchar(100) NOT NULL
  103.         ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  104.     */
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment