Internet

Enviar dados do banco para uma combobox


/*você precisa fazer a conexão  na pagina onde você irá fazer a combobox*/
<?php

 include"conexoes/conectar.php";
 ini_set('default_charset','UTF-8');
?>

/*Na página  onde você criar a combo carregada coloque*/

 <div class="col-xs-6">
     <label for="tipo">Rota:</label>
          <select name="tipo" class="form-control input-sm" id="tipo">
                  <?php
                          $sql = "select rota from tbrota ";
                                $resultado = mysql_query($sql,$conexao);
                                echo"<option value = ''>Selecione</option>";
                                while($dados = mysql_fetch_array($resultado)){
                                  $nome = $dados['rota'];
                                  echo"<option value = '$rota'>$rota</option>";
                                }
                              ?>
                            </select>
                          </div>

----------------------------------------------------------------------------------------------------------------------

Exemplo - Classe PHP

Funcionario.php

<?php
class Funcionario{

private $nome;
private $salarioHora;
private $numHoras;
private $salarioBruto;
private $inss;
private $ir;
private $salarioLiquido;

public function __construct(){

$this->nome = "Ana Maria";
$this->salarioHora = 20;
$this->numHoras = 40;
}

public function setNome($nome){
$this->nome = $nome;
}
public function getNome(){
return $this->nome;
}

public function setSalarioHora($salarioHora){
$this->salarioHora = $salarioHora;
}
public function getSalarioHora(){
return $this->salarioHora;
}

public function setNumHoras($numHoras){
$this->numHoras = $numHoras;
}
public function getNumHoras(){
return $this->numHoras;
}
public function getSalarioBruto(){
return $this->salarioBruto;
}
public function getInss(){
return $this->inss;
}
public function getIr(){
return $this->ir;
}
public function getSalarioLiquido(){
return $this->salarioLiquido;
}
public function calcularBruto(){
$this->salarioBruto = $this->salarioHora * $this->numHoras;
}
public function calcularInss(){
if($this->getSalarioBruto()<=2500):
$this->inss = $this->getSalarioBruto() * 0.11;
else:
$this->inss = $this->getSalarioBruto() * 0.20;
endif;
}

public function calcularIr(){
if($this->getSalarioBruto()<=1903.98){
$this->ir = 0;
}else if($this->getSalarioBruto()>=1903.99 && $this->getSalarioBruto()<=2826.65){
$this->ir = $this->getSalarioBruto() * 0.075;
}else if($this->getSalarioBruto()>=2826.66 && $this->getSalarioBruto()<=3751.05){
$this->ir = $this->getSalarioBruto() * 0.15;
}else if($this->getSalarioBruto()>=3751.06 && $this->getSalarioBruto()<=4664.68){
$this->ir = $this->getSalarioBruto() * 0.225;
}else{
$this->ir = $this->getSalarioBruto() * 0.275;
}

}
public function calcularLiquido(){
$this->salarioLiquido = $this->getSalarioBruto() - $this->getInss() - $this->getIr();
}


}

-----------------------------------------------------------------------------------------------------------------------

contracheque.php

<?php require_once('Funcionario.php');?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Folha PGTO</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <?php
        $func = new Funcionario;

        $func->setNome(isset($_POST['nome']) ? $_POST['nome'] : '');
        $func->setSalarioHora(isset($_POST['sh']) ? $_POST['sh'] : '');
        $func->setNumHoras(isset($_POST['nh']) ? $_POST['nh'] : '');

        $func->calcularBruto();
        $func->calcularInss();
        $func->calcularIr();
        $func->calcularLiquido();
     ?>
    <div class="container">
      <h1>FOLHA DE PAGAMENTO - 2017</h1>
      <hr>
      <p> <?php echo "Nome: ".$func->getNome()?></p>
      <hr>
      <p> <?php echo "+ Salario Bruto: R$ ".$func->getSalarioBruto()?></p>
      <p> <?php echo "- INSS R$ ".$func->getInss()?></p>
      <p> <?php echo "- IR R$ ".$func->getIr()?></p>
      <hr>
      <p> <?php echo "+ Salario a Receber (Liquido)\n R$ ".$func->getSalarioLiquido()?></p>
      <hr>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

-----------------------------------------------------------------------------------------------------------------
index.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Folha PGTO</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
      <h1>FOLHA DE PAGAMENTO - 2017</h1>
      <form action="contracheque.php" method="POST">
        <div class="form-group">
          <label for="Nome">Nome:</label>
          <input type="text" class="form-control" id="nome" name="nome">
        </div>
     
        <div class="form-group">
          <label for="sh">Salário Hora:</label>
          <input type="number" class="form-control" id="sh" name="sh" step="0,5">
        </div>

        <div class="form-group">
          <label for="nh">Número de Horas Trabalhadas:</label>
          <input type="number" class="form-control" id="nh" name="nh">
        </div>

        <button type="submit" class="btn btn-default">Calcular</button>
        <button type="reset" class="btn btn-info">Limpar</button>
      </form>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>


-------------------------------------------------------------------------------------------------------------------------

Pessoal,

Segue abaixo o anexo de um CRUD completo para cadastro de usuário!!!

Donwload

Programa - para utilizar o PDO

EasyPHP 14 -> download



--------------------------------------------------------------------------------------------------------
Exemplo PHP


<?php
require 'conexao.php';

// Recebe o termo de pesquisa se existir
$termo = (isset($_GET['termo'])) ? $_GET['termo'] : '';

// Verifica se o termo de pesquisa está vazio, se estiver executa uma consulta completa
if (empty($termo)):

$conexao = conexao::getInstance();
$sql = 'SELECT id, nome, email, celular, status, foto FROM tab_clientes';
$stm = $conexao->prepare($sql);
$stm->execute();
$clientes = $stm->fetchAll(PDO::FETCH_OBJ);

else:

// Executa uma consulta baseada no termo de pesquisa passado como parâmetro
$conexao = conexao::getInstance();
$sql = 'SELECT id, nome, email, celular, status, foto FROM tab_clientes WHERE nome LIKE :nome OR email LIKE :email';
$stm = $conexao->prepare($sql);
$stm->bindValue(':nome', $termo.'%');
$stm->bindValue(':email', $termo.'%');
$stm->execute();
$clientes = $stm->fetchAll(PDO::FETCH_OBJ);

endif;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
<title>Listagem de Clientes</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/custom.css">
</head>
<body>
<div class='container'>
<fieldset>

<legend><h1>Listagem de Clientes</h1></legend>

<form action="" method="get" id='form-contato' class="form-horizontal col-md-10">
<label class="col-md-2 control-label" for="termo">Pesquisar</label>
<div class='col-md-7'>
    <input type="text" class="form-control" id="termo" name="termo" placeholder="Infome o Nome ou E-mail">
</div>
    <button type="submit" class="btn btn-primary">Pesquisar</button>
    <a href='index.php' class="btn btn-primary">Ver Todos</a>
</form>

<a href='cadastro.php' class="btn btn-success pull-right">Cadastrar Cliente</a>
<div class='clearfix'></div>

<?php if(!empty($clientes)):?>

<table class="table table-striped">
<tr class='active'>
<th>Foto</th>
<th>Nome</th>
<th>E-mail</th>
<th>Celular</th>
<th>Status</th>
<th>Ação</th>
</tr>
<?php foreach($clientes as $cliente):?>
<tr>
<td><img src='fotos/<?=$cliente->foto?>' height='40' width='40'></td>
<td><?=$cliente->nome?></td>
<td><?=$cliente->email?></td>
<td><?=$cliente->celular?></td>
<td><?=$cliente->status?></td>
<td>
<a href='editar.php?id=<?=$cliente->id?>' class="btn btn-primary">Editar</a>
<a href='javascript:void(0)' class="btn btn-danger link_exclusao" rel="<?=$cliente->id?>">Excluir</a>
</td>
</tr>
<?php endforeach;?>
</table>

<?php else: ?>

<h3 class="text-center text-primary">Não existem clientes cadastrados!</h3>
<?php endif; ?>
</fieldset>
</div>
<script type="text/javascript" src="js/custom.js"></script>
</body>
</html>

Nenhum comentário:

Postar um comentário