Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Numerar los registros totales

Estas en el tema de Numerar los registros totales en el foro de Javascript en Foros del Web. Hola por favor tengo este código que me funciona bien pero quisiera añadirle al final de la lista o datos recuperados un mensaje diciendo cuantos ...
  #1 (permalink)  
Antiguo 27/09/2016, 05:37
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Numerar los registros totales

Hola por favor tengo este código que me funciona bien pero quisiera añadirle al final de la lista o datos recuperados un mensaje diciendo cuantos registros se han encontrado.


Código PHP:
Ver original
  1. <?php
  2. include("Connections/wedserver2012.php");
  3. if ($_GET["action"] == "listar")
  4. {
  5.     // valores recibidos por POST
  6.  
  7.     $emis= $_POST['emision'];
  8.  
  9.  
  10.  
  11. if ( ! empty($emis)) {
  12.      
  13. $sql ="(SELECT 1 as qnum,id_aviso,telefonos,telefonos3,movil,dni,name,apellidos,localidad,calle,aparatos,marcas,modelo,codigo,serie,facturacion
  14.  
  15. FROM avisos WHERE emision='$emis' order by id_aviso )";
  16.  
  17.  
  18. }  
  19.  
  20.  
  21.  
  22.     // Ordenar por
  23.     $vorder = $_POST['orderby'];
  24.    
  25.     if($vorder != ''){
  26.         $sql .= " ORDER BY ".$vorder;
  27.     }
  28.      
  29.  
  30.      
  31.     $query = mysql_query($sql);
  32.     if (!$sql) {
  33.     die('Consulta no válida: ' . mysql_error());
  34. }
  35.     if (mysql_num_rows($query)>0 ) {
  36.        
  37.  
  38.          
  39.   $datos = array();
  40.      
  41. while($row = mysql_fetch_array($query)) {
  42.          
  43.  
  44.    
  45.         $datos[] = array(
  46.    
  47.        
  48.              'id_aviso'          => $row['id_aviso'],
  49.             'telefonos'          => $row['telefonos'],
  50.             'name'      => utf8_encode($row['name']),
  51.             'apellidos'       => utf8_encode($row['apellidos']),
  52.             'calle'       => utf8_encode($row['calle']),
  53.             'localidad'        => utf8_encode($row['localidad']),
  54.             'aparatos'        => utf8_encode($row['aparatos']),
  55.             'marcas'        => utf8_encode($row['marcas']),
  56.              'modelo'        => utf8_encode($row['modelo']),
  57.               'codigo'        => utf8_encode($row['codigo']),
  58.                'serie'        => utf8_encode($row['serie']),
  59.             'facturacion'        => utf8_encode($row['facturacion']),
  60.             'style'                 => $style
  61.            
  62.            
  63.            
  64.         );
  65.    
  66.     }
  67.     // convertimos el array de datos a formato json
  68.     echo json_encode($datos);
  69. /* Tienes resultados. */
  70.     }else{
  71. /*No tienes resultados. */
  72.     echo '[]';
  73.         }
  74.   }
  75. ?>





Código PHP:
Ver original
  1. function filtrar(){
  2.  
  3. $.ajax({
  4. data: $("#frm_filtro").serialize()+ordenar,
  5. type: "POST",
  6. dataType: "json",
  7. url: "ajax.php?action=listar",
  8. beforeSend:function(){
  9. $('.loader').css({display:'block'});
  10.   },
  11.  complete: function() {
  12.          $('.loader').css({display:'none'});
  13. },
  14.  success: function(data){
  15.  var html_user ='' ;
  16.  if(data.length > 0){
  17. $.each(data, function(i,item){
  18.  
  19.  
  20.  
  21. html_user += '<tr id="fila1">';
  22. html_user += '<td style="'+item.style+'"><input  onClick="marcar(this)" name="demo" type="checkbox" value="' + item.id_aviso + '"/></td>';
  23. html_user += '<td  style="'+item.style+'">'+item.id_aviso+'</td>';
  24. html_user += '<td  style="'+item.style+'">'+item.telefonos+'</td>';
  25. html_user += '<td  style="'+item.style+'">'+item.name+' '+item.apellidos+'</td>';
  26. html_user += '<td  style="'+item.style+'">'+item.calle+'</td>';
  27. html_user += '<td  style="'+item.style+'">'+item.localidad+'</td>';
  28. html_user += '<td  style="'+item.style+'">'+item.aparatos+'</td>';
  29. html_user += '<td  style="'+item.style+'">'+item.marcas+'</td>';
  30. html_user += '<td  style="'+item.style+'">'+item.modelo+'</td>';
  31. html_user += '<td  style="'+item.style+'">'+item.codigo+'</td>';
  32. html_user += '<td  style="'+item.style+'">'+item.serie+'</td>';
  33. html_user += '<td  style="'+item.style+'">'+item.facturacion+'</td>';
  34. html_user += '</tr>';
  35.  
  36.  
  37. });
  38. }

Última edición por satjaen; 27/09/2016 a las 05:42
  #2 (permalink)  
Antiguo 27/09/2016, 13:20
Avatar de Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: Numerar los registros totales

El total de registros recuperados es el que obtienes en la línea número 16 del código JavaScript:
Código Javascript:
Ver original
  1. data.length

__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand
  #3 (permalink)  
Antiguo 27/09/2016, 15:19
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Numerar los registros totales

Gracias Alexis pero no se como hacerlo. Por favor podrías ayudarme ?
  #4 (permalink)  
Antiguo 27/09/2016, 15:36
Avatar de petit89  
Fecha de Ingreso: marzo-2011
Mensajes: 1.135
Antigüedad: 13 años, 1 mes
Puntos: 170
Respuesta: Numerar los registros totales

añade una linea mas donde imprimas lo que el compañero te mostro:

Código Javascript:
Ver original
  1. html_user += '<td  style="'+item.style+'">'+data.length+'</td>';

imprimiendo data.length obtienes el numero.
__________________
█ WebHosting / Reseller a bajo costo | Uptime Garantizado | Soporte en Español e Ingles
¿Te sirvió la respuesta? Deja un +1 (Triangulo negro al lado derecho)
  #5 (permalink)  
Antiguo 27/09/2016, 15:48
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Numerar los registros totales

Cita:
Iniciado por petit89 Ver Mensaje
añade una linea mas donde imprimas lo que el compañero te mostro:

Código Javascript:
Ver original
  1. html_user += '<td  style="'+item.style+'">'+data.length+'</td>';

imprimiendo data.length obtienes el numero.
Gracias petit89 pero no me lo hace.

Código Javascript:
Ver original
  1. function filtrar()
  2. {
  3.  
  4. $.ajax({
  5. data: $("#frm_filtro").serialize()+ordenar,
  6. type: "POST",
  7. dataType: "json",
  8. url: "ajax.php?action=listar",
  9. beforeSend:function(){
  10. $('.loader').css({display:'block'});
  11. },
  12. complete: function() {
  13.          $('.loader').css({display:'none'});
  14. },
  15. success: function(data){
  16.  var html_user ='' ;
  17.  if(data.length > 0){
  18. $.each(data, function(i,item){
  19.  
  20. html_user += '<tr id="fila1">';
  21. html_user += '<td style="'+item.style+'"><input  onClick="marcar(this)" name="demo" type="checkbox" value="' + item.id_aviso + '"/></td>';
  22. html_user += '<td  style="'+item.style+'">'+item.id_aviso+'</td>';
  23. html_user += '<td  style="'+item.style+'">'+item.telefonos+'</td>';
  24. html_user += '<td  style="'+item.style+'">'+item.name+' '+item.apellidos+'</td>';
  25. html_user += '<td  style="'+item.style+'">'+item.calle+'</td>';
  26. html_user += '<td  style="'+item.style+'">'+item.localidad+'</td>';
  27. html_user += '<td  style="'+item.style+'">'+item.aparatos+'</td>';
  28. html_user += '<td  style="'+item.style+'">'+item.marcas+'</td>';
  29. html_user += '<td  style="'+item.style+'">'+item.modelo+'</td>';
  30. html_user += '<td  style="'+item.style+'">'+item.codigo+'</td>';
  31. html_user += '<td  style="'+item.style+'">'+item.serie+'</td>';
  32. html_user += '<td  style="'+item.style+'">'+item.facturacion+'</td>';
  33. html_user += '<td  style="'+item.style+'">'+data.length+'</td>';
  34. html_user += '</tr>';
  35.  
  36.  
  37.  
  38. });
  39. }
  40. if(html_user == '')
  41. html_user = '<tr><td colspan="4" align="center">No se encontraron registros..</td></tr>';
  42. $("#data tbody").html(html_user);
  43. $('#carga').html('');
  #6 (permalink)  
Antiguo 28/09/2016, 01:15
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Numerar los registros totales

Perdón si que sale ya !!!!!!!
Pero obtengo el número total de registros en cada fila y lo que quisiera es que saliera el mensaje al final de todas las filas.

Ahora mismo me sale así:

Aviso -Nombre - Ciudad- Telefono - Registro
1 - Juan - Melilla - 9582233 - 2
2 - Manolo - Sevilla - 63599 - 2


Y quisiera que saliera así al final del todo:

Aviso -Nombre - Ciudad- Telefono
1 - Juan - Melilla - 9582233
2 - Manolo - Sevilla - 63599


- Se han encontrado 2 registros.

Última edición por satjaen; 28/09/2016 a las 02:08
  #7 (permalink)  
Antiguo 28/09/2016, 07:41
Avatar de AngelKrak  
Fecha de Ingreso: noviembre-2014
Mensajes: 917
Antigüedad: 9 años, 5 meses
Puntos: 91
Respuesta: Numerar los registros totales

prueba con esto(ponlo al ultimo, antes de finalizar la funcion):

Código Javascript:
Ver original
  1. var count = $("#fila1").length;
  2. $("body").append(count+" Resultados");

Demo:
http://codepen.io/anon/pen/wzZmKy
  #8 (permalink)  
Antiguo 28/09/2016, 08:27
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Numerar los registros totales

Cita:
Iniciado por AngelKrak Ver Mensaje
prueba con esto(ponlo al ultimo, antes de finalizar la funcion):

Código Javascript:
Ver original
  1. var count = $("#fila1").length;
  2. $("body").append(count+" Resultados");

Demo:
http://codepen.io/anon/pen/wzZmKy

Gracias pero era así:
Código Javascript:
Ver original
  1. var count = data.length;

Etiquetas: funcion, js, php, registros, select, totales, valor
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:56.