Boa tarde galera Com base em um tutorial na internet, estou montando um sistema para verificar disponibilidade de quarto para fazer a reserva online e gostaria de uma ajuda. O sistema está funcionando em partes, já consigo fazer algumas coisas utilizando o codigo que já tenho, porem falta algumas coisas que explico mais abaixo CREATE TABLE IF NOT EXISTS `quarto` (
`quartoID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(40) NOT NULL,
`descricao` varchar(250) NOT NULL,
`maxocupacao` smallint(5) unsigned NOT NULL,
`valor` smallint(5) unsigned NOT NULL,
`minimo` smallint(5) unsigned NOT NULL,
`inicio` date NOT NULL,
`fim` date NOT NULL,
`quant` varchar(3) NOT NULL,
PRIMARY KEY (`quartoID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4;
INSERT INTO `quarto` (`quartoID`, `nome`, `descricao`, `maxocupacao`, `valor`, `minimo`, `inicio`, `fim`, `quant`) VALUES
(1, 'STANDARD SOLTEIRO', 'Tarifa de balcão', 1, 110, 2, '2011-06-01', '2011-06-22', '3'),
(2, 'STANDARD DUPLO ', 'Para duas pessoas', 2, 130, 2, '2011-06-01', '2011-06-23', '2'),
(3, 'STANDARD TRIPLO', 'para 3 pessoas', 3, 100, 2, '2011-06-01', '2011-06-23', '2');
CREATE TABLE IF NOT EXISTS `reserva` (
`resID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`checkin` date NOT NULL,
`checkout` date NOT NULL,
`quartoID` int(10) unsigned NOT NULL,
`hospede` varchar(80) NOT NULL,
`comentario` varchar(250) NOT NULL,
PRIMARY KEY (`resID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4;
INSERT INTO `reserva` (`resID`, `checkin`, `checkout`, `quartoID`, `hospede`, `comentario`) VALUES
(1, '2011-06-12', '2011-06-18', 1, 'Teste', 'Teste'),
(2, '2011-06-01', '2011-06-08', 2, 'Robson', 'Teste'),
(3, '2011-06-01', '2011-06-05', 1, 'Testet', 'asdasd');
Página disponibilidade recebe as datas de check-in e check-out separados por dia, mes e ano e quandidade de pessoas
Dentro do código abaixo:
Recebe data de entrada e saida e calcula quantos dias ($dias_diferenca)
Escreve por extenso a data recebida (formata_data_extenso)
<?
$numerodeocupante = $_POST['numerodeocupante'];
$diain = $_POST['arrDate_day'];
$mesin = $_POST['arrDate_mon'];
$anoin = $_POST['arrDate_year'];
$diaout = $_POST['deptDate_day'];
$mesout = $_POST['deptDate_mon'];
$anoout = $_POST['deptDate_year'];
$datain= '"' .$_POST["arrDate_year"] . "-" . $_POST["arrDate_mon"] . "-" . $_POST["arrDate_day"]. '"';
$dataout= '"' .$_POST["deptDate_year"] . "-" . $_POST["deptDate_mon"] . "-" . $_POST["deptDate_day"]. '"';
$datain2= $_POST["arrDate_year"] . "-" . $_POST["arrDate_mon"] . "-" . $_POST["arrDate_day"];
$dataout2= $_POST["deptDate_year"] . "-" . $_POST["deptDate_mon"] . "-" . $_POST["deptDate_day"];
//data check-in
$ano1 = $anoin;
$mes1 = $mesin;
$dia1 = $diain;
//data creck-out
$ano2 = $anoout;
$mes2 = $mesout;
$dia2 = $diaout;
//calculo timestam das duas datas
$timestamp1 = mktime(0,0,0,$mes1,$dia1,$ano1);
$timestamp2 = mktime(4,12,0,$mes2,$dia2,$ano2);
//diminuo a uma data a outra
$segundos_diferenca = $timestamp1 - $timestamp2;
//echo $segundos_diferenca;
//converto segundos em dias
$dias_diferenca = $segundos_diferenca / (60 * 60 * 24);
//obtenho o valor absoluto dos dias (tiro o possível sinal negativo)
$dias_diferenca = abs($dias_diferenca);
//tiro os decimais aos dias de diferenca
$dias_diferenca = floor($dias_diferenca);
echo "$datain<br>$dataout<br>$datain2<br>$dataout2<br>$dias_diferenca<br>";
function formata_data_extenso($strDate)
{
// Array com os dia da semana em português;
$arrDaysOfWeek = array('Domingo','Segunda-feira','Terça-feira','Quarta-feira','Quinta-feira','Sexta-feira','Sábado');
// Array com os meses do ano em português;
$arrMonthsOfYear = array(1 => 'Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');
// Descobre o dia da semana
$intDayOfWeek = date('w',strtotime($strDate));
// Descobre o dia do mês
$intDayOfMonth = date('d',strtotime($strDate));
// Descobre o mês
$intMonthOfYear = date('n',strtotime($strDate));
// Descobre o ano
$intYear = date('Y',strtotime($strDate));
// Formato a ser retornado
return $arrDaysOfWeek[$intDayOfWeek] . ', ' . $intDayOfMonth . ' de ' . $arrMonthsOfYear[$intMonthOfYear] . ' de ' . $intYear;
}
echo formata_data_extenso($_POST["arrDate_year"] . "-" . $_POST["arrDate_mon"] . "-" . $_POST["arrDate_day"]);
echo "<br>";
echo formata_data_extenso($_POST["deptDate_year"] . "-" . $_POST["deptDate_mon"] . "-" . $_POST["deptDate_day"]);
/* this block opens a connection to the database */
$link = mysql_connect("mysql.site.com.br", "root", "root")
or die("Servidor não disponível (BD OUT 001)");
if (!mysql_select_db("bancodedados", $link)) {
echo "Banco de dados não disponível. Entre em contato com o Administrador";
exit;
}
/* This block builds the query notice that we added a search condition to the end (WHERE maxOccupants > " . $_POST["numGuests"]) */
$myQuery = "SELECT quarto.quartoID, nome, descricao, maxocupacao, valor, minimo, inicio, fim ";
$myQuery = $myQuery . " FROM quarto, reserva ";
$myQuery = $myQuery . " WHERE quarto.maxocupacao >= " . $_POST["numerodeocupante"];
$myQuery = $myQuery . " AND quarto.minimo <= " . $dias_diferenca;
$myQuery = $myQuery . " AND quarto.inicio <= " . $datain;
$myQuery = $myQuery . " AND quarto.fim >= " . $dataout;
$myQuery = $myQuery . " AND quarto.quant !=0";
$myQuery = $myQuery . " AND ".$datain." >= reserva.checkout";
$myQuery = $myQuery . " GROUP BY quartoID, nome, descricao, maxocupacao, valor, minimo, inicio, fim ";
$myQuery = $myQuery . " HAVING count(*) > 0 ";
$myQuery = $myQuery . " ORDER BY maxocupacao asc, valor desc ";
/* Now we run the query and put a pointer to the results into $result*/
$result = mysql_query($myQuery, $link)
or die("Erro para localizar quarto");
/* And we check to see if we did in fact get any rows back, if so we print them*/
if (mysql_num_rows($result) > 0)
{
print "<center><br><br>Results<BR><table border=1 width=90%><tr><th></th><th>Name</th><th>Description</th><th>Maximum<br>Occupants</th><th>Rate</th></tr>\n";
/* print each row */
while ($row = mysql_fetch_array($result))
{
print "<tr><td><A HREF='reservar.php?quartoID=" . $row["quartoID"] . "&datain2=$datain2&dataout2=$dataout2'>Reserve</td><td>" . $row["nome"] . "</A></td><td>" . $row["descricao"] . "</td><td>" . $row["maxocupacao"] . "</td><td>" . $row["valor"] . "</td></tr>";
}
print "</table></center><br>";
} else {
/*or print a message saying there are no rooms*/
print "<center>Não existe quarto disponível.</center><br>";
}
mysql_close($link);
?> O código está ai para todos que queiram utilizar, os que puderem me ajudar nos seguintes itens, vou ficar muito grato. Em uma pousada, podemos dizer que durante aquele período o quarto pode ser reservado algumas vezes. Um exemplo Tenho 4 quartos, durante esse período ele pode ser reservado 4 vezes, porem quando chegar no limite, informar que somente tem disponibilidade para depois do primeiro check-in possível que vai está no banco 'reserva' para aquele tipo de quarto Por favor, me ajudem!! Abraços