Advertisement
Guest User

busca_cep.php

a guest
Feb 6th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php require_once('Connections/db.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $colname_ceps = "-1";
  35. if (isset($_GET['cep'])) {
  36.   $colname_ceps = $_GET['cep'];
  37. }
  38. $row_ceps = array();
  39. mysql_select_db($database_db, $db);
  40. $query_ceps = sprintf("SELECT
  41. (SELECT c.nome FROM cidades c WHERE c.id = cidade_id) AS cidade,
  42. (SELECT b.nome FROM bairros b WHERE b.id = bairro_id) AS bairro,
  43. cep,
  44. (SELECT uf.uf FROM estados uf WHERE uf.id = uf_cod) AS estado,
  45. logracompl
  46. FROM enderecos WHERE cep = %s", GetSQLValueString($colname_ceps, "text"));
  47. $ceps = mysql_query($query_ceps, $db) or die(mysql_error());
  48. $row_ceps = mysql_fetch_assoc($ceps);
  49. $totalRows_ceps = mysql_num_rows($ceps);
  50.     if ($totalRows_ceps > 0){
  51.         $cep_res[] = array(
  52.             'cep'       => $row_ceps['cep'],
  53.             'estado'    => $row_ceps['estado'],
  54.             'cidade'    => utf8_encode($row_ceps['cidade']),
  55.             'endereco'  => utf8_encode($row_ceps['logracompl']),
  56.             'bairro'    => utf8_encode($row_ceps['bairro']));
  57.             echo (json_encode($cep_res));
  58.     } else {
  59.         $query_ceps = sprintf("SELECT c.nome AS cidade,
  60.         (SELECT uf.uf FROM estados uf WHERE uf.id = c.estado_cod) AS estado,
  61.         c.cep
  62.         FROM cidades c
  63.         WHERE c.cep = %s", GetSQLValueString($colname_ceps, "text"));  
  64.         $ceps = mysql_query($query_ceps, $db) or die(mysql_error());
  65.         $row_ceps = mysql_fetch_assoc($ceps);
  66.         $totalRows_ceps = mysql_num_rows($ceps);
  67.             if ($totalRows_ceps > 0){
  68.                 $cep_res[] = array(
  69.                     'cep'       => $row_ceps['cep'],
  70.                     'estado'    => $row_ceps['estado'],
  71.                     'cidade'    => utf8_encode($row_ceps['cidade']));
  72.                     echo (json_encode($cep_res));
  73.             } else {
  74.                 $cep_res[] = array();
  75.                 echo (json_encode($cep_res));
  76.              }
  77.     }
  78.  
  79. mysql_free_result($ceps);
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement