gpt4 book ai didi

php - 如果某些列数据与原始查询不准确,如何阻止其显示?

转载 作者:行者123 更新时间:2023-11-29 11:37:22 25 4
gpt4 key购买 nike

有人可以帮助我处理帖子、查询和 echo 吗?记录有多个不同的日期,我想进行搜索,其中 6 个不同日期中的任何一个都在 2 个不同日期之间。当我从表单中搜索并发布帖子后,这会正确显示所有不同的记录,其中 6 个日期之一位于表单中选取的 2 个日期之间。但它显示了记录的所有日期。我只希望它显示实际上位于所选的 2 个日期之间的日期。

<?php
if (isset($_POST['search'])) {
$fromdate = mysql_real_escape_string(htmlspecialchars($_POST['fromdate']));
$todate = mysql_real_escape_string(htmlspecialchars($_POST['todate']));

echo "<h2>Search Result</h2>";
echo "<p><strong>From:</strong> ".$fromdate." <strong>To:</strong> ".$todate."</p>"; //test

$query = mysql_query("
SELECT *
FROM jobs j
WHERE dropdate1 BETWEEN '$fromdate' AND '$todate'
OR dropdate2 BETWEEN '$fromdate' AND '$todate'
OR dropdate3 BETWEEN '$fromdate' AND '$todate'
OR dropdate4 BETWEEN '$fromdate' AND '$todate'
OR dropdate5 BETWEEN '$fromdate' AND '$todate'
OR dropdate6 BETWEEN '$fromdate' AND '$todate';"
);

while($row = mysql_fetch_array($query)) {
$dropdate1 = $row['dropdate1'];
$dropdate2 = $row['dropdate2'];
$dropdate3 = $row['dropdate3'];
$dropdate4 = $row['dropdate4'];
$dropdate5 = $row['dropdate5'];
$dropdate6 = $row['dropdate6'];

echo "<strong>".$row['id'].":</strong> ".$row['clientname']."<br>";

if ($dropdate1 !== "") {
echo "<strong>Drop 1:</strong> ".$row['dropdate1']."<br>";
}
if ($dropdate2 !== "") {
echo "<strong>Drop 2:</strong> ".$row['dropdate2']."<br>";
}
if ($dropdate3 !== "") {
echo "<strong>Drop 3:</strong> ".$row['dropdate3']."<br>";
}
if ($dropdate4 !== "") {
echo "<strong>Drop 4:</strong> ".$row['dropdate4']."<br>";
}
if ($dropdate5 !== "") {
echo "<strong>Drop 5:</strong> ".$row['dropdate5']."<br>";
}
if ($dropdate6 !== "") {
echo "<strong>Drop 6:</strong> ".$row['dropdate6']."<br>";
}

echo "<br>";
}
}
?>

以下是一些示例结果。

Search Result

From: 04-01-2016 To: 04-08-2016

1: blah1
Drop 1: 04-03-2015

2: blah2
Drop 1: 03-17-2015
Drop 2: 03-23-2015
Drop 3: 03-30-2015
Drop 4: 04-06-2015

3: blah3
Drop 1: 04-05-2015
Drop 2: 04-05-2015

4: blah4
Drop 1: 03-31-2015
Drop 2: 04-07-2015

5: blah5
Drop 1: 04-05-2015

因此,我的 if 语句适合不显示空日期,但现在我想更进一步,不显示不在两个搜索日期之间的日期。

这就是我希望的搜索结果:

Search Result

From: 04-01-2016 To: 04-08-2016

1: blah1
Drop 1: 04-03-2015

2: blah2
Drop 4: 04-06-2015

3: blah3
Drop 1: 04-05-2015
Drop 2: 04-05-2015

4: blah4
Drop 2: 04-07-2015

5: blah5
Drop 1: 04-05-2015

最佳答案

试试这个:

<?php
if (isset($_POST['search'])) {
$fromdate = mysql_real_escape_string(htmlspecialchars($_POST['fromdate']));
$todate = mysql_real_escape_string(htmlspecialchars($_POST['todate']));

echo "<h2>Search Result</h2>";
echo "<p><strong>From:</strong> ".$fromdate." <strong>To:</strong> ".$todate."</p>"; //test

$query = mysql_query("SELECT * FROM jobs WHERE dropdate1 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate2 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate3 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate4 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate5 BETWEEN '".$fromdate."' AND '".$todate."' OR dropdate6 BETWEEN '".$fromdate."' AND '".$todate."'");
$fromDate1 = new DateTime($_POST['fromdate']);
$toDate1 = new DateTime($_POST['todate']);
while($row = mysql_fetch_array($query)) {
$dropdate1 = new DateTime($row['dropdate1']);
$dropdate2 = new DateTime($row['dropdate2']);
$dropdate3 = new DateTime($row['dropdate3']);
$dropdate4 = new DateTime($row['dropdate4']);
$dropdate5 = new DateTime($row['dropdate5']);
$dropdate6 = new DateTime($row['dropdate6']);

echo "<strong>".$row['id'].":</strong> ".$row['clientname']."<br>";

if ($dropdate1 !== "" && $dropdate1 >= $fromDate1 && $dropDate1 <= $toDate1) {
echo "<strong>Drop 1:</strong> ".$row['dropdate1']."<br>";
}
if ($dropdate2 !== "" && $dropdate2 >= $fromDate1 && $dropDate1 <= $toDate1) {
echo "<strong>Drop 2:</strong> ".$row['dropdate2']."<br>";
}
if ($dropdate3 !== "" && $dropdate3 >= $fromDate1 && $dropDate1 <= $toDate1) {
echo "<strong>Drop 3:</strong> ".$row['dropdate3']."<br>";
}
if ($dropdate4 !== "" && $dropdate4 >= $fromDate1 && $dropDate1 <= $toDate1) {
echo "<strong>Drop 4:</strong> ".$row['dropdate4']."<br>";
}
if ($dropdate5 !== "" && $dropdate5 >= $fromDate1 && $dropDate1 <= $toDate1) {
echo "<strong>Drop 5:</strong> ".$row['dropdate5']."<br>";
}
if ($dropdate6 !== "" && $dropdate6 >= $fromDate1 && $dropDate1 <= $toDate1) {
echo "<strong>Drop 6:</strong> ".$row['dropdate6']."<br>";
}

echo "<br>";
}
}

这应该有效。

关于php - 如果某些列数据与原始查询不准确,如何阻止其显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36363400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com