7
I’ve set up a calendar with a PHP + Mysql calendar , it’s already 80% of the development.
But, I have a problem, like if I register for an event for the day 20/07, this appointment is displayed normally in the calendar. If I make another appointment for the same day, the calendar only displays the first scheduled appointment. But I wanted every appointment of the day to be shown.
Follow the complete code of the same.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<?php
$idUsuario = ($_SESSION['UsuarioID']);
$mes=date('m');
$ano=date('Y');
if($mes == 1)
{
$qtdDiasMes=31;
$nomeMesMes="Janeiro";
}
if($mes == 2)
{
$qtdDiasMes=28;
$nomeMes="Fevereiro";
}
if($mes == 3)
{
$qtdDiasMes=31;
$nomeMes="Março";
}
if($mes == 4)
{
$qtdDiasMes=30;
$nomeMes="Abril";
}
if($mes == 5)
{
$qtdDiasMes=31;
$nomeMes="Maio";
}
if($mes == 6)
{
$qtdDiasMes=30;
$nomeMes="Junho";
}
if($mes == 7)
{
$qtdDiasMes=31;
$nomeMes="Julho";
}
if($mes == 8)
{
$qtdDiasMes=31;
$nomeMes="Agosto";
}
if($mes == 9)
{
$qtdDiasMes=30;
$nomeMes="Setembro";
}
if($mes == 10)
{
$qtdDiasMes=31;
$nomeMes="Outubro";
}
if($mes == 11)
{
$qtdDiasMes=30;
$nomeMes="Novembro";
}
if($mes == 12)
{
$qtdDiasMes=31;
$nomeMes="Dezembro";
}
?>
<div class="row">
<div class="col-md-12">
<a data-toggle="modal" href='#formAddEvent'>
<button type="button" class="btn btn-success btn-default">Novo Compromisso</button>
</a>
</div>
</div>
<div class="modal fade" id="formAddEvent" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">
<strong>Cadastro de novo compromisso </strong>
</h4>
</div>
<div class="modal-body">
<div class="portlet-body form">
<form class="form-horizontal" method="POST" action="<?php $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" id="addEvent" autocomplete="off">
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-4"><font color="red">Data do evento</font></label>
<div class="col-md-8">
<input type="date" class="form-control" id="txtData" name="txtData">
<input type="hidden" class="form-control" id="txtIdUsuario" name="txtIdUsuario" value="<?=$idUsuario;?>">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-4">Assunto</label>
<div class="col-md-6">
<input type="text" class="form-control" id="txtAssunto" name="txtAssunto">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-4">Lembrete</label>
<div class="col-md-8">
<textarea id="txtMensagem" name="txtMensagem"> </textarea>
</div>
</div>
</div>
</div><!--::: ROW :: -->
</div><!-- Form Body -->
</div><!-- portlet form-body -->
</div><!-- modal body -->
<div class="modal-footer">
<button type="submit" name="btnSubmit" class="btn btn-success btn-default">Salvar</button>
<button type="button" class="btn default" data-dismiss="modal">Fechar</button>
</form>
</div>
</div><!-- modal content -->
</div>
</div>
<hr>
<div class="row">
<div class="portlet box red">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-calendar"></i>Minha Agenda
</div>
</div>
<div class="portlet-body">
<div class="row">
<div class="col-md-12">
<p align="center"><font size="6">
<b><?php echo $nomeMes . " de " . $ano; ?></b>
</font></p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-striped table-bordered table-hover" width="350" height="450" align="center">
<tr>
<td width=30><center>Dom</center></td>
<td width=30><center>Seg</center></td>
<td width=30><center>Ter</center></td>
<td width=30><center>Qua</center></td>
<td width=30><center>Qui</center></td>
<td width=30><center>Sex</center></td>
<td width=30><center>Sab</center></td>
</tr>
<?php
echo "<tr align='center'>";
for($i=1; $i<=$qtdDiasMes;$i++)
{
$diadasemana = date("w",mktime(0,0,0,$mes,$i,$ano));
$cont = 0;
if($i == 1)
{
while($cont < $diadasemana)
{
echo "<td width='194' height='107' style='size:12px;'> </td>";
$cont++;
}
}
if($i >=1 and $i <= 9){
$dataCalendar = $ano.'-'.$mes.'-'.'0'.$i; //pega o Ano , o mes , e o dia que recebe o valor de $i
} else{
$dataCalendar = $ano.'-'.$mes.'-'.$i; //pega o Ano , o mes , e o dia que recebe o valor de $i
}
$sql = mysql_query("SELECT * FROM tblAgenda where data = '$dataCalendar' and idUsuario = $idUsuario");
$row = mysql_fetch_array ($sql);
$assunto = $row['assunto'];
$idModal = $row['id'];
echo " <td width='194' height='107' style='size:12px;' >"
.$i;
// dia da semana
echo" <center>
<a data-toggle='modal' href='#".$idModal."'>
".$assunto."
</a>
</center>";
echo "</td>";
if($diadasemana == 6)
{
echo "</tr>";
echo "<tr align='center'>";
}
}
echo "</tr>";
?>
</table>
</div>
</div><!-- ::: ROW ::: -->
</div><!-- PORTLED BODY -->
</div><!-- PORTLED BOX -->
</div><!-- ROW -->
<?php
// parte que faz os Modals aparecerem.
$sql2 = mysql_query("SELECT * FROM tblAgenda where idUsuario = $idUsuario");
while($row2 = mysql_fetch_array ($sql2))
{
$assuntoModal = $row2['assunto'];
$mensagem = $row2['mensagem'];
$idEvent = $row2['id'];
echo '
<div class="modal fade" id="'.$idEvent.'" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">'.$assuntoModal.'</h4>
</div>
<div class="modal-body">
'.$mensagem.'
</div>
<div class="modal-footer">
<button type="button" class="btn default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>';
}
if(isset($_POST['btnSubmit'])){
$idUsuario = $_POST['txtIdUsuario'];
$data = $_POST['txtData'];
$assunto = $_POST['txtAssunto'];
$mensagem = $_POST['txtMensagem'];
$sqlCadastro = "INSERT INTO tblAgenda
(idUsuario,assunto,mensagem,data) VALUES ($idUsuario,'$assunto','$mensagem','$data')";
mysql_query($sqlCadastro) or die (mysql_error() . 'Por favor , verifique se todos os dados estão preenchidos corretamente');
if(mysql_affected_rows() == 1) //verifica se foi afetada alguma linha, nesse caso inserida alguma linha
{
echo" <script language='javascript' type='text/javascript'>
alert('Compromisso agendado com sucesso');window.location.href='/admin2/perfil/agenda'
</script>";
}
else
{
echo"<script language='javascript' type='text/javascript'>
alert('Não foi possível enviar esse formulario');window.location.href='/admin2/contatos/lista-de-contatos'
</script>";
}
}
My table tblAgenda
:
id int(11) AUTO_INCREMENT Primária
idUsuario int(11)
assunto varchar(200) latin1_swedish_ci
mensagem varchar(999) latin1_swedish_ci
data date
The code is very nice and can be used if you want, because it is easy to understand. I’ve always looked on the Internet but I’ve never found anything like.
If anyone can help me improve the code will help me and help many others.
I like to use this guy here when working with calendars: http://fullcalendar.io/. He integrates well with the backend using Ajax.
– gmsantos
In the AP edition: "I showed it to a friend , and it was only adding a while between the <td> </td> that displays the subject. Here is the code for anyone who wants a calendar with a php + mysql calendar. That can help to improve the code,/ "
– Jorge B.
Henry I will reverse your question. Put the code with the changes as answer. Here we ask questions and answers. And then we mark the right answer.
– Jorge B.