Charles_Mad

nova-cidade.php

Jun 10th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. <?php
  2. function eg_cadastro_cidade () {
  3.  
  4. // Vamos testar a versão do PHP e do WordPress
  5. // caso as versões sejam antigas, desativamos
  6. // o nosso plugin.
  7. if ( version_compare( PHP_VERSION, '5.2.1', '<' )
  8. or version_compare( get_bloginfo( 'version' ), '3.3', '<' ) ) {
  9. deactivate_plugins( basename( __FILE__ ) );
  10. }
  11.  
  12. // Vamos checar se a nova tabela existe
  13. // A propriedade prefix é o prefixo de tabela escolhido na
  14. // instalação do WordPress
  15.  
  16. $tablename = $wpdb->prefix . 'cidade';
  17.  
  18. // Se a tabela não existe vamos criá-la
  19. $sql = "CREATE TABLE ".$tablename." (
  20. id_cidade mediumint(11) NOT NULL AUTO_INCREMENT,
  21. nome_cidade varchar(100) NOT NULL,
  22. estado varchar(50) NOT NULL,
  23. PRIMARY KEY (id_cidade)
  24. );";
  25.  
  26. // Para usarmos a função dbDelta() é necessário carregar este ficheiro
  27. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  28.  
  29. // Esta função cria a tabela na base de dados e executa as otimizações
  30. // necessárias.
  31. dbDelta( $sql );
  32.  
  33. //insert
  34. if (isset($_POST['cadastro'])){
  35.  
  36. $nome_cidade = $_POST['nome_cidade'];
  37. $estado = $_POST['estado'];
  38.  
  39. if ( ( $nome_cidade == '' ) ||
  40. ( $estado == '' ) ) {
  41. $message.= "Preencha todos os campos!";
  42. }
  43.  
  44. else{
  45.  
  46. global $wpdb;
  47.  
  48. $cidade_results = $wpdb->get_row("SELECT id_cidade FROM ".$wpdb->prefix."cidade WHERE id_cidade = '$id_cidade'");
  49.  
  50. if ( empty( $cidade_results ) ) {
  51. // Registar os cidade na base de dados
  52. $nova_cidade = array(
  53. 'nome_cidade' => $nome_cidade,
  54. 'estado' => $estado,
  55. );
  56.  
  57. // Guardar os valores na tabela
  58. $inserir = $wpdb->insert( $wpdb->prefix . "cidade", $nova_cidade );
  59.  
  60.  
  61. //$wpdb-> show_errors();
  62. //$wpdb-> print_error();
  63.  
  64. }
  65.  
  66. }
  67. if ($inserir) {
  68. echo 'Atualizando registro: ' . $wpdb->insert_id;
  69. ?>
  70.  
  71. <script>location.href="<?php echo $_SERVER['REQUEST_URI']; ?>";</script>
  72. <?php
  73.  
  74. } else {
  75. echo 'Campos obrigatórios em branco!';
  76. }
  77. }
  78.  
  79. ?>
  80.  
  81.  
  82. <link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/GerePRO/style-admin.css" rel="stylesheet" />
  83. <div class="wrap">
  84. <h2>Adicionar Nova Cidade</h2>
  85.  
  86. <form id="formulario" method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
  87. <p>Cadastro de Cidades</p>
  88. <table class='wp-list-table widefat fixed'>
  89. <tr><th>Nome</th><td><input type="text" name="nome_cidade" value="<?php echo $nome_cidade;?>"/></td></tr>
  90. <tr><th>Estado</th><td><input type="text" name="estado" value="<?php echo $estado;?>"/></td></tr>
  91. </table>
  92. <input onclick="funcao1()" type='submit' name="cadastro" value='Cadastrar' class='button'>
  93. </form>
  94. </div>
  95.  
  96. <?php echo '';?>
  97.  
  98. <div class="wrap">
  99. <h2>Lista de Cidades</h2>
  100. <table class='wp-list-table widefat fixed'>
  101. <?php
  102. $lista = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."cidade ");
  103. if (empty ($lista) ) {
  104. echo "Sem cidades cadastradas";
  105. } else {
  106.  
  107. ?>
  108. <?php foreach ( $lista as $cidade_results ) : ?>
  109. <?php
  110. $id = $cidade_results->id_cidade;
  111. echo $id;
  112. ?>
  113. <tr class="cor1"><th>Nome:</th><td><a href="admin.php?page=eg_update_cidade&id=<?php echo $id;?>"><?php echo $cidade_results->nome_cidade; ?></a></td></tr>
  114. <tr><th>Estado</th><td><?php echo $cidade_results->estado; ?></td></tr>
  115.  
  116. <?php endforeach;
  117. }
  118. ?>
  119.  
  120. </table>
  121. </div>
  122.  
  123. <?php
  124.  
  125.  
  126.  
  127. }
  128.  
  129. ?>
Advertisement
Add Comment
Please, Sign In to add comment