views:

78

answers:

3

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!

+1  A: 

Hm, strange...

Maybe this helps:

<?php
    if (!isset($_SESSION["dias"])) { $_SESSION["dias"] = datasDisp(); }
    echo "var datasDisponiveis = [".imprimeDatas($_SESSION['dias'])."];";
?>
captaintokyo
It didn't, here's the output: <new lines> <dates> , var datasDisponiveis = [];
dccarmo
It must be in `conecta.php` as MacAnthony suggests. Can you post that code too?
captaintokyo
Yep, it was there. Thank you for your help!
dccarmo
+2  A: 

It's likely that this include file is printing out blank lines:

include "conecta.php";

I would double check to make sure there are no blank lines and/or spaces outside of the <?php ?> tags in that file.

MacAnthony
I think you are right.
captaintokyo
Well, that dataDisp function is within another archive which is a function library that has a bunch of functions. Each of these functions has an include "conecta.php";, which I think it's dumb (this is a legacy code, I didn't write myself) but I'm in a hurry to release the system. Anyways, I tried to remove every new line from conecta.php, but it didn't work.
dccarmo
I doubled checked here and indeed it had 2 new lines at the end here. I removed it and it worked! Thank you so much! :D
dccarmo
It's a good practice to leave the closing php tag off, so additional newlines at the end of the file won't cause any problems.
Maerlyn
A: 

If your code (either the main program or any include file) has any white space either before the beginning "<?php" tag or after the ending "?>" tag, you will get spurious blank lines in your output.

This is quite a common error, especially at the end of the file, so check all your includes and remove any unnecessary spaces or blank lines at the top and bottom.

Spudley
What some of the libraries archives have before de <?php is a javascript function defined. Would that be a problem?
dccarmo