The same variable repeats several times in the while

Asked

Viewed 385 times

1

In the while words are appearing several times, example:

macarrão
macarrão
macarrão
macarrão
macarrão
macarrão
arroz
arroz
arroz
arroz
arroz
arroz

What am I doing wrong?

<?php
$usuario=$_SESSION['email'];

require_once 'Classes/ProjetosVO.php';
require_once 'Classes/ProjetosDAO.php';

$mysqli = new mysqli('localhost', 'root', '', 'bdpi');

$objBDProjeto=new ProjetosDAO();
$objProjeto= new ProjetosVO();

$rsProjeto= $objBDProjeto->ListarProjetos("1");

$sqlProjetos="select P.codigo_PROJETO, P.nome_PROJETO from projetos P, usuarios U, funcoes F where U.email_USUARIO like '$usuario' and U.email_USUARIO=F.emailUsuario_FUNCAO and U.email_USUARIO=P.responsavel_PROJETO and F.status_FUNCAO=1 ";

$rsProjeto= mysqli_query($mysqli, $sqlProjetos) or die (mysqli_error($mysqli));

$tblProjeto=mysqli_fetch_array($rsProjeto);


?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="https://use.fontawesome.com/eb2dfeaa2c.js"></script>
<br>
<br>
<center><h1 class="Titulo">Meus Projetos</h1></center>

&nbsp;&nbsp;
<?php
    include 'FormCadastraProjeto.php';
?>
<?php
    while($tblProjeto==mysqli_fetch_array($rsProjeto)){

?>
<a href="Projeto.php?codProjeto=<?=$tblProjeto['codigo_PROJETO'];?>" >      <h1><i class="fa fa-folder" aria-hidden="true"></i></h1>

<?php
    echo $tblProjeto['nome_PROJETO'];
?>
</a>

<?php
  }

?>
  • 2 times? $mysqli = new mysqli('localhost', 'root', '', 'bdpi');

3 answers

1

I’m not sure about your table structure, but you can solve this by putting a distinct after the select:

$sqlProjetos="select DISTINCT P.codigo_PROJETO, P.nome_PROJETO from projetos P, usuarios U, funcoes F..."

See if it works like this.

0

What are you comparing in this while ?. Depending could use

foreach($tblProjeto as $tp){
?>
<a href="Projeto.php?codProjeto=<?=$tp['codigo_PROJETO'];?>" >      <h1><i class="fa fa-folder" aria-hidden="true"></i></h1>

0

Your mysqli_fetch_array is repeated and misplaced in while. See comment on your code line below:

...$rsProjeto= mysqli_query($mysqli, $sqlProjetos) or die (mysqli_error($mysqli));

$tblProjeto=mysqli_fetch_array($rsProjeto);  // <<<--- Remova essa linha que está repetida com o while.

?>...

Then arrange your while which is in the form of comparison with two equal signs (==) as follows:

<?php
    while($tblProjeto==mysqli_fetch_array($rsProjeto)){

?>

To:

<?php
    while($tblProjeto = mysqli_fetch_array($rsProjeto, MYSQLI_ASSOC)){

?>

It is not yet known why the repetition, but it can be because of these two lines, for besides repeating the two lines, and make a comparison mode in while, if you do not put the return mode of the array php will return all modes. Which can cause repetition. It’s a little tricky to explain. But you can do a test later with var_dump in your mysqli_fetch_array, and see what it returns.

But at last, try to make these changes, and tell me if it helped. If you continue with the repetition or some error, let me know.

Browser other questions tagged

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