Consult Mysql to bring events before, during and after the current date

Asked

Viewed 379 times

0

I asked this question but it had not become very clear the code so I decided to post again with it complete. I seek the help of friends for a solution if possible of this code. I have a mysql database and am trying to make an event warning before, during and after. The warning before and during works if you only have 1 event daily, if you have more than 1 no longer appears. in the database I have:

INSERT INTO `agenda` (`id`, `nome`, `data`, `time_off`) VALUES
(1, 'agenda um', '2016-09-06 12:28:00', '2016-09-06 12:32:00'),
(2, 'agenda dois', '2016-09-06 14:00:00', '2016-09-06 14:10:00'),
(3, 'agenda tres', '2016-09-07 15:00:00', '2016-09-07 15:15:00');

with this code in php:

<style>
body {background-color:#B0ABA5;padding-top: 50px;padding-bottom: 20px;}
#home{margin-top: 10px}
#home .aviso{background-color:#979898;border:2px solid #a1a1a1; border-radius: 20px}
#home .aviso p{margin:0px}
#home .aviso a{background-color:#b0b0b0;border:2px solid #a1a1a1; border-radius: 10px;color:red;font-weight: bold;text-decoration: none}
</style>
<?php
$conectar = mysql_connect("localhost", "root", "") or die("Erro na conexão");
mysql_select_db("agenda")or die("Base não encontrada");
//datas atuais
setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
$ano = date('Y');
$mes = date('m');
$dia = date('d');
$horas = date('H');
$horasEx = date('H:i');
$data_atual = date('Y-m-d');
$horazero = date('00:00:01');
$dia_horazero = date('d 00:01');
$resultado_evento = mysql_query("SELECT * FROM agenda WHERE  Year(data) = '$ano' and Month(data) = '$mes' and Day(data) >= '$dia' and Hour(data) >= '$horazero' order by data asc limit 1");
?>
<!--evento do dia-->
<div id="home">
 <h2 class="text-center">teste</h2>
<?php
while ($linhas_evento = mysql_fetch_assoc($resultado_evento)) {
//datas banco dados
$dia_atualEvento = $linhas_evento['data'];
$datahora_evento = strtotime($dia_atualEvento);
$total_eventoBd = date("Y-m-d", $datahora_evento);
$ano_eventoBd = date("Y", $datahora_evento);
$diaMes_evento = date("d-m", $datahora_evento);
$mes_evento = date("m", $datahora_evento);
$dia_evento = date("d", $datahora_evento);
$diahora_evento = date("d H:i", $datahora_evento);
$hora_Exinicio = date('H:i', $datahora_evento);
$time_off = $linhas_evento['time_off'];
$final_evento = strtotime($time_off);
$final_eventoBd = date("H:i", $final_evento);
// antes do evento esta msg esta ok-->
if ($data_atual === $total_eventoBd and $horasEx < $hora_Exinicio) {
echo "antes evento";
?>
<div class="container">
  <div class="col-md-12">
    <div class="row aviso">
      <h3 class="text-center">Me alegrei quando me disseram: Vamos a casa do Senhor</h3>
      <p class="text-center">Hoje haverá culto de <?ph echo $linhas_evento['ministerio']; ?> na igreja as <?php echo $hora_Exinicio; ?>.    </p>
    </div>
  </div>
</div>
<?php
}
// durante o evento esta msg esta ok -->
elseif ($total_eventoBd === $data_atual && $horasEx < $final_eventoBd) { echo "durante"; ?>
<div class="container">
  <div class="col-md-12">
    <div class="row aviso">
      <p class="text-center">Agora está tendo culto de <?ph echo $linhas_evento['ministerio']; ?> na igreja até as <?php echo $final_eventoBd . " hrs."; ?>.</p>
      <p class="text-center">Venha participar, ainda da tempo ou acesse.</p>
    </div>
  </div>
</div>
<?php
}
// busca proximo evento sendo proximo dia não encontra
elseif ($ano === $ano_eventoBd && $mes === $mes_evento && $dia < $dia_evento) {
?>
<p class="text-center">O nosso próximo evento será dia<strong> <?php  echo $diaMes_evento . "</strong> as <strong>" . $hora_Exinicio . "</strong>"; ?>. Voce é nosso convidado.</p>
<?php ?>
<div class="container">
  <div class="col-md-12">
    <div class="row aviso">
      <p class="text-center"><strong>Romanos 10:17 </strong> “De sorte que a fé é pelo ouvir, e o ouvir pela palavra de Deus.”</p>
      <p class="text-center">O nosso próximo culto será de <?ph echo $linhas_evento['ministerio'] ." dia<strong> " . $diaMes_evento . "</strong> as <strong>" . $hora_Exinicio . "</strong>"; ?>. Voce é nosso convidado.</p>
      <p class="text-center">Ou acesse nossa Agenda e se programe para o próximo evento.</p>
    </div>
  </div>
</div>
<?php
  }
}
?>
</div>

When there is an event on the day it appears, if there are two events the second one with larger schedule than the first one does not appear. I have made several attempts no more solved, and when there is no event in the day would have to show the next day.. If you could help me solve this problem I’d be grateful

  • Hello there, Alexandre. Your database query is set with conditions that determine what will be displayed in the code, reduce the filter by taking the time condition and do a new test. Check that the result of the query is returned everything you need, so you know where the problem is, in the query or in the code.

  • 1

    Remove the limit 1 from your SQL query to return more than 1 record.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.