Create a preview of an XML file with CodeIgniter

1

I'm creating a stock input / output system using an XML file

To do this, I need to create a preview of the XML content that the user is uploading (as a table, just for the person to know if that is the same XML)

I'm developing in CodeIgniter, receiving the XML by the controller looks like this:

  public function recebeXML(){
        $arquivo = $_FILES['arquivo'];
        $xml = simplexml_load_file($arquivo['tmp_name']);
        $numProdutos = count(/*numero de produtos*/);
        for($i = 0; $i<$numProdutos; $i++){
            $produto = $xml->NFe->det[$i]->prod;

                $dados[$i] = array(
                    "CodigoProduto" => (string)$produto->cProd,
                    "NomeProduto" => (string)$produto->xProd,
                    "Quantidade" => (string)$produto->uCom,
                );
            }
        }

        $this->entradaDados($dados);
    }

The data is placed in the table by means of a foreach.

The question itself is: can you put the organized data into the user view without having to reload the page? I thought about creating a modal to appear after I uploaded, but how to put the files in there?

    
asked by anonymous 30.10.2017 / 17:40

1 answer

2

Basically your code can be reused because it already generates the information, but I will propose a simple example by means of a with :

Organization of Folders

  

XML

<?xmlversion="1.0"?>
<Items>
    <Item>
        <id>1</id>
        <name>São Paulo</name>
    </Item>
    <Item>
        <id>2</id>
        <name>Rio de Janeiro</name>
    </Item>
    <Item>
        <id>3</id>
        <name>Osasco</name>
    </Item>
</Items>

View

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Home</title>
</head>
<body>

<div id="container">
    <h1>Home</h1>
    <div>
        <input type="file" name="arquivo" id="arquivo">
    </div>
    <button id="btnCarregar">Carregar</button>
    <table id="tbl">        
        <tbody>

        </tbody>
    </table>
</div>
    <script src="/public/js/jquery-2.1.0.min.js"></script>
    <script>
        function myDataForm()
        {
            var fdata = new FormData();
            fdata.append('arquivo', $('#arquivo')[0].files[0]);
            return fdata;
        }
        function call_tabela(result)
        {
            var tbody = $("#tbl tbody");
            tbody.empty();
            $.each(result, function(index, item){
                tbody.append('<tr>')
                tbody.append('<td>'+item.id+'</td>');
                tbody.append('<td>'+item.name+'</td>');
                tbody.append('<tr>');
            });         
        }
        $(document).ready(function(){
            $("#btnCarregar").click(function(){
                $.ajax({
                    url: '/home/receive',
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    data: myDataForm(),
                    success: function(result) 
                    {                       
                        call_tabela(result.Item);
                    },
                    error: function(xhr, ajaxOptions, thrownError) 
                    {
                        console.log(xhr);
                    }
                });         
            });
        });
    </script>
</body>
</html>

Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller 
{
    public function index()
    {
        $this->load->view('home');
    }

    public function receive()
    {
        $values = simplexml_load_file($_FILES['arquivo']['tmp_name']);
        echo json_encode($values);
    }
}

Basically this code makes the choice of an xml of your preference in my case I put an example in the response itself and this is the example and the home screen:

  

Afterdisplayingthefileandhavingtoloadtheinitialscreenloadsthedatawithoutrefresh:

  

    
30.10.2017 / 18:45