简体   繁体   English

如何使用日期范围从表中查询多个详细信息来获取数据?

[英]How can i get data from using date range for querying multiple details from a table?

I've been able to get multiple details from this table, using the codes beneath the table...使用表格下方的代码,我已经能够从该表格中获取多个详细信息...

I know the table looks odd, but that's the available data table from the client.我知道该表看起来很奇怪,但这是来自客户端的可用数据表。

    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

Below is the code i used to get the details:以下是我用来获取详细信息的代码:

$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];
        }
    }
}

now what i'm trying to do is get a date range below to help display only the records from $datfrm and $datto现在我要做的是在下面获取一个日期范围,以帮助仅显示来自$datfrm$datto的记录

actually i tried this below, but i'm getting error:实际上我在下面尝试了这个,但我收到了错误:

//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);

but now with the code i tried beneath, I'm getting more errors:但是现在使用我在下面尝试的代码,我遇到了更多错误:

//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);

Below are the errors displayed plus one answer i'm not sure it's even correct:以下是显示的错误加上一个我不确定它是否正确的答案:

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

And my desired result is to get only values in e1 and e2 that are in the date range from d1 and d2 if there are 2 files in e1 or e2 field, i don't want it to display or map anything.如果 e1 或 e2 字段中有 2 个文件,我想要的结果是仅获取 e1 和 e2 中的值,这些值在 d1 和 d2 的日期范围内,我不希望它显示或 map 任何东西。

You want to skip lines where there are not exactly 3 elements in the e1 field.您想跳过e1字段中不完全是 3 个元素的行。 After the line: $efind = explode(',', $efind);行后: $efind = explode(',', $efind); , add this: ,添加这个:

if (count($efind) !== 3) {
    continue;
}

Add this in both places.在这两个地方添加这个。


Another problem in the code is that when you try to compare dates, you compare strings with <= and >= .代码中的另一个问题是,当您尝试比较日期时,您将字符串与<=>=进行比较。 You should instead use strcmp() .您应该改用strcmp()

Without knowing the entire content of your table, it is hard to see where the undefined index error comes from.在不知道表的全部内容的情况下,很难看出未定义索引错误的来源。

I hope this will help我希望这个能帮上忙

    <?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];
}
  


?> 

I was having a bit of trouble understanding exactly what you wanted out of e1 and e2 - assume its just the last value in the field.我在理解你想要从 e1 和 e2 中得到什么时遇到了一些麻烦——假设它只是该字段中的最后一个值。 If I got that wrong, you can correct it in the function $eview.如果我弄错了,您可以在 function $eview 中更正它。

Here is code that yields a result of 4.2 with the data you posted.这是使用您发布的数据产生 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";

You are doing你在做

if ($result = $db->query($query)) {
    $finalScore1 = 0;
   ......
}

if ($result = $db->query($query)) {
   $finalScore2 = 0;
   .......
}


echo ($finalscore +$finalscore2);

Try to do:试着做:

$finalScore1 = 0;
if ($result = $db->query($query)) {
   ......
}

$finalScore2 = 0;
if ($result = $db->query($query)) {
   .......
}


echo ($finalscore +$finalscore2);

You have also not defined, define on the top of all:您还没有定义,首先定义:

$place_map = ['first' => 1, 'second' => 0.8, 'third' => 0.4];

Hope these resolve you error, rest result as per data and code希望这些解决您的错误,rest 结果根据数据和代码

First, change your dates from首先,将您的日期从

$datfrm ="00/00/0001";
$datto ="31/12/2099";

to

$datfrm ="00-00-0001";
$datto ="31-12-2099";

then add然后加

    if (empty($last_score)) {
      $last_score=0;
    }else{

    }
    

between之间

$last_score = trim($scores[count($scores)-1]);

and

$finalScore15 += $place_map[$last_score];

that should solve your problem completely.那应该可以完全解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM