1

hola espero me puedan ayudar. tengo una tabla con muchos registros en donde necesito listar los datos del campo usuarios. este campo tiene muchos nombres repetidos ya que las mismas personas registran datos.

necesito listar algo como:

pedro = 2 veces 
juan  = 8 veces
diego = 16 veces 

espero se entienda y me puedan ayudar quedo atento a sus comentarios

si me pueden ayudar con el código de la consulta por favor, no manejo mucho esto y es para un trabajo del instituto

tengo este codico que me lista los registros guardados, quiero generar otra consulta que me cuente a los funcionarios y me diga cuantos ahi de cada uno

  <div class="table-responsive">
   <table class="table table-bordered" style="font-size:12px;">
    <thead>
      <tr>
     
     <th style="width:5%;">Fecha</th>
     <th style="width:5%;">Run</th>
     <th style="width:5%;">Alumno</th>
     <th style="width:5%;">Curso</th>
     <th style="width:5%;">Nombre Retira</th>
     <th style="width:5%;">Run Retira</th>
     <th style="width:5%;">Funcionario</th>
   </tr>
   </thead>
   <tbody id="myTable">
    <tr>
        
        <?php
    
        $consulta = "SELECT * FROM entregados ORDER BY fecha DESC";
        $resultado = mysql_query($consulta,$link);
        while($fila = mysql_fetch_array($resultado)){
        ?>  
            <tr>
                
                <td><?php echo $fila['fecha']; ?></td>
                
                <td><?php 
                                        $rut = $fila['runalu'];
                                        echo $rut; ?></td>
                                        
                
                <td><?php echo $fila['alumno']; ?></td>
                <td><?php echo $fila['curso']; ?></td>
                <td><?php echo $fila['nombre_retira']; ?></td>
                <td><?php echo $fila['run_retira']; ?></td>
                <td><?php echo $fila['funcionario']; ?></td>
                
            </tr>
                
                
    <?php
    }       
    ?>      
      
    </tr>
  </tbody>
  </table>
 </div>
5
  • 1
    ¿qué has intentado?, edita y muéstranos que llevas
    – user128299
    el 29 jul. 2020 a las 22:44
  • Puedes hacer un count del id o el nombre en cuestion y un GROUP BY. Por ej: SELECT COUNT(id), nombre FROM tabla GROUP BY nombre
    – Andres_Ha
    el 29 jul. 2020 a las 22:45
  • ¿Y qué valor se supone que estás tratando de contar para cada uno de los usuarios?, además el registro deseado esta en la misma tabla de los usuarios?
    – user128299
    el 29 jul. 2020 a las 22:53
  • QUIERO UNA CONSULTA QUE ME CUENTE A LOS FUNCIONARIOS Y ME DIGA QUE NOMBRE FUNCIONARIO ESTA REPETIDO = X VECES CONSIDERANDO QUE AHÍ MUCHOS FUNCIONARIOS Y VARIOS SE REPITEN
    – JAGR
    el 29 jul. 2020 a las 22:57
  • LO NECESITO EN PHP AMIGO
    – JAGR
    el 30 jul. 2020 a las 0:20

1 respuesta 1

1

Hola aca va un ejemplo del lado PHP. El código esta testeado parcialmente ya que no dispongo de acceso a la base de datos en concreto:

<div class="table-responsive">
    <table class="table table-bordered" style="font-size:12px;">
        <thead>
            <tr>

                <th style="width:5%;">Fecha</th>
                <th style="width:5%;">Run</th>
                <th style="width:5%;">Alumno</th>
                <th style="width:5%;">Curso</th>
                <th style="width:5%;">Nombre Retira</th>
                <th style="width:5%;">Run Retira</th>
                <th style="width:5%;">Funcionario</th>
            </tr>
        </thead>
        <tbody id="myTable">


            <?php
            $consulta = "SELECT * FROM entregados ORDER BY fecha DESC";
            $resultado = mysql_query($consulta, $link);
            //EN ESTE ARRAY ASOCIATIVO SE GUARDA COMO CLAVE EL NOMBRE Y EL VALOR LA CANTIDAD DE REGISTROS ASOCIADOS.
            $data = array();
            while ($fila = mysql_fetch_array($resultado)) {
                ?>  
                <tr>

                    <td><?php echo $fila['fecha']; ?></td>

                    <td><?php
                        $rut = $fila['runalu'];
                        echo $rut;
                        ?></td>


                    <td><?php echo $fila['alumno']; ?></td>
                    <td><?php echo $fila['curso']; ?></td>
                    <td><?php echo $fila['nombre_retira']; ?></td>
                    <td><?php echo $fila['run_retira']; ?></td>
                    <td><?php echo $fila['funcionario']; ?></td>

                </tr>


                <?php
                //ACA VERIFICAMOS QUE LA CLAVE EXISTA. SI EXISTE LE SUMAMOS 1 Y SI NO EXISTE ASIGNAMOS LA CLAVE CON VALOR 1 (EL PRIMER REGISTRO)
                if (isset($data[alumno])) {
                    $data[alumno] += 1;
                } else {
                    $data[alumno] = 1;
                }
            }
            ?>      

        </tbody>
    </table>
    <table>
        <thead>
            <tr>
                <th>Nombre</th>
                <th>Cantidad</th>
            </tr>
        </thead>
        <tbody>
            <?php
            // ACA HACEMOS UN LOOP AL ARRAY ASOCIATIVO PARA MOSTRAR NOMBRE Y CANTIDADES.
            foreach ($data as $clave => $valor) {
                echo '<tr>';
                echo '<td>' . $clave . "</td>";
                echo '<td>' . $valor . "</td>";
                echo '</tr>';
            }
            ?>
        </tbody>
    </table>

</div>
2
  • Una respuesta que solo es código se considera de baja calidad, editar y explicar que programaste se agradecería mucho
    – user128299
    el 30 jul. 2020 a las 0:43
  • Hola Beta, el código es 95% del usuario. Solo agregue una lineas para el total abajo. el cual voy a comentar.
    – toto
    el 30 jul. 2020 a las 0:47

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.