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

后端 未结 5 1238
北海茫月
北海茫月 2021-01-23 08:12

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 cl

相关标签:
5条回答
  • 2021-01-23 08:25

    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];
    }
      
    
    
    ?> 
    
    0 讨论(0)
  • 2021-01-23 08:32

    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.

    0 讨论(0)
  • 2021-01-23 08:33

    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. If I got that wrong, you can correct it in the function $eview.

    Here is code that yields a result of 4.2 with the data you posted.

    $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";
    
    0 讨论(0)
  • 2021-01-23 08:37

    You want to skip lines where there are not exactly 3 elements in the e1 field. After the line: $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().

    Without knowing the entire content of your table, it is hard to see where the undefined index error comes from.

    0 讨论(0)
  • 2021-01-23 08:43

    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

    0 讨论(0)
提交回复
热议问题