PHP 开放时间 &使馆假期
我一直在使用这段代码:
$rawsql = "SELECT
*
FROM
_erc_foffices n
INNER JOIN
_erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE())
INNER JOIN
_erc_openings_times t ON o.id = t.opening_id
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
AND
(
n.id = %d
)
;";
$sql = sprintf($rawsql, mysql_real_escape_string($id));
$result = mysql_query($sql);
/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<div class='address'><p>" . $row["title"] . "<br/>";
echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . "</p>";
echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>";
echo "<div class='email'>" . $row["email"] . "</div>";
$extra_notes = $row["extra"];
}
} else {
$embassy_closed = mysql_query("SELECT * FROM _erc_foffices WHERE id = '$embassy_id'");
while($row = mysql_fetch_array($embassy_closed))
{
echo "<div class='address'><p>" . $row["title"] . "<br/>";
echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . " <font color='red'>The embassy is closed.</font></p>";
echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>";
echo "<div class='email'>" . $row["email"] . "</div>";
$extra_notes = $row["extra"];
}
}
它从数据库中获取大使馆的开放时间,并决定大使馆当前是否开放。如果不是,则会显示一条消息“大使馆已关闭”。现在我需要添加公共假期,因此我将查询更改为如下所示:
$rawsql = "SELECT
*
FROM
_erc_foffices n
INNER JOIN
_erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE())
INNER JOIN
_erc_openings_times t ON o.id = t.opening_id
LEFT JOIN
_erc_holidays h ON h.branch_id = n.id
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
AND
(
n.id = %d
)
AND
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP())
NOT BETWEEN UNIX_TIMESTAMP(h.begins_at) AND UNIX_TIMESTAMP(h.ends_at)
)
;";
但这仅输出地址/电子邮件等两次。
有人可以指出我做错了什么吗?
感谢您的帮助
编辑:我现在已经解决了这个问题,只是使用了第二个查询和 if/else 循环。
I've been using this code:
$rawsql = "SELECT
*
FROM
_erc_foffices n
INNER JOIN
_erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE())
INNER JOIN
_erc_openings_times t ON o.id = t.opening_id
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
AND
(
n.id = %d
)
;";
$sql = sprintf($rawsql, mysql_real_escape_string($id));
$result = mysql_query($sql);
/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<div class='address'><p>" . $row["title"] . "<br/>";
echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . "</p>";
echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>";
echo "<div class='email'>" . $row["email"] . "</div>";
$extra_notes = $row["extra"];
}
} else {
$embassy_closed = mysql_query("SELECT * FROM _erc_foffices WHERE id = '$embassy_id'");
while($row = mysql_fetch_array($embassy_closed))
{
echo "<div class='address'><p>" . $row["title"] . "<br/>";
echo $row["address_1"] . "<br/> " . $row["address_2"] . "<br/> " . $row["address_3"] . "<br/> " . $row["address_4"] . "<br/> " . $row["address_5"] . " <font color='red'>The embassy is closed.</font></p>";
echo "<div class='buttons'><img src='http://localhost/erc/images/texttouser_button.png'/><br/><a href='" . $row["url"] . "' target='blank'><img src='http://localhost/erc/images/website_button.png'/></a></div></div>";
echo "<div class='email'>" . $row["email"] . "</div>";
$extra_notes = $row["extra"];
}
}
which takes the opening times for embassies from a database and decides whether or not the embassy is currently open. If it's not, a message saying 'The embassy is closed' is shown. Now I need to add in public holidays, so I have altered the query to look like this:
$rawsql = "SELECT
*
FROM
_erc_foffices n
INNER JOIN
_erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE())
INNER JOIN
_erc_openings_times t ON o.id = t.opening_id
LEFT JOIN
_erc_holidays h ON h.branch_id = n.id
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
AND
(
n.id = %d
)
AND
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP())
NOT BETWEEN UNIX_TIMESTAMP(h.begins_at) AND UNIX_TIMESTAMP(h.ends_at)
)
;";
but this just outputs the address/email etc twice.
Could someone point out what I'm doing wrong please?
Thanks for any help
Edit: I have fixed this now, just used a 2nd query and if/else loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会自称是专家,您的联接看起来非常复杂,但根据我的经验,如果您的查询中出现重复项,那么您就没有正确分组,并且我在查询中根本看不到 GROUP BY 。
希望有帮助。
顺便问一下...您的公共假期数据是从哪里获取的?我现在正在寻找完全相同的东西,这就是我发现这篇文章的原因。
I wouldn't profess to be an expert and your joins look very complicated, but in my experience if you're getting duplicates in your query then you haven't grouped properly and I can't see a GROUP BY at all in your query.
Hope that helps.
By the way... where did you source your public holiday data? I'm looking for the exact same thing at the moment which is how I came across this post.
使用第二个查询和 if/else 循环,它按我想要的方式工作。
Used a 2nd query and if/else loop and it's working as I wanted.