Hi. I'm actually working on a booking system in PHP and I created a script that searches a MySql database checking if a X day is occupied (or not) storing un-occupied dates in an array. After that, I return the array to a function that prints all dates, passing those dates to a jQuery Datepicker array variable, so Datepicker knows which date it should display. The thing is, when I print this array, it prints A LOT OF new lines before starts printing the dates itself.
This is the code that prints the array in the html file:
var datasDisponiveis =
<?php
if (!(isset($_SESSION["dias"]))) {
$_SESSION["dias"] = datasDisp();
}
echo "["; imprimeDatas($_SESSION["dias"]); echo "];";
?>
Translation: dias = days, datasDisp = function that create de available dates array, imprimeDatas = printDates (some portuguese class as bonus lol)
Here is datasDisp():
function datasDisp () {
include "conecta.php";
$consulta = "SELECT id,mes,dia,ano FROM agenda";
$data = mysql_query($consulta,$conexao);
$vetordata = mysql_fetch_array($data);
$sql = "SELECT COUNT(*) FROM domingo";
$horas_disponiveis = mysql_query($sql,$conexao);
$vetor= mysql_fetch_array($horas_disponiveis);
$semana = array('domingo'=>0,'segunda'=>0,'terça'=>0,'quarta'=>0,'quinta'=>0,'sexta'=>0,'sabado'=>0);
foreach ($semana as $dia => $valor) {
$sql = "SELECT COUNT(*) FROM $dia";
$horas_disponiveis = mysql_query($sql,$conexao);
$vetor= mysql_fetch_array($horas_disponiveis);
$semana[$dia] = $vetor[0];
}
$atual = new DateTime(date("Y-m-d"));
$atual->modify('+10 day');
$final = dataFinal();
$i = 0;
$j = 1;
while ($atual->format('j/n/Y') != $final) {
$ver_atual = explode("/",$atual->format('j/n/Y'));
$dia_s = dia_semana($ver_atual[0],$ver_atual[1],$ver_atual[2]);
if (verificaDataDisponivel($ver_atual[0],$ver_atual[1],$ver_atual[2],$semana[$dia_s])) {
$datad[$i] = $atual->format('n/j/Y');
$i++;
}
$atual->modify('+1 day');
}
return $datad;
}
And imprimeDatas():
function imprimeDatas ($datas) {
foreach ($datas as $dia => $valor) {
echo "'".$datas[$dia]."', ";
}
}
I hope it's not too confusing to understand.
What's printing:
var datasDisponiveis =
['9/24/2010', '9/25/2010', '9/26/2010', '9/27/2010', '9/28/2010', '9/29/2010', '9/30/2010', '10/1/2010', '10/2/2010', '10/3/2010', '10/4/2010', '10/5/2010', '10/6/2010', '10/7/2010', '10/8/2010', '10/9/2010', '10/10/2010', '10/11/2010', '10/12/2010', '10/13/2010', '10/14/2010', '10/15/2010', '10/16/2010', '10/17/2010', '10/18/2010', '10/19/2010', '10/20/2010', '10/21/2010', '10/22/2010', '10/23/2010', '10/24/2010', '10/25/2010', '10/26/2010', '10/27/2010', '10/28/2010', '10/29/2010', '10/30/2010', '10/31/2010', '11/1/2010', '11/2/2010', '11/3/2010', '11/4/2010', '11/5/2010', '11/6/2010', '11/7/2010', '11/8/2010', '11/9/2010', '11/10/2010', '11/11/2010', '11/12/2010', '11/13/2010', '11/14/2010', '11/15/2010', '11/16/2010', '11/17/2010', '11/18/2010', '11/19/2010', '11/20/2010', '11/21/2010', '11/22/2010', '11/23/2010', '11/24/2010', '11/25/2010', '11/26/2010', '11/27/2010', '11/28/2010', '11/29/2010', '11/30/2010', '12/1/2010', '12/2/2010', '12/3/2010', '12/4/2010', '12/5/2010', '12/6/2010', '12/7/2010', '12/8/2010', '12/9/2010', '12/10/2010', '12/11/2010', '12/12/2010', '12/13/2010', '12/14/2010', '12/15/2010', '12/16/2010', '12/17/2010', '12/18/2010', '12/19/2010', '12/20/2010', '12/21/2010', '12/22/2010', '12/23/2010', '12/24/2010', '12/25/2010', '12/26/2010', '12/27/2010', '12/28/2010', '12/29/2010', '12/30/2010', ];
Thanks in advance!