Create a preview of an XML file with Codeigniter

Asked

Viewed 346 times

1

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

For 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 it is that XML even)

I am developing in Codeigniter, the receipt of XML by the controller is 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 through a foreach.

The question itself is : is there any way to put organized data into the user’s view without having to reload the page? I thought of creating a modal to appear after I uploaded, but how to put the files inside?

  • You wanted something with Ajax, without refreshing the page? IE, this code already works, but, you want to do with a better interaction?

  • Exactly that, I just don’t know how to proceed with it

  • Have to do a post with ajax passing the XML file as you already do traditionally and in the response of ajax rescue this data and show in a table, for example with Angular would be easy to mount this, but, are steps to be followed.

  • Do you know of any site or forum that detail more how to do this? I know absolutely nothing about AJAX

  • Here on the site there is a lot of link about ajax (https://answall.com/questions/tagged/ajax) just give a read, I can even put together a minimum example who knows if you can use as an example and pass it to your code.

  • Could be, would help a lot!

Show 1 more comment

1 answer

2


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

Organization of Folders

inserir a descrição da imagem aqui

XML

<?xml version="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 answer itself and this is the example and the home screen:

inserir a descrição da imagem aqui

after displaying the file and having the Start screen load the data without refresh:

inserir a descrição da imagem aqui

  • 1

    Very good! I will try to use

  • If useful @Walterfelipe accepted as answer!

  • I’ll do it, I just have a question in the call_table() ?

  • @Walterfelipe create the Rows that come from PHP! or the tr and td in tbody of that table!

  • it is giving error in the folder directory of the controller... can you pass me your folder organization? just so I have a notion

  • @Walterfelipe is in the edited answer. What is the error?

  • It has already been solved, I put the way you wrote in the reply and it worked perfectly

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.