Jump to content
Fórum Script Brasil
  • 0

Agrupar registros


claytonprog

Question

Pessoal olha eu de novo aqui:

Quero fazer uma consulta para agrupar alguns registros e exibilos por mês.

Primeiro estou tentando gerar os meses para estes registros, na tabela tá assim:

id id_cliente id_projeto data

1 1 1 2012-01-02

1 1 1 2012-01-11

1 1 1 2012-02-02

1 1 1 2012-03-02

1 1 1 2012-05-11

Está me listando os meses assim:

Janeiro

Janeiro

Feveiro

Março

Maio

Janeiro está se repetindo ( errado ) era para agrupar as datas de janeiro.

Quando deveria está listando os meses assim:

Janeiro

Feveiro

Março

Maio

Segue abaixo o código pra ver se alguém pode resolver o problema.

<div class="content-box">
                    <div class="box-body">
                        <div class="box-header clear">
                            <h2>Listagem de Arquivos</h2>
                        </div>
                        <div class="box-wrap clear">
                        
                        
                        
                       <div class="page clear">
            
            <div class="columns clear">
            
            
             <?php
                               
                                  include "../connections/conexao.php";
                        
                                  $id_projeto = $_GET["id_projeto"];
                                  $id_cliente = $_GET["id_cliente"];
                                        
                                                          
                                    $strSQL = mysql_query("SELECT * FROM upload_projetos where id_projeto='$id_projeto' and id_cliente='$id_cliente' GROUP BY data asc");
                                    
                                    
                                        while ($aux = mysql_fetch_array($strSQL)) {
                                                    
                                        $id = $aux['id'];   
                                       $_dt_not= $aux['data'];    
                                
                                list( $date, $time ) = explode( ' ', $_dt_not );
                                $data    = explode("-",$date);
                                $_dt_not = "$data[2]/$data[1]/$data[0]";
                                $dt_not = "$data[1]"; 
            
                                switch($dt_not) // acha o mês em português
                                {
                                  case "1":
                                    $mes_port = "Janeiro";
                                    break;
                                  case "2":
                                    $mes_port = "Fevereiro";
                                    break;
                                  case "3":
                                    $mes_port = "Março";
                                    break;
                                  case "4":
                                    $mes_port = "Abril";
                                    break;
                                  case "5":
                                    $mes_port = "Maio";
                                    break;
                                  case "6":
                                    $mes_port = "Junho";
                                    break;
                                  case "7":
                                    $mes_port = "Julho";
                                    break;
                                  case "8":
                                    $mes_port = "Agosto";
                                    break;
                                  case "9":
                                    $mes_port = "Setembro";
                                    break;
                                  case "10":
                                    $mes_port = "Outubro";
                                    break;
                                  case "11":
                                    $mes_port = "Novembro";
                                    break;
                                  case "12":
                                   $mes_port = "Dezembro";
                                   break;
                                }
                                                                    
                                                
                                                                
                              ?>
              
              <div class="col1-3">              
             
                    <div class="content-box">                    
                        <div class="box-body">                        
                            <div class="box-header clear">
                               
                            </div> 
                                                   
                               <div class="box-wrap clear">
                               
                                 <div align="center"><img src="images/calendario.png" alt="" />
                                   
                                    </div>
                                 <div align="center"><a  href="ver_obra.php?id=<? echo $dt_not;?>"><?= $mes_port;?></a></div>
                               
                          </div> 
                      </div>
                     
                    </div> 
                   
                    
                </div>
                
                  <?php
                    }                                        
                  ?>
                
                </div>
                
            
              <div class="box-wrap clear"></div> 
            
            </div> 
            </div>
            <!-- end of content-box -->
                       </div>
                       <p>
                        
                    
                    
                            
                          <div class="tab-footer clear"></div>
                    
                        </div>

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

O group by vai agrupar datas exatas, se fosse a mesma funcionaria legal.. mas são dias diferentes. e voce quer agrupar por mes, se for...

no lugar de group by data use group by month(data)

tente assim:

SELECT *,month(data) as Mes FROM upload_projetos where id_projeto='$id_projeto' and id_cliente='$id_cliente' GROUP BY by month(data)");

para achar o mes coloque

switch($aux["Mes"])

Edited by jissa
Link to comment
Share on other sites

  • 0

Funcionou perfeitamente! valeu.

Agora a segunda parte:

Preciso que quando fo r clicado no link:

<div align="center"><a href="ver_obra.php?id=<? echo $dt_not;?>"><?= $mes_port;?></a></div>

chamar a página ver_obra.php

Nessa página deverá listar as imagens das obras referente ao mês selecionado.

Preciso montar a SQL

A tabela está assim:

tabela: projetos

id

id_cliente

foto

data

descricao

----------------------------

tabela: upload_projetos

id

id_projeto

id_cliente

titulo

foto

data

Como ficaria a SQL sendo que preciso agrupar as imagens pelo : $id_projeto, $id_cliente e o mês selecionado.

Link to comment
Share on other sites

  • 0

se for para listar todos os uploads para o projeto de um cliente dentro do mes seria simples

SELECT * FROM upload_projetos

where id_projeto='$id_projeto' and id_cliente='$id_cliente' and month(data) ='$Mes'

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...