Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Pessoa {
- private $nome;
- private $rua;
- private $numero;
- private $anoNascimento;
- private $ano;
- private $dia;
- private $idade;
- private $id;
- public function __construct(){
- $this->nome="";
- $this->rua="";
- $this->numero=0;
- $this->anoNascimento=0;
- $this->idade=0;
- }
- public function getId(){
- return $this->id;
- }
- public function setId($id){
- $this->id = $id;
- }
- public function getNome(){
- return $this->nome;
- }
- public function setNome($nome){
- if ($nome=="") { echo "Você deve preencher o nome"; } else {
- $this->nome = $nome;
- }
- }
- public function getRua(){
- return $this->rua;
- }
- public function setRua($rua){
- $this->rua = $rua;
- }
- public function getNumero(){
- return $this->numero;
- }
- public function setNumero($numero){
- $this->numero = $numero;
- }
- public function getAnoNascimento(){
- return $this->anoNascimento;
- }
- public function setAnoNascimento($anoNascimento){
- $this->anoNascimento = $anoNascimento;
- }
- public function getDia(){
- return $this->dia;
- }
- public function setDia($dia){
- $this->dia = $dia;
- }
- public function getMes(){
- return $this->mes;
- }
- public function setMes($mes){
- $this->mes = $mes;
- }
- public function getIdade(){
- return $this->idade;
- }
- public function setIdade(){
- $anoAtual =date("Y");
- $mesAtual =date("m");
- $diaAtual =date("d");
- $this->idade = $anoAtual-$this->anoNascimento;
- if ($mesAtual>=$this->mes) {
- if ($diaAtual<=$this->dia) {
- $this->idade= $this->idade-1;
- }
- }
- }
- public function endereco(){
- $nome = $this->nome;
- $rua = $this->rua;
- $numero = $this->numero;
- $string = "$nome mora na $rua número $numero.";
- return $string;
- }
- public function dados(){
- $id = $this->id;
- $nome = $this->nome;
- $rua = $this->rua;
- $numero = $this->numero;
- $anoNascimento = $this->anoNascimento;
- $idade = $this->idade;
- $string = "<div>Nome: $nome | Idade: $idade</div>
- <div>$rua número $numero.<div><div>Ano nascimento: $anoNascimento</div>";
- $string .= "<div> <a href='editarPessoa.php?id=$id'>Editar</a> | ";
- $string .= "<a href='excluirPessoa.php?id=$id'>Excluir</a> </div>";
- return $string;
- }
- public function inserirDados() {
- include "acessa.php";
- $nome = $this->nome;
- $rua = $this->rua;
- $numero = $this->numero;
- $anoNascimento = $this->anoNascimento;
- $idade = $this->idade;
- $dia = $this->dia;
- $mes = $this->mes;
- $sql="INSERT INTO pessoas (id, nome,rua,numero,anoNascimento,idade)
- VALUES ('','$nome','$rua','$numero','$dia','$mes','$anoNascimento','$idade')";
- if($conexao->query($sql) === false) {
- trigger_error('Erro na SQL: ' . $sql . ' Erro: ' .
- $conexao->error, E_USER_ERROR);
- }
- else {
- echo "Cadastro de pessoa efetuado com sucesso";
- }
- $conexao->close();
- }
- public function listarPessoas(){
- include "acessa.php";
- $sql = "Select * from pessoas";
- if(!$resultado = $conexao->query($sql)) {
- trigger_error('Erro na SQL: ' . $sql . ' Erro: ' .
- $conexao->error, E_USER_ERROR);
- }
- else {
- //vamos listar as pessoas cadastradas no banco
- while($linha = $resultado->fetch_assoc()) {
- $this->id=$linha["id"];
- $this->nome=$linha["nome"];
- $this->rua=$linha["rua"];
- $this->numero=$linha["numero"];
- $this->dia=$linha["dia"];
- $this->mes=$linha["mes"];
- $this->anoNascimento=$linha["anoNascimento"];
- $this->idade=$linha["idade"];
- echo $this->dados();
- echo "<hr/><br/>";
- }
- }
- $conexao->close();
- }
- public function excluirPessoa($id) {
- include "acessa.php";
- $sql="Delete from pessoas where id= $id";
- if($conexao->query($sql) === false) {
- trigger_error('Erro na SQL: ' . $sql . ' Erro: ' .
- $conexao->error, E_USER_ERROR);
- }
- else {
- echo "Pessoa excluida com sucesso";
- }
- $conexao->close();
- }
- public function alterarPessoa($id) {
- include "acessa.php";
- $nome = $this->nome;
- $rua = $this->rua;
- $numero = $this->numero;
- $anoNascimento = $this->anoNascimento;
- $idade = $this->idade;
- $dia = $this->dia;
- $mes = $this->mes;
- $sql="Update pessoas set nome='$nome',rua='$rua',numero='$numero',dia='$dia',mes='$mes',anoNascimento='$anoNascimento',idade='$idade' where id = $id";
- if($conexao->query($sql) === false) {
- trigger_error('Erro na SQL: ' . $sql . ' Erro: ' .
- $conexao->error, E_USER_ERROR);
- }
- else {
- echo "Cadastro de pessoa alterado com sucesso";
- }
- $conexao->close();
- }
- public function buscarDados($id){
- include "acessa.php";
- $sql = "Select * from pessoas where id = $id";
- if(!$resultado = $conexao->query($sql)) {
- trigger_error('Erro na SQL: ' . $sql . ' Erro: ' .
- $conexao->error, E_USER_ERROR);
- }
- else {
- //vamos listar as pessoas cadastradas no banco
- while($linha = $resultado->fetch_assoc()) {
- $this->id=$linha["id"];
- $this->nome=$linha["nome"];
- $this->rua=$linha["rua"];
- $this->numero=$linha["numero"];
- $this->anoNascimento=$linha["anoNascimento"];
- $this->idade=$linha["idade"];
- $this->dia=$linha["dia"];
- $this->mes=$linha["mes"];
- }
- }
- $conexao->close();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment