Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function eg_cadastro_cidade () {
- // Vamos testar a versão do PHP e do WordPress
- // caso as versões sejam antigas, desativamos
- // o nosso plugin.
- if ( version_compare( PHP_VERSION, '5.2.1', '<' )
- or version_compare( get_bloginfo( 'version' ), '3.3', '<' ) ) {
- deactivate_plugins( basename( __FILE__ ) );
- }
- // Vamos checar se a nova tabela existe
- // A propriedade prefix é o prefixo de tabela escolhido na
- // instalação do WordPress
- $tablename = $wpdb->prefix . 'cidade';
- // Se a tabela não existe vamos criá-la
- $sql = "CREATE TABLE ".$tablename." (
- id_cidade mediumint(11) NOT NULL AUTO_INCREMENT,
- nome_cidade varchar(100) NOT NULL,
- estado varchar(50) NOT NULL,
- PRIMARY KEY (id_cidade)
- );";
- // Para usarmos a função dbDelta() é necessário carregar este ficheiro
- require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
- // Esta função cria a tabela na base de dados e executa as otimizações
- // necessárias.
- dbDelta( $sql );
- //insert
- if (isset($_POST['cadastro'])){
- $nome_cidade = $_POST['nome_cidade'];
- $estado = $_POST['estado'];
- if ( ( $nome_cidade == '' ) ||
- ( $estado == '' ) ) {
- $message.= "Preencha todos os campos!";
- }
- else{
- global $wpdb;
- $cidade_results = $wpdb->get_row("SELECT id_cidade FROM ".$wpdb->prefix."cidade WHERE id_cidade = '$id_cidade'");
- if ( empty( $cidade_results ) ) {
- // Registar os cidade na base de dados
- $nova_cidade = array(
- 'nome_cidade' => $nome_cidade,
- 'estado' => $estado,
- );
- // Guardar os valores na tabela
- $inserir = $wpdb->insert( $wpdb->prefix . "cidade", $nova_cidade );
- //$wpdb-> show_errors();
- //$wpdb-> print_error();
- }
- }
- if ($inserir) {
- echo 'Atualizando registro: ' . $wpdb->insert_id;
- ?>
- <script>location.href="<?php echo $_SERVER['REQUEST_URI']; ?>";</script>
- <?php
- } else {
- echo 'Campos obrigatórios em branco!';
- }
- }
- ?>
- <link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/GerePRO/style-admin.css" rel="stylesheet" />
- <div class="wrap">
- <h2>Adicionar Nova Cidade</h2>
- <form id="formulario" method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
- <p>Cadastro de Cidades</p>
- <table class='wp-list-table widefat fixed'>
- <tr><th>Nome</th><td><input type="text" name="nome_cidade" value="<?php echo $nome_cidade;?>"/></td></tr>
- <tr><th>Estado</th><td><input type="text" name="estado" value="<?php echo $estado;?>"/></td></tr>
- </table>
- <input onclick="funcao1()" type='submit' name="cadastro" value='Cadastrar' class='button'>
- </form>
- </div>
- <?php echo '';?>
- <div class="wrap">
- <h2>Lista de Cidades</h2>
- <table class='wp-list-table widefat fixed'>
- <?php
- $lista = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."cidade ");
- if (empty ($lista) ) {
- echo "Sem cidades cadastradas";
- } else {
- ?>
- <?php foreach ( $lista as $cidade_results ) : ?>
- <?php
- $id = $cidade_results->id_cidade;
- echo $id;
- ?>
- <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>
- <tr><th>Estado</th><td><?php echo $cidade_results->estado; ?></td></tr>
- <?php endforeach;
- }
- ?>
- </table>
- </div>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment