PHP 脚本打印不必要的新行
我实际上正在用 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(一些葡萄牙语类作为奖励哈哈)
这里是 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', ];
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该包含文件很可能正在打印出空行:
我会仔细检查以确保该文件中的
> 标记之外没有空行和/或空格。
It's likely that this include file is printing out blank lines:
I would double check to make sure there are no blank lines and/or spaces outside of the
<?php ?>
tags in that file.嗯,奇怪......
也许这有帮助:
Hm, strange...
Maybe this helps:
如果您的代码(主程序或任何包含文件)在开始“”之后有任何空格标签,您将在输出中得到虚假的空行。
这是一个相当常见的错误,尤其是在文件末尾,因此请检查所有包含内容并删除顶部和底部任何不必要的空格或空行。
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.