How to assign counter as part of the class name, which already have the Divs (Using php, Jquery)?

0

My problem is that I want to create several elements according to the amount that the While syllabus finds and assign the counter concatenated with the name of the CLASS that already has (THAT IS: ELEMENT); also assign the counter as an identifier; AS YOU CAN SEE IN THE OTHER CODE THAT WILL SEND THEM, I WILL GIVE YOU THE NAME TO THE DOCUMENT MADE IN HTML

This in the Code I Want to Resolve Using Jquery Php and Mysql

		#campoBusq {
		border: 1px solid blue ;
		width:45%;
		float:left;
		padding:5px;
		height:300px;

		}

		#categoria {
		border: 1px solid red;
		width:45%;
		padding:5px;
		float:left;
		height:300px;
		}

		.elemento {
		border: 1px solid green;
		width:90%;
		margin:1%;
		padding:1%;
		float:left;
		clear:left;
		}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="campoBusq"></div>
	<div id="categoria">

		<?php 
			include('../configuracion/conexion.php'); 
		?>

		<script>
			<?php 
				$consulta = "SELECT idCategoria FROM buscar_categoria";
				$resultado = $conexion->query($consulta) or die("Error de busqueda o conexion");

				 while ($datosCateorias=$resultado->fetch_assoc() ) {
			?>  
					var datosCateorias = '<?php echo $datosCateorias['idCategoria'] ?>';
			    	$('#categoria').append('<option class="elemento '+contador+'" ident="'+contador+'" >' + datosCateorias + '</option>');

			<?php  
			       }

			    mysqli_free_result($resultado);
			    $conexion->close();
			?>



		</script>
	</div>
</div>

The script gives me this message WHERE THE VALUES ARE NOT PRINTED

    
asked by Gamez 10.03.2017 в 21:59
source

1 answer

0

Try it like this. It is not necessary to use javascript or jquery in that part, because the only thing you are doing is creating html content and that can be done with php very simply. I created the counter variable because I did not see it anywhere because of your code.

<div id="campoBusq"></div>
    <div id="categoria">

        <?php 
            include('../configuracion/conexion.php'); 

            $consulta = "SELECT idCategoria FROM buscar_categoria";
            $resultado = $conexion->query($consulta) or die("Error de busqueda o conexion");
            contador = 0;
                while ($datosCateorias=$resultado->fetch_assoc() ) {
                    contador++;
                    echo "<option class='elemento ".contador."' iden='".contador."'>". $datosCateorias['idCategoria']."</option>";
                }
                  mysqli_free_result($resultado);
                $conexion->close();
        ?>  
    </div>
</div>

Your complete code will remain that way (all with their corresponding open and closed labels, put them on):

jQuery(document).ready(function() {

            $('#categoria').on('click','.elemento',function() {
               var e = $(this).clone();
               var identificador = $(this).attr("iden");

               if($("#campoBusq").find("."+identificador).length){

               }else{
                 $(e).appendTo('#campoBusq');
               } 
           });

            $('#campoBusq').on('click','.elemento',function() {
               $(this).remove();
            });

        });

        #campoBusq {
        border: 1px solid blue ;
        width:45%;
        float:left;
        padding:5px;
        height:300px;

        }

        #categoria {
        border: 1px solid red;
        width:45%;
        padding:5px;
        float:left;
        height:300px;
        }

        .elemento {
        border: 1px solid green;
        width:90%;
        margin:1%;
        padding:1%;
        float:left;
        clear:left;
        }




  <div id="campoBusq"></div>
    <div id="categoria">

        <?php 
            include('../configuracion/conexion.php'); 

            $consulta = "SELECT idCategoria FROM buscar_categoria";
            $resultado = $conexion->query($consulta) or die("Error de busqueda o conexion");
            contador = 0;
                while ($datosCateorias=$resultado->fetch_assoc() ) {
                    contador++;
                    echo "<option class='elemento ".contador."' iden='".contador."'>". $datosCateorias['idCategoria']."</option>";
                }
                  mysqli_free_result($resultado);
                $conexion->close();
        ?>  
    </div>
</div>
    
answered by 10.03.2017 в 22:21