Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá.. estou me matando aki no PHP tem uns dias já..
Acredito q seja simples, mais não estou conseguindo achar uma solução..
Quero quantos itens tem nas categorias cadastradas em cada status..
Já tentei GROUP BY
COUNT
DISTINCT
E tudo trás os mesmos resultados
MEU COD.
<?php
include "conecta.php";
?>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' )
{
$where = Array();
$id = getPost('f');
$data = getPost('d');
$categoria = getPost('n');
$status = getPost('s');
$fabricante = getPost('c');
$id_cliente = getPost('b');
$defeito = getPost('a');
if( $id ){ $where[] = " `id` = '{$id}'"; }
if( $data ){ $where[] = " `data` = '{$data}'"; }
if( $categoria ){ $where[] = " `categoria` = '{$categoria}'"; }
if( $status ){ $where[] = " `status` = '{$status}'"; }
if( $fabricante ){ $where[] = " `fabricante` = '{$fabricante}'"; }
if( $id_cliente ){ $where[] = " `id_cliente` = '{$id_cliente}'"; }
if( $defeito ){ $where[] = " `defeito` = '{$defeito}'"; }
$sql = "SELECT id, cod, data, categoria, status, fabricante, id_cliente, defeito FROM cad_ped";
if( sizeof( $where ) )
$sql .= ' WHERE '.implode( ' AND ', $where);
}
//a cargo do leitor melhorar o filtro anti injection
function filter( $str ){
return addslashes( $str );
}
function getPost( $key ){
return isset( $_POST[ $key ] ) ? filter( $_POST[ $key ] ) : null;
}
?>
<style type="text/css">
label { display: block; }
body,td,th {
color: #666;
}
a {
font-family: Verdana, Geneva, sans-serif;
font-size: 10px;
color: #666;
}
a:visited {
color: #333;
}
a:hover {
color: #333;
}
a:active {
color: #666;
}
</style>
<body text="#666666" link="#666666" vlink="#333333" alink="#333333"> <form action="" method="post">
<table width="30%" border="0">
<tr>
<td width="32%">Data:</td>
<td width="68%"><input type="text" name="d" /></td>
</tr>
<tr>
<td>categoria: </td>
<td><input type="text" name="n" /></td>
</tr>
<tr>
<td>status:</td>
<td><select name="s" size="1" id="status">
<option></option>
<option>Aguardando Reparo</option>
<option>Em processo de Reparo</option>
<option>Aguardando liberação para entrega</option>
</select></td>
</tr>
<tr>
<td>fabricante:</td>
<td><input type="text" name="c" /></td>
</tr>
<tr>
<td>id_cliente:</td>
<td><input type="text" name="b" /></td>
</tr>
<tr>
<td>defeito: </td>
<td><input type="text" name="a" /></td>
</tr>
<tr>
<td height="21"> </td>
<td><table width="100%" border="0">
<tr>
<td width="58%"> </td>
<td width="42%"><input type="submit" name="ok" value="Buscar" /></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
<p>
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td height="21" bgcolor="#CCCCCC">Categoria</td>
<td bgcolor="#CCCCCC">Aguardando Reparo</td>
<td bgcolor="#CCCCCC">Em processo de Reparo</td>
<td bgcolor="#CCCCCC">Aguardando liberação para entrega</td>
</tr>
<?php
if(!empty($_POST['ok'])){
$sql_listar_pedidos = mysql_query($sql);
$total_registros = mysql_num_rows($sql_listar_pedidos);
while ($resultado_pedido = mysql_fetch_array($sql_listar_pedidos)){
$categoria = $resultado_pedido['categoria'];
$status = $resultado_pedido['status'];
$id_cliente = $resultado_pedido['id_cliente'];
?>
<?php
$melok = "SELECT categoria, status FROM cad_ped where (categoria = '$categoria' AND status='Aguardando Reparo')";
$melok = mysql_query($melok);
$total = mysql_num_rows($melok);
$melok_ = "SELECT DISTINCT * FROM cad_ped where categoria = '$categoria' AND status='Em processo de Reparo'";
$melok_ = mysql_query($melok_);
$total_ = mysql_num_rows($melok_);
$melok__ = "SELECT DISTINCT * FROM cad_ped where categoria = '$categoria' AND status='Aguardando liberação para entrega'";
$melok__ = mysql_query($melok__);
$total__ = mysql_num_rows($melok__);
$soma = $total + $total_ + $total__;
?>
<tr>
<?php
if ( $soma >= 1 ) {
?>
<td><?php echo $categoria ?></td>
<td><?php echo $total ?> </td>
<td><?php echo $total_ ?> </td>
<td><?php echo $total__ ?></td>
<?php
}
}
?>
<td></td>
</table>
<?php
echo $categoria .$total_registros .$total .$total_ .$total__ ?>
<?php
}
?>
<div><a href="painel.html">Voltar</a></div>
<div><a href="login.php">Sair</a></div>
</form>
Ele retorna
Categoria Aguardando Reparo - Em processo de Reparo - Aguardando liberação para entrega
nathy_cad 1 - 1 - 2
nathy_cad 1 - 1 - 2
nathy_cad 1 - 1 - 2
nathy_cad 1 - 1 - 2
oi 0 - 1 - 0
oi8010
Como eu quero
Categoria Aguardando Reparo - Em processo de Reparo - Aguardando liberação para entrega
nathy_cad 1 - 1 - 2
oi 0 - 1 - 0
Alguém pode me ajudar??????
Desde já agradeço
Carregando comentários...