Error in forming a string through a Loop (with Foreach) in an Array

Asked

Viewed 58 times

1

Hello.

I have a code that makes a Foreach in an Array that forms a String (in a variable) with all the values of this Array.

The problem is that when I echo this String, at the end of the Loop, no value is displayed. echo gives an empty output.

However, if I take out the "<" and ">", the String is displayed normally. with the values of all the indexes of the Array.

I’ll leave my code:

<?php

//CLASSE PARA PEGAR TODOS OS USUÁRIOS QUE NÃO FOREM DE DESENVOLVIMENTO
	class PegarFuncionarios
	{				
		private $dep;	
		
		public function __get($atributo)
		{			
			return $this->$atributo;
		}
		
		public function __set($atributo, $valor)
		{			
			$this->$atributo = $valor;
		}		
							
		public function todosFuncionarios()
		{	
			try
			{			
				//Conexão com o Banco de Dados (Futuramente podemos atribuir essa conexao a uma classe
				$d = new Conexao();
				$conexao = $d->conectar();
								
				$query = "SELECT usuario FROM tbl_usuarios WHERE departamento <> :dep ORDER BY usuario";
				
				$stmt = $conexao->prepare($query);
				$stmt->bindValue(':dep',$this->__get('dep'));
				$stmt->execute();	
				$lista_usuarios = $stmt->fetchAll(PDO::FETCH_ASSOC);			
				
				//$retorno_usuarios = array();
				
				$retorno_usuarios = "";
				
				foreach($lista_usuarios as $key => $linha_usuarios)
				{		
					$retorno_usuarios .= "<". $linha_usuarios['usuario'] .">";						
				} 						
				
				echo $retorno_usuarios;
				
			}
			catch(PDOException $e)
			{
				//Verificando o erro ocorrido
				echo "Erro: ".$e->getCode()." Mensagem: ".$e->getMessage();				
			}
		}		
	}
  
  
?>

Without the PDO, in another part of the system, I make a similar code and it works (The problem is that now I’m wanting to use PDO with OO in the codes. To get better organized and safe). See below the code that works with mysqli:

<?php

$pesquisar_interessados = "SELECT usuario FROM tbl_usuarios WHERE (administrador = 1 AND USUARIO <> '$usuario_logado') OR (DEPARTAMENTO = '$usuario_departamento' AND USUARIO <> '$usuario_logado' AND SUPERVISOR = 1)";
			$operacao_interessados = mysqli_query($conecta, $pesquisar_interessados) or die("Erro no Select interessados");	
					
			$resultado = "";
			
			while ($row = mysqli_fetch_array($operacao_interessados))
			{					
				$resultado .= "<".$row['usuario'].">";			
			}		




?>

Would you know where I’m going wrong? It seems to be a very basic mistake.

1 answer

2


Thiago, he’s thinking < is an html tag. Instead of using "<", use "&#60;" and instead of ">", use "&#62;" and see if you can fix it. Because <qualquer_coisa> is a tag. The browser automatically detects the header, it can recognize one as html and the other as plaintext. However, you should not use special characters as < anyway, so switch to the respective entities

  • Thank you, Denied. It worked. I just wanted to know why one code worked and the other.

Browser other questions tagged

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