Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Martinson Freitas

Listar resultado da pesquisa por mês numa tabela formatada por CSS

Recommended Posts

Boa noite a todos!

 

Sou novo por aqui e estou iniciando os estudos em PHP, SQL, HTML5 e CSS.

 

Tenho uma tabela onde estão cadastrados a data de um certo evento e outra tabela onde está cadastrado os dados da pessoa que irá realizar o evento. Gostaria de exibir o resultado dessas duas tabelas agrupado por mês, com os campos data_evento, nome_participante.

 

Tipo a cada mês o campo <th> da tabela no html, fica com o nome do mês, com uma cor diferente para dar destaque. Em seguida viriam a data do evento e o participante.

 

Fiz o relacionamento das tabelas e consigo visualizar as informações, mas até o momento não consegui encontrar algo que me ajude a formatar dessa maneira. Já vi algo parecido em ASP, mas não consegui ver o código pra ver como funcionava. Esse é formato que desejo:

 

modelo.jpg

 

 

Esse é o código que tenho até o momento:

// INICIO DO CODIGO //
 
    $re = mysql_query("SELECT * FROM eventos_tb, reg_geral_tb WHERE reg_geral_tb.NOME = eventos_tb.NOME
                    AND eventos_tb.DT >= DATE(NOW()) ORDER BY DT ASC;");
    $total = mysql_num_rows($re);
    
    if(mysql_errno() != 0) {
        if(!isset($erros)) {
            echo "Erro o arquivo de inicializacao foi alterado!";
            exit;
        }
        echo $erros[mysql_errno()];
        exit;
    }
?>


<table width="100%" border="0" cellspacing="5">
    <th colspan="5" align="center">
        Acompanhe a agenda de eventos cadastrados!
    </th>
    
    <tr>
        <td bgcolor="#DCDCDC"><label>Data do evento</label></td>    
        <td bgcolor="#DCDCDC"><label>Nome</label></td> 
    </tr>
    
    <?php
    while($l = mysql_fetch_array($re)) {
        $evento   = strtotime($l["DT"]);
        $dt_evento = date('d/m/Y', $evento);;
        
        $nome        = $l["NOME"];   
        
    echo "
        <tr>
            <td>  $dt_evento</td>
            <td>  $nome</td>           
        </tr>\n";
        
    }
    
    @mysql_close();
    ?>    
    <tr>
        <td>
            Total de evento no ano = <?php echo "$total"; ?>
        </td>
        <td colspan="4" align="left">
              
        </td>
    </tr>
</table>
 
// FIM DO CODIGO //
 
A visualização desse código é padrão em linhas corridas, gostaria de melhorar a estética dessa tabela.

 

Se alguém puder me ajudar agradeço! Enquanto isso vou fuçando por aqui pra ver se encontro algo que me ajude. Abraços a todos!

Compartilhar este post


Link para o post
Compartilhar em outros sites

1 - Faz um switch por mês

2 - Dentro de cada mês, vc atribui uma variável diferente (ex. $varJaneiro, $varFevereiro) e sempre vai concatenando.

 

No final, você imprime as variáveis como bem entender.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Mesmo sem entender nada do que você falou e muito menos php, consegui fazer pesquisando sobre switch. O código ficou longo por causa da quantidade de concatenações, 12 meses... Mas deu tudo certo!

 

Obrigado pela ajuda!

 

Abraços!

 

P.S.: /* Código abaixo */

 

$mes = date("m", $dt);
$ano = date ("Y", $dt);

switch ($mes){
case 1:
echo "
<th colspan='5' bgcolor='ff00ff'> Janeiro/ $ano</th>
<tr>
<td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 2:
//código se var1 for 2
echo "
<th colspan='5' bgcolor='ff00ff'> Fevereiro/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 3:
//código se var1 for 3
echo "
<th colspan='5' bgcolor='ff00ff'> Março/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 4:
//código se var1 for 4
echo "
<th colspan='5' bgcolor='ff00ff'> Abril/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 5:
//código se var1 for 5
echo "
<th colspan='5' bgcolor='ff00ff'> Maio/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 6:
//código se var1 for 6
echo "
<th colspan='5' bgcolor='ff00ff'> Junho/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 7:
//código se var1 for 7
echo "
<th colspan='5' bgcolor='ff00ff'> Julho/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 8:
//código se var1 for 8
echo "
<th colspan='5' bgcolor='ff00ff'> Agosto/ $ano</th>
<tr><td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 9:
//código se var1 for 9
echo "
<th colspan='5' bgcolor='ff00ff'> Setembro/ $ano</th>
<tr>
<td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>

\n";
break;
case 10:
//código se var1 for 10
echo "
<th colspan='5' bgcolor='ff00ff'> Outubro/ $ano</th>
<tr>
<td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 11:
//código se var1 for 11
echo "
<th colspan='5' bgcolor='ff00ff'> Novembro/ $ano</th>
<tr>
<td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
case 12:
//código se var1 for 12
echo "
<th colspan='5' bgcolor='ff00ff'> Dezembro/ $ano</th>
<tr>
<td>  $dt</td>
<td>  $nome</td>
</tr>
<tr><td colspan='2'>  </td></tr>
\n";
break;
default:
//código se var não for nenhum dos casos anteriores
break;
}


Acabei de verificar que o <th> é repetido várias vezes... Estou vendo como melhorar...

 

Mas agradeço ajuda!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Faça assim, mais simples:

<?php

	switch(sua data){
		case "Janeiro":  $varJaneiro = "<tr><td>".sua data."</td></tr>";	
		case "Fevereiro":  $varFevereiro = "<tr><td>".sua data."</td></tr>";
		case "Março":  $varMarco = "<tr><td>".sua data."</td></tr>";
	}
	
?>
<table>
	<thead>
        <tr>
            <th>Janeiro</th>
        </tr>
    </thead>
    <tbody>
		<?php echo $varJaneiro; ?>
    </tbody>
</table>
<table>
	<thead>
        <tr>
            <th>Fevereiro</th>
        </tr>
    </thead>
    <tbody>
		<?php echo $varFevereiro; ?>
    </tbody>
</table>
<table>
	<thead>
        <tr>
            <th>Março</th>
        </tr>
    </thead>
    <tbody>
		<?php echo $varMarco; ?>
    </tbody>
</table>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Desculpa pela minha insistência, mas não deu certo!

 

Erro: "( ! ) Notice: Undefined variable: varJaneiro" | ( ! ) Notice: Undefined variable: varFevereiro | ( ! ) Notice: Undefined variable: varMarco"

 

Outra coisa, o <th> do mês vai aparecer mesmo que não tenha nenhum evento gravado para ele e é justamente o que eu quero omitir no código passado anteriormente.

 

De qualquer forma, obrigado pela força... Vou tentando por aqui!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Só criar a variável antes de usá-la.

É um notice, não necessariamente um erro fatal. A execução não trava.

 

$varJaneiro = "";

<?php

        $varJaneiro = "";
        $varFevereiro = "";
        $varMarco = "";

        switch(sua data){
		case "Janeiro":  $varJaneiro = "<tr><td>".sua data."</td></tr>";	
		case "Fevereiro":  $varFevereiro = "<tr><td>".sua data."</td></tr>";
		case "Março":  $varMarco = "<tr><td>".sua data."</td></tr>";
	}
	
?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Utilizei o conceito do Case mas usei o "if" o resultado não foi elegante, mas bastante satisfatório e atendeu o que eu esperava.. Segue abaixo:

 

<?php

$re = mysql_query("SELECT * FROM credencial_old_tb WHERE credencial_old_tb.DATA_INICIO >= DATE(NOW()) ORDER BY DATA_INICIO ASC;");

 

$l = mysql_fetch_array($re);
$inicio = strtotime($l["DATA_INICIO"]);
$dt_inicio = date('d/m/Y', $inicio);


$mes = date("m", $inicio);
$ano = date("Y", $inicio);

if ($mes==1) {
$mes_nome = "Janeiro";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==2) {
$mes_nome = "Fevereiro";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==3) {
$mes_nome = "Março";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==4) {
$mes_nome = "Abril";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==5) {
$mes_nome = "Maio";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==6) {
$mes_nome = "Junho";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}
if ($mes==7) {
$mes_nome = "Julho";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}
if ($mes==8) {
$mes_nome = "Agosto";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==9) {
$mes_nome = "Setembro";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==10) {
$mes_nome = "Outubro";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes=11) {
$mes_nome = "Novembro";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $mes++;
}
}

if ($mes==12) {
$mes_nome = "Dezembro";
$sql = mysql_query("SELECT * FROM credencial_old_tb WHERE month(DATA_INICIO)=$mes AND year(DATA_INICIO)=$ano ORDER BY DATA_INICIO ASC;");
$total = mysql_num_rows($sql);
if ($total>0) {
echo "
<th bgcolor='ff00ff'> $mes_nome</th>
\n";
while($m = mysql_fetch_array($sql)) {

$nome = $m["NOME"];
$start_city = $m["LOCAL_INICIO"];
$kind = $m["FORMA"];
$dia = date("d", strtotime($m["DATA_INICIO"]));
$dt_partida = date('d/n/Y',strtotime($m["DATA_INICIO"]));
$email = $m["EMAIL"];

echo "
<tr>
<td>  $dia</td>
<td>  $nome</td>
<td>  $start_city</td>
<td>  $kind</td>
<td>  $email</td>

</tr>\n";
}
echo "<tr><td colspan='5' align='left'>Total no mês $mes_nome: $total</td></tr>";
$mes == $ano++;
}
}
@mysql_close();
?>

 

=======================================================================

 

Acredito que pode ser melhorado, e continuarei tentando...

 

Qualquer sugestão e ajuda, agradeço!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.