Puzzle with array and modal

Asked

Viewed 96 times

0

So I have a page that shows a total of 8 Divs per pagination, these Divs have little information inside, they have 2 button that allows you to delete and have more information about what is inside the div than in the case and the data of a phone number, the problem and the following, of those 8 Divs I can see the first 2 normally: I say see the information of the number, but in the other 6 appears the same information that is inside the second div... I think the problem and in the if more can not solve, follows the code:

<?php 
    include_once("../conn/conexao.php");//faz a conexao com o banco de dados
     
    //verifica a página atual caso seja informada na URL, senão atribui como 1ª página 
    $pagina = (isset($_GET['pagina']))? $_GET['pagina'] : 1; 
 
    //seleciona todos os itens da tabela 
    $cmd = "select * from tb_numeros"; 
    $produtos = mysqli_query($conexao, $cmd); 
 
    //conta o total de itens 
    $total = mysqli_num_rows($produtos); 
 
    //seta a quantidade de itens por página, neste caso, 2 itens 
    $registros = 8; 
 
    //calcula o número de páginas arredondando o resultado para cima 
    $numPaginas = ceil($total/$registros); 
 
    //variavel para calcular o início da visualização com base na página atual 
    $inicio = ($registros*$pagina)-$registros; 
 
    //seleciona os itens por página 
    $cmd = "select * from tb_numeros limit $inicio,$registros"; 
    $produtos = mysqli_query($conexao, $cmd); 
    $total = mysqli_num_rows($produtos); 
     
    //exibe os produtos selecionados 
    
        echo "<div class='container'>
				<div class='row pricing text-center'>
				<div class='col-md-3'>
				<div class='well'>

				";
				$cont=0;
					while ($produto = mysqli_fetch_array($produtos)){
						if (($cont%8)==0):
    	 		
                           
                 echo "<h3><b>".$produto['numero']."</b></h3>
				 		<hr>
						<p>Chip:&nbsp;".$produto['nchip']."</p>
						<hr>
						<p>IMEI:&nbsp;".$produto['imei']."</p>
						<hr>
						<div class='col-xs-6'>
						<a href='#' class='btn btn-danger btn-block'><span class='glyphicon glyphicon-trash'></span></a></div>
						<div class='col-xs-6'><a href='#info' data-toggle='modal' class='btn btn-success btn-block'><span class='glyphicon glyphicon-plus'></span>&nbsp;Info</a></div>
						
					 
    <div class='modal fade' id='info' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
        <div class='modal-dialog'>
            <div class='modal-content'>
              <div class='modal-header'>
					<button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>
					<h3 class='modal-title' id='lineModalLabel' align='center'>Dados do Telefone</h3>
					
				<div class='modal-body'>
					<div class='form-group'>
						<div class='col-xs-12'><center>
						<label for='exampleInputPassword1'>Funcionário</label></center>
						<input type='text' name='funcionario' class='form-control' value=".$produto['funcionario']." style='text-align: center;' readonly='readonly' id='letras' required>
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-4'><center>
						<label for='exampleInputPassword1'>Número</label></center>
						<input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$produto['numero']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
				   
					<div class='form-group'>
						<div class='col-xs-2'><center>
						<label for='exampleInputPassword1'>RCA</label></center>
						<input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$produto['rca']." style='text-align: center;' maxlength='4' readonly='readonly' onkeypress='return SomenteNumero(event)' required>
						</div>
					</div>
				   
				   <div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>Região</label></center>
						<input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$produto['regiao']." style='text-align: center;' readonly='readonly' required>
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>Número Chip</label></center>
						<input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$produto['nchip']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>IMEI</label></center>
						<input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$produto['imei']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-12'><center>
						<label for='exampleInputPassword1'>Cadastrado Por</label></center>
						<input type='text' name='usuario' class='form-control' id='exampleInputPassword1' value=".$produto['usuario']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
				
				</div>
				</div>
            </div>
        </div>
    </div>

				 </div>
				 </div>
				";	
											
					else :
					echo"<div class='col-md-3'>
						<div class='well'>	
						<h3><b>".$produto['numero']."</b></h3>
				 		<hr>
						<p>Chip:&nbsp;".$produto['nchip']."</p>
						<hr>
						<p>IMEI:&nbsp;".$produto['imei']."</p>
						<hr>
						<div class='col-xs-6'>
						<a href='#' class='btn btn-danger btn-block'><span class='	glyphicon glyphicon-trash'></span></a></div>
						<div class='col-xs-6'><a href='#info2' data-toggle='modal' class='btn btn-success btn-block'><span class='glyphicon glyphicon-plus'></span>&nbsp;Info</a></div>
						
						<div class='modal fade' id='info2' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
        <div class='modal-dialog'>
            <div class='modal-content'>
              <div class='modal-header'>
					<button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>
					<h3 class='modal-title' id='lineModalLabel' align='center'>Dados do Telefone</h3>
					
				<div class='modal-body'>
					<div class='form-group'>
						<div class='col-xs-12'><center>
						<label for='exampleInputPassword1'>Funcionário</label></center>
						<input type='text' name='funcionario' class='form-control' value=".$produto['funcionario']." style='text-align: center;' readonly='readonly' id='letras' required>
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-4'><center>
						<label for='exampleInputPassword1'>Número</label></center>
						<input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$produto['numero']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
				   
					<div class='form-group'>
						<div class='col-xs-2'><center>
						<label for='exampleInputPassword1'>RCA</label></center>
						<input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$produto['rca']." style='text-align: center;' maxlength='4' readonly='readonly' onkeypress='return SomenteNumero(event)' required>
						</div>
					</div>
				   
				   <div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>Região</label></center>
						<input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$produto['regiao']." style='text-align: center;' readonly='readonly' required>
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>Número Chip</label></center>
						<input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$produto['nchip']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>IMEI</label></center>
						<input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$produto['imei']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-12'><center>
						<label for='exampleInputPassword1'>Cadastrado Por</label></center>
						<input type='text' name='usuario' class='form-control' id='exampleInputPassword1' value=".$produto['usuario']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
				
				</div>
				</div>
            </div>
        </div>
    </div>
				 </div>
				 </div>
				 ";
					
					
						endif;
				$cont++;
					}for($i = 1; $i < $numPaginas + 1; $i++) { 
					echo "<div class='row'>
              <div class='text-center'><ul class='pagination pagination-large'>
		<li><a href='visualizarn.php?pagina=$i'>".$i."</a></li></ul></div></div> "; 
				} 
	echo"
		</div>
	";
		
         
?>

  • it will be easier to insert the html press F12 and search should have some div putting hierarchy on the other

  • -I don’t understand...

1 answer

1


It turns out that the tag that opens the modal from the third iteration of while is always <a href='#info2'... and that is to say, show the div whose id is equal to info2.

The solution is to increase the number after the term info, that is to say, <a href='#info".$cont."'. I took advantage of the variable $cont that already exists in your code and that at each iteration it is already incremented.

Note that the <div class='modal fade' id='info2' must have the id value corresponding to the tag that calls it, so you must also concatenate the value of $cont at each iteration <div class='modal fade' id='info".$cont."'.

I took advantage of your HTML, ( thus I think :D ), which contained more opening tags div that of closure </div> and pulled out the if-if that’s unnecessary.

//exibe os produtos selecionados 

    echo "<div class='container'>
            <div class='row pricing text-center'>

            ";
            $cont=0;
                while ($produto = mysqli_fetch_array($produtos)){

                    echo"<div class='col-md-3'>
                    <div class='well'>";


             echo "<h3><b>".$produto['numero']."</b></h3>
                    <hr>
                    <p>Chip:&nbsp;".$produto['nchip']."</p>
                    <hr>
                    <p>IMEI:&nbsp;".$produto['imei']."</p>
                    <hr>
                    <div class='col-xs-6'>
                    <a href='#' class='btn btn-danger btn-block'><span class='glyphicon glyphicon-trash'></span></a></div>
                    <div class='col-xs-6'><a href='#info".$cont."' data-toggle='modal' class='btn btn-success btn-block'><span class='glyphicon glyphicon-plus'></span>&nbsp;Info</a></div>


<div class='modal fade' id='info".$cont."' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
    <div class='modal-dialog'>
        <div class='modal-content'>
          <div class='modal-header'>
                <button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>
                <h3 class='modal-title' id='lineModalLabel' align='center'>Dados do Telefone</h3>

            <div class='modal-body'>
                <div class='form-group'>
                    <div class='col-xs-12'><center>
                    <label for='exampleInputPassword1'>Funcionário</label></center>
                    <input type='text' name='funcionario' class='form-control' value=".$produto['funcionario']." style='text-align: center;' readonly='readonly' id='letras' required>
                    <br></div>
                </div>

                <div class='form-group'>
                    <div class='col-xs-4'><center>
                    <label for='exampleInputPassword1'>Número</label></center>
                    <input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$produto['numero']." style='text-align: center;' readonly='readonly' >
                    </div>
                </div>

                <div class='form-group'>
                    <div class='col-xs-2'><center>
                    <label for='exampleInputPassword1'>RCA</label></center>
                    <input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$produto['rca']." style='text-align: center;' maxlength='4' readonly='readonly' onkeypress='return SomenteNumero(event)' required>
                    </div>
                </div>

               <div class='form-group'>
                    <div class='col-xs-6'><center>
                    <label for='exampleInputPassword1'>Região</label></center>
                    <input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$produto['regiao']." style='text-align: center;' readonly='readonly' required>
                    <br></div>
                </div>

                <div class='form-group'>
                    <div class='col-xs-6'><center>
                    <label for='exampleInputPassword1'>Número Chip</label></center>
                    <input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$produto['nchip']." style='text-align: center;' readonly='readonly' >
                    </div>
                </div>

                <div class='form-group'>
                    <div class='col-xs-6'><center>
                    <label for='exampleInputPassword1'>IMEI</label></center>
                    <input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$produto['imei']." style='text-align: center;' readonly='readonly' >
                    <br></div>
                </div>

                <div class='form-group'>
                    <div class='col-xs-12'><center>
                    <label for='exampleInputPassword1'>Cadastrado Por</label></center>
                    <input type='text' name='usuario' class='form-control' id='exampleInputPassword1' value=".$produto['usuario']." style='text-align: center;' readonly='readonly' >
                    <br></div>
                </div>


            </div>
            </div>
        </div>
    </div>
</div>
             </div>
             </div>
             ";


            $cont++;
                }

            for($i = 1; $i < $numPaginas + 1; $i++) { 
                echo "<div class='row'>
          <div class='text-center'><ul class='pagination pagination-large'>
    <li><a href='visualizarn.php?pagina=$i'>".$i."</a></li></ul></div></div> "; 
            } 
echo"
    </div></div>
";

Browser other questions tagged

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