How to correctly display an XML that has blank cells in PHP?

Asked

Viewed 146 times

0

Good afternoon, everyone.

I followed a video classroom how to display XML data in PHP, and everything worked out, the result was displayed correctly, but if a cell is empty it plays the value of the next cell in the variable, and this is preventing me to use as intended.

That’s the index, there’s no secret in it:

<!DOCTYPE html>
<html lang="pt-br">
	<head>
		<meta charset="utf-8">
		<title>Importar dados do Excel</title>
	<head>
	<body>
		<h1>Upload Excel</h1>
		
		<form method="POST" action="processa.php" enctype="multipart/form-data">
			<label>Arquivo</label>
			<input type="file" name="arquivo"><br><br>
			<input type="submit" value="Enviar">
		</form>
		
	</body>
</html>

And here is where XML is processed and the result displayed on the screen:

<?php
//$dados = $_FILES['arquivo'];
//var_dump($dados);

if(!empty($_FILES['arquivo']['tmp_name'])){
    $arquivo = new DomDocument();
    $arquivo->load($_FILES['arquivo']['tmp_name']);
    //var_dump($arquivo);

    $linhas = $arquivo->getElementsByTagName("Row");
    //var_dump($linhas);

    $primeira_linha = true;

    foreach($linhas as $linha){
        if($primeira_linha == false){
            $nome = $linha->getElementsByTagName("Data")->item(0)->nodeValue;
            echo "Nome: $nome <br>";

            $email = $linha->getElementsByTagName("Data")->item(1)->nodeValue;
            echo "E-mail: $email <br>";

            $niveis_acesso_id = $linha->getElementsByTagName("Data")->item(2)->nodeValue;
            echo "Nivel de Acesso: $niveis_acesso_id <br>";

            echo "<hr>";
        }
        $primeira_linha = false;
    }
}
?>

This is the table generated with XML:

inserir a descrição da imagem aqui

And the sample result on the page when XML is loaded is this:

Name: Cesar
E-mail: [email protected]
Level of Access: 1

Name: Kelly
E-mail: [email protected]
Level of Access: 1

Name: Jessica E-mail: [email protected]
Level of Access: 2

However, if I go to the spreadsheet and simply remove some E-mail for example, it pushes the value that would be from the access level to the variable, leaving the data visualization like this:

Name: Cesar
E-mail: 1

Notice: Trying to get Property 'nodeValue' of non-object in C: xampp htdocs Somente_import_excel processes.php online 23

Access Level:

Name: Kelly
E-mail: [email protected]
Level of Access: 1

Name: Jessica
E-mail: [email protected]
Level of Access: 2

I don’t know if I explained my doubt well, but if anyone can shed some light on how I can treat when the cell is blank...

  • Ever tried a if($niveis_acesso_id == '' || $niveis_acesso_id == null){&#xA;$niveis_acesso_id = "não tem";&#xA;}

  • Vinicius, I didn’t get to try this because I doubtless echo the variables, and they are getting wrong values if there is an empty cell. Basically the $email variable gets what should go to $niveis_access_id if the email cell is empty.

No answers

Browser other questions tagged

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