hercioneto

FAEPW2Pessoa.class

Oct 4th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.27 KB | None | 0 0
  1. <?php  
  2.     class Pessoa {
  3.         private $nome;
  4.         private $rua;
  5.         private $numero;
  6.         private $anoNascimento;
  7.         private $ano;
  8.         private $dia;
  9.         private $idade;
  10.         private $id;
  11.        
  12.         public function __construct(){
  13.             $this->nome="";
  14.             $this->rua="";
  15.             $this->numero=0;
  16.             $this->anoNascimento=0;
  17.             $this->idade=0;
  18.         }
  19.        
  20.         public function getId(){
  21.             return $this->id;
  22.         }
  23.        
  24.         public function setId($id){
  25.             $this->id = $id;
  26.         }
  27.        
  28.         public function getNome(){
  29.             return $this->nome;
  30.         }
  31.        
  32.         public function setNome($nome){
  33.             if ($nome=="") { echo "Você deve preencher o nome"; } else {
  34.                 $this->nome = $nome;
  35.             }
  36.         }
  37.        
  38.         public function getRua(){
  39.             return $this->rua;
  40.         }
  41.        
  42.         public function setRua($rua){
  43.             $this->rua = $rua;
  44.         }
  45.        
  46.         public function getNumero(){
  47.             return $this->numero;
  48.         }
  49.        
  50.         public function setNumero($numero){
  51.             $this->numero = $numero;
  52.         }
  53.        
  54.         public function getAnoNascimento(){
  55.             return $this->anoNascimento;
  56.         }
  57.        
  58.         public function setAnoNascimento($anoNascimento){
  59.             $this->anoNascimento = $anoNascimento;
  60.         }
  61.        
  62.         public function getDia(){
  63.             return $this->dia;
  64.         }
  65.        
  66.         public function setDia($dia){
  67.             $this->dia = $dia;
  68.         }
  69.        
  70.         public function getMes(){
  71.             return $this->mes;
  72.         }
  73.        
  74.         public function setMes($mes){
  75.             $this->mes = $mes;
  76.         }
  77.        
  78.         public function getIdade(){
  79.             return $this->idade;
  80.         }
  81.        
  82.         public function setIdade(){
  83.             $anoAtual =date("Y");
  84.             $mesAtual =date("m");
  85.             $diaAtual =date("d");
  86.             $this->idade = $anoAtual-$this->anoNascimento;
  87.             if ($mesAtual>=$this->mes) {
  88.                 if ($diaAtual<=$this->dia) {
  89.                     $this->idade= $this->idade-1;
  90.                 }
  91.             }
  92.         }
  93.        
  94.        
  95.         public function endereco(){
  96.             $nome = $this->nome;
  97.             $rua = $this->rua;
  98.             $numero = $this->numero;
  99.             $string = "$nome mora na $rua número $numero.";
  100.             return $string;
  101.         }
  102.         public function dados(){
  103.             $id = $this->id;
  104.             $nome = $this->nome;
  105.             $rua = $this->rua;
  106.             $numero = $this->numero;
  107.             $anoNascimento = $this->anoNascimento;
  108.             $idade = $this->idade;
  109.             $string = "<div>Nome: $nome | Idade: $idade</div>
  110.            <div>$rua número $numero.<div><div>Ano nascimento: $anoNascimento</div>";
  111.             $string .= "<div> <a href='editarPessoa.php?id=$id'>Editar</a> | ";
  112.             $string .= "<a href='excluirPessoa.php?id=$id'>Excluir</a> </div>";
  113.             return $string;
  114.         }
  115.        
  116.         public function inserirDados() {
  117.             include "acessa.php";
  118.             $nome = $this->nome;
  119.             $rua = $this->rua;
  120.             $numero = $this->numero;
  121.             $anoNascimento = $this->anoNascimento;
  122.             $idade = $this->idade;
  123.             $dia = $this->dia;
  124.             $mes = $this->mes;
  125.            
  126.             $sql="INSERT INTO pessoas (id, nome,rua,numero,anoNascimento,idade)
  127.            VALUES ('','$nome','$rua','$numero','$dia','$mes','$anoNascimento','$idade')";
  128.             if($conexao->query($sql) === false) {
  129.                 trigger_error('Erro na SQL: ' . $sql    . ' Erro: ' .
  130.                 $conexao->error, E_USER_ERROR);
  131.             }
  132.             else {
  133.                 echo "Cadastro de pessoa efetuado com sucesso";
  134.             }
  135.             $conexao->close();
  136.         }
  137.        
  138.         public function listarPessoas(){
  139.             include "acessa.php";
  140.             $sql = "Select * from pessoas";        
  141.             if(!$resultado = $conexao->query($sql)) {
  142.                 trigger_error('Erro na SQL: ' . $sql    . ' Erro: ' .
  143.                 $conexao->error, E_USER_ERROR);
  144.             }
  145.             else { 
  146.                 //vamos listar as pessoas cadastradas no banco
  147.                 while($linha = $resultado->fetch_assoc()) {
  148.                     $this->id=$linha["id"];
  149.                     $this->nome=$linha["nome"];
  150.                     $this->rua=$linha["rua"];
  151.                     $this->numero=$linha["numero"];
  152.                     $this->dia=$linha["dia"];
  153.                     $this->mes=$linha["mes"];
  154.                     $this->anoNascimento=$linha["anoNascimento"];
  155.                     $this->idade=$linha["idade"];
  156.                     echo $this->dados();
  157.                     echo "<hr/><br/>";
  158.                 }
  159.             }
  160.             $conexao->close();         
  161.         }
  162.        
  163.        
  164.         public function excluirPessoa($id) {
  165.             include "acessa.php";
  166.             $sql="Delete from pessoas where id= $id";
  167.             if($conexao->query($sql) === false) {
  168.                 trigger_error('Erro na SQL: ' . $sql    . ' Erro: ' .
  169.                 $conexao->error, E_USER_ERROR);
  170.             }
  171.             else {
  172.                 echo "Pessoa excluida com sucesso";
  173.             }
  174.             $conexao->close();
  175.         }
  176.        
  177.         public function alterarPessoa($id) {
  178.             include "acessa.php";
  179.             $nome = $this->nome;
  180.             $rua = $this->rua;
  181.             $numero = $this->numero;
  182.             $anoNascimento = $this->anoNascimento;
  183.             $idade = $this->idade;
  184.             $dia = $this->dia;
  185.             $mes = $this->mes;
  186.             $sql="Update pessoas set nome='$nome',rua='$rua',numero='$numero',dia='$dia',mes='$mes',anoNascimento='$anoNascimento',idade='$idade' where id = $id";
  187.             if($conexao->query($sql) === false) {
  188.                 trigger_error('Erro na SQL: ' . $sql    . ' Erro: ' .
  189.                 $conexao->error, E_USER_ERROR);
  190.             }
  191.             else {
  192.                 echo "Cadastro de pessoa alterado com sucesso";
  193.             }
  194.             $conexao->close();
  195.         }
  196.        
  197.        
  198.         public function buscarDados($id){
  199.             include "acessa.php";
  200.             $sql = "Select * from pessoas where id = $id";         
  201.             if(!$resultado = $conexao->query($sql)) {
  202.                 trigger_error('Erro na SQL: ' . $sql    . ' Erro: ' .
  203.                 $conexao->error, E_USER_ERROR);
  204.             }
  205.             else { 
  206.                 //vamos listar as pessoas cadastradas no banco
  207.                 while($linha = $resultado->fetch_assoc()) {
  208.                     $this->id=$linha["id"];
  209.                     $this->nome=$linha["nome"];
  210.                     $this->rua=$linha["rua"];
  211.                     $this->numero=$linha["numero"];
  212.                     $this->anoNascimento=$linha["anoNascimento"];
  213.                     $this->idade=$linha["idade"];
  214.                     $this->dia=$linha["dia"];
  215.                     $this->mes=$linha["mes"];
  216.                 }
  217.             }
  218.             $conexao->close();         
  219.         }
  220.        
  221.     }
  222. ?>
Advertisement
Add Comment
Please, Sign In to add comment