Advertisement
eappereira

Json Example

Apr 30th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. //MySql
  2. CREATE TABLE user
  3.     (
  4.     id serial NOT NULL,
  5.     nome text,
  6.     end text,
  7.     tel numeric(10,2)
  8.     )
  9.  
  10.  
  11. //PHP
  12. <?php
  13.  
  14.     header('Content-Type:' . "text/plain");
  15.      
  16.     $host   = "localhost";
  17.     $user   = "user";
  18.     $pswd   = "pass";
  19.     $dbname = "bd";
  20.     $con    = null;
  21.      
  22.     $con = @pg_connect("host=$host user=$user password=$pswd dbname=$dbname") or die (pg_last_error($con));
  23.      
  24.     //@pg_close($con);
  25.      
  26.     if(!$con) {
  27.         echo '[{"erro": "Não foi possível conectar ao banco"';
  28.         echo '}]';
  29.     }else {
  30.        
  31.         $sql    = "SELECT * FROM user ORDER BY nome";
  32.         $result = pg_query($sql);
  33.         $n      = pg_num_rows($result);
  34.      
  35.         if (!$result) {
  36.             echo '[{"erro": "Há algum erro com a busca. Não retorna resultados"';
  37.             echo '}]';
  38.         }else if($n<1) {
  39.  
  40.             echo '[{"erro": "Não há nenhum dado cadastrado"';
  41.             echo '}]';
  42.         }else {
  43.            
  44.             for($i = 0; $i<$n; $i++) {
  45.                 $dados[] = pg_fetch_assoc($result, $i);
  46.             }
  47.      
  48.             echo json_encode($dados, JSON_PRETTY_PRINT);
  49.         }
  50.     }
  51. ?>
  52.  
  53. //HTML
  54.  
  55. <!DOCTYPE HTML>
  56.     <html lang="pt-br">
  57.         <head>
  58.             <meta charset="UTF-8">
  59.            
  60.             <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
  61.             <script src="script.js" type="text/javascript"></script>
  62.      
  63.         </head>
  64.         <body onload="carregarItens()">
  65.             <section>
  66.                 <h1>EltonDEV</h1>
  67.                 <h2></h2>
  68.                 <table id="minhaTabela">
  69.                     <caption>Cadastro de Clientes</caption>
  70.                     <thead>
  71.                         <th>ID</th>
  72.                         <th>Nome</th>
  73.                         <th>Endereco</th>
  74.                         <th>Telefone</th>
  75.                     </thead>
  76.                     <tbody>
  77.                     </tbody>
  78.                 </table>
  79.             </section>
  80.         </body>
  81.     </html>
  82.  
  83.  
  84. //Json
  85.  
  86. function carregarItens(){
  87.  
  88.         var itens = "", url = "ophpdobanco.php";
  89.      
  90.         $.ajax({
  91.             url: url,
  92.             cache: false,
  93.             dataType: "json",
  94.             beforeSend: function() {
  95.                 $("h2").html("Carregando...");
  96.             },
  97.             error: function() {
  98.                 $("h2").html("Há algum problema com a fonte de dados");
  99.             },
  100.             success: function(retorno) {
  101.                 if(retorno[0].erro){
  102.                     $("h2").html(retorno[0].erro);
  103.                 }
  104.                 else{
  105.  
  106.                     for(var i = 0; i&lt;retorno.length; i++){
  107.                         itens += "
  108.     ";
  109.                         itens += "" + retorno[i].id + "
  110.     "; itens += "" + retorno[i].nome + "
  111.      
  112.     "; itens += "" + retorno[i].end + "
  113.      
  114.     "; itens += "" + retorno[i].tel + "
  115.      
  116.     "; itens += "
  117.      
  118.     ";
  119. }
  120.        $("#minhaTabela tbody").html(itens);
  121.        $("h2").html("Carregado");
  122.            } }
  123.        });
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement