0
I have a Wordpress plugin, which can be used on multiple blogs with the same configuration.
I have two tables in it: wp_tabela_um and wp_tabela_2
How can I create a button to export these tables and a field to import in case of a new installation.
0
I have a Wordpress plugin, which can be used on multiple blogs with the same configuration.
I have two tables in it: wp_tabela_um and wp_tabela_2
How can I create a button to export these tables and a field to import in case of a new installation.
1
Despite the question a little vague and made my answer get big I created a solution.
As you know which is the table the fields and their attributes, we will first select the records, create the files and then insert them into the database. I will create just the example with wp_tabela_um then just replicate to too many tables.
Create a button <a href="gerarArquivo.php">criar arquivos</a> simple to generate the files.
gerarArquivo.php
// selecionamos os registros
    $quer_wp_tabela_um = mysqli_query($mysqli, "SELECT * FROM wp_tabela_um");
    // cria o arquivo wp_tabela_um
    while($registro = mysqli_fetch_array($quer_wp_tabela_um)){
        // abaixo criamos o arquivo se ele não existir COM DATA
        // o a+ indica que o arquivo é para escrita e leitura
        $arquivo = fopen("arquivos/wp_tabela_um".date("d-m-Y").".txt", "a+");
        // aqui você coloca os campos dos registros
        $campo1 = $registro['id'];
        $campo2 = $registro['nome'];
        $campo3 = $registro['email'];
        $campo4 = $registro['telefone'];
        // string completa com delimitador (;) e quebra de linha \n
        $string = $campo1.";".$campo2.";".$campo3.";".$campo4."\n";
        if ($arquivo) {
            // aqui escreve linha por linha no arquivo
            fwrite($arquivo, $string);
        }
        fclose($arquivo);
    }
Now these files are in the directory arquivo of your website.
With these files we send to a certain database at once with a submission form:
formulaDenvio.php
<form action="importarArquivos.php" method="post" enctype="multipart/form-data">
    <input type="file" name="arquivo"/>
    <input type="submit"/> 
</form>
importArchives.php
$arquivos = $_FILES['arquivo'];
    // caminho com o diretório do arquivo
    $file = "arquivos/".$arquivos['name'];
    // salve o arquivo no diretório
    move_uploaded_file($arquivo['tmp_name'], $file);
    // abre o arquivo apenas para leitura
    $arquivo = fopen($file, 'r');
    // vamos ler o arquivo linha por linha e registra-lo
    while(!feof($arquivo)){
        //pega a linha atual
        $linha = fgets($arquivo);
        $registro =  explode(";" , $linha);
        $id = $registro[0];
        $nome = $registro[1];
        $email = $registro[2];
        $telefone = $registro[3];
        //insere o registro
        mysqli_query($mysqli, "INSERT INTO wp_tabela_um (id, nome, email, telefone) VALUES ($id, $nome, $email, $telefone)");
    }
This is just one example! For there are many ways to do
Browser other questions tagged php mysql database wordpress
You are not signed in. Login or sign up in order to post.
Your question is very vague, more details than you need and what you already have.
– DaviAragao
you want to export only tables or thebes and records?
– Andrei Coelho
Andrei, I need to export the records, because when the user is importing the table will already be created in wordpress from it.
– Anderson Costa