PHP脚本打印不必要的新行

时间:2010-09-14 12:53:52

标签: php jquery calendar newline

我实际上正在使用PHP预订系统,我创建了一个脚本,用于搜索MySql数据库,检查X天是否被占用(或者不是)在数组中存储未占用的日期。之后,我将数组返回到打印所有日期的函数,将这些日期传递给jQuery Datepicker数组变量,因此Datepicker知道它应该显示的日期。问题是,当我打印这个数组时,它会在开始打印日期之前打印出很多新行。

这是在html文件中打印数组的代码:

var datasDisponiveis = 
<?php
    if (!(isset($_SESSION["dias"]))) {
        $_SESSION["dias"] = datasDisp();
    }
    echo "["; imprimeDatas($_SESSION["dias"]); echo "];";
?>

翻译:dias = days,datasDisp =创建可用日期数组的函数,imprimeDatas = printDates(某些葡萄牙语类作为奖励lol)

这是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;
}

并且imprimeDatas():

function imprimeDatas ($datas) {
 foreach ($datas as $dia => $valor) {
 echo "'".$datas[$dia]."', ";
 }
}

我希望理解它不会太混乱。

什么是印刷品:

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', ];

提前致谢!

3 个答案:

答案 0 :(得分:2)

这个包含文件可能会打印出空行:

include "conecta.php";

我会仔细检查以确保该文件中的<?php ?>标记之外没有空白行和/或空格。

答案 1 :(得分:1)

嗯,奇怪......

也许这会有所帮助:

<?php
    if (!isset($_SESSION["dias"])) { $_SESSION["dias"] = datasDisp(); }
    echo "var datasDisponiveis = [".imprimeDatas($_SESSION['dias'])."];";
?>

答案 2 :(得分:0)

如果您的代码(主程序或任何包含文件)在开头“&lt;?php”标签之前或结尾“?&gt;”之后有任何空白区域标记,您将在输出中得到虚假的空白行。

这是一个非常常见的错误,尤其是在文件的末尾,因此请检查所有包含内容并删除顶部和底部的任何不必要的空格或空行。