Foros del Web » Programando para Internet » PHP »

Total de produtos en cada categoria !

Estas en el tema de Total de produtos en cada categoria ! en el foro de PHP en Foros del Web. Muy buenas, a ver si alguien me puede dar unas luces de como puedo hacer un total de produtos en cada categoria. Ex: Categoria (23) ...
  #1 (permalink)  
Antiguo 11/04/2009, 15:23
Avatar de b0zZy  
Fecha de Ingreso: enero-2009
Ubicación: Francia
Mensajes: 28
Antigüedad: 15 años, 3 meses
Puntos: 0
Mensaje SOLUCIONADO !! Total de produtos en cada categoria !

Muy buenas, a ver si alguien me puede dar unas luces de como puedo hacer un total de produtos en cada categoria. Ex:
Categoria (23)
Categoria1 (342)
Categoria2 (0)
Categoria3 (56)
...
He intentado esto,
Código PHP:
$sql="SELECT c.cat_id, COUNT( cp.cat_id ) AS total
FROM tbl_product AS c
LEFT JOIN tbl_category AS cp ON cp.cat_id = c.cat_id
GROUP BY c.cat_id"
;
$results mysql_query($sql) or die(mysql_error());
print (
"$results"); 
Y en PhpMyAdmin me salen bien los resultados, pero a la hora de aplicarlo, solo me sale Resource id #12 y no llego a conseguirlo. Quizas porque mis conocimientos en PHP son basicos .

Gracias

Última edición por b0zZy; 12/04/2009 a las 16:38 Razón: Solucionado !
  #2 (permalink)  
Antiguo 11/04/2009, 16:45
Avatar de pacmanaman  
Fecha de Ingreso: marzo-2009
Mensajes: 84
Antigüedad: 15 años, 1 mes
Puntos: 3
Respuesta: Total de produtos en cada categoria !

while($fila = mysql_fecth_array($results))
{
echo $fila['cat_id']."-". $fila['Total'];
}

SaluDOS!
__________________
(<++
  #3 (permalink)  
Antiguo 11/04/2009, 19:27
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Total de produtos en cada categoria !

Cita:
$results = mysql_query($sql) or die(mysql_error());
print ("$results");
No puede imprimir un resource, tienes que obtener los datos usando mysql_fetch_array() cmo te indico pacmanaman
  #4 (permalink)  
Antiguo 12/04/2009, 04:09
Avatar de b0zZy  
Fecha de Ingreso: enero-2009
Ubicación: Francia
Mensajes: 28
Antigüedad: 15 años, 3 meses
Puntos: 0
Respuesta: Total de produtos en cada categoria !

Cita:
Iniciado por pacmanaman Ver Mensaje
while($fila = mysql_fecth_array($results))
{
echo $fila['cat_id']."-". $fila['Total'];
}

SaluDOS!
Hola pacmanaman, gracias por contestar.
Lo he intentado asi:
Código PHP:
$sql="SELECT c.cat_id, COUNT( cp.cat_id ) AS total
FROM tbl_product AS c
LEFT JOIN tbl_category AS cp ON cp.cat_id = c.cat_id
GROUP BY c.cat_id"
;
while(
$fila mysql_fecth_array($results))
{
echo 
$fila['cat_id']."-"$fila['Total'];

pero me da -> Fatal error: Call to undefined function mysql_fecth_array() in C:\wamp\www\include\categoryList.php on line 27
Aqui dejo todo el categorylist, puede que el error venga de atras
Código PHP:
<?php
if (!defined('WEB_ROOT')) {
    exit;
}

$categoryList    getCategoryList();
$categoriesPerRow 6;
$numCategory     count($categoryList);
$columnWidth    = (int)(100 $categoriesPerRow);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="20">
<?php 
if ($numCategory 0) {
    
$i 0;
    for (
$i$i $numCategory$i++) {
        if (
$i $categoriesPerRow == 0) {
            echo 
'<tr>';
        }
        
        
// Tenemos la $url, $image, $name, $price
        
extract ($categoryList[$i]);
        
    
$sql="SELECT c.cat_id, COUNT( cp.cat_id ) AS total
FROM tbl_product AS c
LEFT JOIN tbl_category AS cp ON cp.cat_id = c.cat_id
GROUP BY c.cat_id"
;
while(
$fila mysql_fecth_array($results))
{
echo 
$fila['cat_id']."-"$fila['Total'];
}


    echo 
"<td width=\"$columnWidth%\" align=\"center\"style=vertical-align:baseline><a href=\"$url\"><img src=\"$image\" border=\"0\"><br>$name<br></a>()</td>\r\n";    
    
        if (
$i $categoriesPerRow == $categoriesPerRow 1) {
            echo 
'</tr>';
        }
        
    }
    
    if (
$i $categoriesPerRow 0) {
        echo 
'<td colspan="' . ($categoriesPerRow - ($i $categoriesPerRow)) . '">&nbsp;</td>';
    }
} else {
?>
    <tr><td width="100%" align="center" valign="center">Aucune</td></tr>
<?php    
}    
?>
</table>
  #5 (permalink)  
Antiguo 12/04/2009, 09:47
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Total de produtos en cada categoria !

mysql_fecth_array($results)) no es una funcion -_- esta mal escrita:

mysql_fetch_array($results))

Una busqueda en el manual te hubiera dado la solucion.
  #6 (permalink)  
Antiguo 12/04/2009, 10:26
Avatar de pacmanaman  
Fecha de Ingreso: marzo-2009
Mensajes: 84
Antigüedad: 15 años, 1 mes
Puntos: 3
Respuesta: Total de produtos en cada categoria !

La escribi mal, la copio de la mia!

$results = mysql_query($sql) or die(mysql_error()); <<< TE FALTA ESTA LINEA!
while($fila = mysql_fetch_array($results))
{
echo $fila['cat_id']."-". $fila['Total'];
}
__________________
(<++
  #7 (permalink)  
Antiguo 12/04/2009, 11:30
Avatar de b0zZy  
Fecha de Ingreso: enero-2009
Ubicación: Francia
Mensajes: 28
Antigüedad: 15 años, 3 meses
Puntos: 0
Respuesta: Total de produtos en cada categoria !

Cita:
Iniciado por Ronruby Ver Mensaje
mysql_fecth_array($results)) no es una funcion -_- esta mal escrita:

mysql_fetch_array($results))

Una busqueda en el manual te hubiera dado la solucion.
Hola Ronruby,
Tienes razon, esta es la funcion
Código PHP:
function getCategoryList()
{
    
$sql "SELECT cat_id, cat_name, cat_image
            FROM tbl_category
            WHERE cat_parent_id = 0
            ORDER BY cat_name"
;
    
$result dbQuery($sql);
    
    
$cat = array();
    while (
$row dbFetchAssoc($result)) {
        
extract($row);
        
        if (
$cat_image) {
            
$cat_image WEB_ROOT 'images/category/' $cat_image;
        } else {
            
$cat_image WEB_ROOT 'images/no-image-small.png';
        }
        
        
$cat[] = array('url'   => $_SERVER['PHP_SELF'] . '?c=' $cat_id,
                       
'image' => $cat_image,
                       
'name'  => $cat_name);

    }
    
    return 
$cat;            

Pero no llego a adaptarlo !! Me tiene harto. Me puedes ayudar, porfavor !
Gracias a ambos, Ronruby y pacmanaman.
  #8 (permalink)  
Antiguo 12/04/2009, 16:36
Avatar de b0zZy  
Fecha de Ingreso: enero-2009
Ubicación: Francia
Mensajes: 28
Antigüedad: 15 años, 3 meses
Puntos: 0
(SOLUCIONADO)Respuesta: Total de produtos en cada categoria !

Ya lo he solucionado !! Aqui lo dejo por si le sirve a alguien !
Código PHP:
function getCategoryList()
{
    
$sql "SELECT p.cat_id, p.cat_id2, p.cat_id3, cat_name, cat_image, COUNT(p.cat_id) AS TOTAL
            FROM tbl_category c, tbl_product p
            WHERE c.cat_id = p.cat_id
            GROUP BY cat_name"
;
    
$result dbQuery($sql);
    
    
$cat = array();
    while (
$row dbFetchAssoc($result)) {
        
extract($row);
        
        if (
$cat_image) {
            
$cat_image WEB_ROOT 'images/category/' $cat_image;
        } else {
            
$cat_image WEB_ROOT 'images/no-image-small.png';
        }
        
        
$cat[] = array('url'   => $_SERVER['PHP_SELF'] . '?c=' $cat_id,
                       
'image' => $cat_image,
                       
'name'  => $cat_name,
                       
'id'    => $TOTAL);

    }
    
    return 
$cat;            

Gracias a ambos de todas formas
  #9 (permalink)  
Antiguo 12/04/2009, 20:52
Avatar de pacmanaman  
Fecha de Ingreso: marzo-2009
Mensajes: 84
Antigüedad: 15 años, 1 mes
Puntos: 3
Respuesta: Total de produtos en cada categoria !

No es nada! SaluDOS!
__________________
(<++
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 04:04.