![](/img/trans.png)
[英]How to filter data from multiple table mapping date range with current date using Mysql and PHP
[英]How can i get data from using date range for querying multiple details from a table?
使用表格下方的代碼,我已經能夠從該表格中獲取多個詳細信息...
我知道該表看起來很奇怪,但這是來自客戶端的可用數據表。
id | class | gender | e1 | e2 | d1 | d2
----------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | class1 | male | ,first, first, first | ,first, first, first | , 2019-12-24,2019-12-24,2019-12-24 | ,2019-12-04,2019-11-21,2019-11-20 |
2 | class2 | female | ,second, second, second | ,second, second, second | ,2019-12-04,2019-11-21,2019-11-20 | , 2019-12-20,2019-12-20,2019-12-20
3 | class3 | male | ,first, first, first | ,third, third, third | ,2019-12-06,2019-12-13,2019-12-19 | ,2019-12-06,2019-12-13,2019-12-19
4 | class4 | male | ,third, third, third | ,third, third, third | ,2019-12-20,2019-12-20,2019-12-20 | ,2019-12-24,2019-12-24,2019-12-24
5 | class5 | female | ,second, second | ,third, third, third | ,2019-12-24,2019-12-24,2019-12-24 | ,2019-12-24,2019-12-24,2019-12-24
以下是我用來獲取詳細信息的代碼:
$datfrm ="00/00/0001";
$datto ="31/12/2099";
$gender="male";
$place_map = ['first' => 1, 'second' => 0.8, 'third' => 0.4];
//e1view
$query = "SELECT e1 FROM eyfstb WHERE gender = '$gender'";
// perform the query ...
if ($result = $db->query($query)) {
$finalScore1 = 0;
$scores="";
$last_score="";
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$efind = $row['e1'];
if (substr_count($efind, ",") >'2'){
$scores = explode(',', $row['e1']);
$last_score = trim($scores[count($scores)-1]);
$finalScore1 += $place_map[$last_score];
}
}
}
//e2view
$query = "SELECT e2 FROM eyfstb WHERE gender = '$gender'";
// perform the query ...
if ($result = $db->query($query)) {
$finalScore2 = 0;
$scores="";
$last_score="";
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$efind = $row['e2'];
if (substr_count($efind, ",") >'2'){
$scores = explode(',', $row['e2']);
$last_score = trim($scores[count($scores)-1]);
$finalScore2 += $place_map[$last_score];
}
}
}
現在我要做的是在下面獲取一個日期范圍,以幫助僅顯示來自$datfrm
和$datto
的記錄
實際上我在下面嘗試了這個,但我收到了錯誤:
//Filter out dates between start and end date
$d1result = array_filter($d1view, function($data_item) use($datfrm,$datto) {
return $data_item >= $datfrm && $data_item <= $datto;
});
$e1result = array_intersect_key($e1view, $d1result);
但是現在使用我在下面嘗試的代碼,我遇到了更多錯誤:
//e1view
$query = "SELECT e1,d1 FROM eyfstb WHERE gender = '$gender'";
// perform the query ...
if ($result = $db->query($query)) {
$finalScore1 = 0;
$scores="";
$last_score="";
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$efind = $row['e1'];
$dview = $row["d1"];
if (substr_count($efind, ",") >'2'){
$dview = ltrim($dview,',');
$dview = explode(',', $dview);
$efind = ltrim($efind,',');
$efind = explode(',', $efind);
$dresult = array_filter($dview, function($data_item) use($datfrm,$datto) {
return $data_item >= $datfrm && $data_item <= $datto;
});
$eresult = array_intersect_key($efind, $dresult);
$efind = implode(',', $eresult);
$efind = ','.$efind;
$scores = explode(',', $efind);
$last_score = trim($scores[count($scores)-1]);
$finalScore1 += $place_map[$last_score];
}
}
}
//e2view
$query = "SELECT e2,d2 FROM eyfstb WHERE gender = '$gender'";
// perform the query ...
if ($result = $db->query($query)) {
$finalScore2 = 0;
$scores="";
$last_score="";
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$efind = $row['e2'];
$dview = $row["d2"];
if (substr_count($efind, ",") >'2'){
$dview = ltrim($dview,',');
$dview = explode(',', $dview);
$efind = ltrim($efind,',');
$efind = explode(',', $efind);
$dresult = array_filter($dview, function($data_item) use($datfrm,$datto) {
return $data_item >= $datfrm && $data_item <= $datto;
});
$eresult = array_intersect_key($efind, $dresult);
$efind = implode(',', $eresult);
$efind = ','.$efind;
$scores = explode(',', $efind);
$last_score = trim($scores[count($scores)-1]);
$finalScore2 += $place_map[$last_score];
}
}
}
echo ($finalscore +$finalscore2);
以下是顯示的錯誤加上一個我不確定它是否正確的答案:
Notice: Undefined index: in C:\xampp\htdocs\SAP\genderresult\gender1.php on line 69
(line 69 is `$finalScore1 += $place_map[$last_score];`)
Notice: Undefined index: in C:\xampp\htdocs\SAP\genderresult\gender1.php on line 69
Notice: Undefined index: in C:\xampp\htdocs\SAP\genderresult\gender1.php on line 106
(line 69 is `$finalScore2 += $place_map[$last_score];`)
39.4
如果 e1 或 e2 字段中有 2 個文件,我想要的結果是僅獲取 e1 和 e2 中的值,這些值在 d1 和 d2 的日期范圍內,我不希望它顯示或 map 任何東西。
您想跳過e1
字段中不完全是 3 個元素的行。 行后: $efind = explode(',', $efind);
,添加這個:
if (count($efind) !== 3) {
continue;
}
在這兩個地方添加這個。
代碼中的另一個問題是,當您嘗試比較日期時,您將字符串與<=
和>=
進行比較。 您應該改用strcmp() 。
在不知道表的全部內容的情況下,很難看出未定義索引錯誤的來源。
我希望這個能幫上忙
<?php
$Date1 = '01-10-2010';
$Date2 = '07-12-2010';
$array = array();
$range = 0;
$Variable1 = strtotime($Date1);
$Variable2 = strtotime($Date2);
for ($currentDate = $Variable1; $currentDate <= $Variable2;
$currentDate += (86400)) {
$Store = date('Y-m-d', $currentDate);
$array[] = $Store;
$range++;
}
for ($i=0; $i < $range; $i++) {
echo $array[$i];
}
?>
我在理解你想要從 e1 和 e2 中得到什么時遇到了一些麻煩——假設它只是該字段中的最后一個值。 如果我弄錯了,您可以在 function $eview 中更正它。
這是使用您發布的數據產生 4.2 結果的代碼。
$datfrm = ""; // or something like '2010-01-01'
$datto = '2200'; // or something like '2015-12-01'
$gender="male";
$place_map = ['first' => 1, 'second' => 0.8, 'third' => 0.4];
// My understanding of the code is to get the weighted value of the
// last word in the column - if that's wrong, you can change this
// I am assuming you want the date that corresponded to the item you used
// for the final score, so if wrong, you need to change this.
// Also assume there will be the same number of dates as 'e's
//
// return false if bad value, or 0 if not in date range
$eview = function ($row, $ecol, $dcol) use ($place_map, $datfrm, $datto) {
$parts = array_filter(array_map('trim', explode(",", $row[$ecol])));
$dates = array_filter(array_map('trim', explode(",", $row[$dcol])));
if (!$parts || count($parts) != count($dates)) {
echo "Expected same number of parts and dates!\n";
return false;
}
$date = array_pop($dates); // Getting last date...
return ($date >= $datfrm && $date < $datto) ?
($place_map[array_pop($parts)] ?? false) : 0;
};
$finalScore1 = $finalScore2 = 0;
// perform the query ...
$query = "SELECT e1, e2, d1, d2 FROM eyfstb WHERE gender = '{$gender}'";
if ($result = $db->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$finalScore1 += $eview($row, 'e1', 'd1') ?: 0;
$finalScore2 += $eview($row, 'e2', 'd2') ?: 0;
}
}
echo ($finalScore1+$finalScore2) . "\n";
你在做
if ($result = $db->query($query)) {
$finalScore1 = 0;
......
}
if ($result = $db->query($query)) {
$finalScore2 = 0;
.......
}
echo ($finalscore +$finalscore2);
試着做:
$finalScore1 = 0;
if ($result = $db->query($query)) {
......
}
$finalScore2 = 0;
if ($result = $db->query($query)) {
.......
}
echo ($finalscore +$finalscore2);
您還沒有定義,首先定義:
$place_map = ['first' => 1, 'second' => 0.8, 'third' => 0.4];
希望這些解決您的錯誤,rest 結果根據數據和代碼
首先,將您的日期從
$datfrm ="00/00/0001";
$datto ="31/12/2099";
至
$datfrm ="00-00-0001";
$datto ="31-12-2099";
然后加
if (empty($last_score)) {
$last_score=0;
}else{
}
之間
$last_score = trim($scores[count($scores)-1]);
和
$finalScore15 += $place_map[$last_score];
那應該可以完全解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.