<?php
function findIt($jarr, $fkey, $val, $rkey) {
    foreach ($jarr as $dic) {
        if (strcmp($dic[$fkey],$val)==0)
            return $dic[$rkey];
    }
    return "";
}

$userid = $_POST["userID"];

$string = file_get_contents("JSON/WMU.json");
$j_wmu = json_decode($string, true);
$string = file_get_contents("JSON/GameTypes.json");
$j_gametypes = json_decode($string, true);
$string = file_get_contents("JSON/GameSexTypes.json");
$j_gamesextypes = json_decode($string, true);
$string = file_get_contents("JSON/Taken.json");
$j_taken = json_decode($string, true);
$string = file_get_contents("JSON/Counties.json");
$j_counties = json_decode($string, true);
$string = file_get_contents("JSON/Towns.json");
$j_towns = json_decode($string, true);


$servername = "localhost";
$username = "biggame";
$password = "MaRtIn";
$dbname = "BigGame";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$query = "select * from userkill where user_id='" . $userid . "'";;
//echo '<br>' . $query . '<br>';
$result = $conn->query($query);

$firsttime=true;
echo '[';
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        //user kills response = [{"user_id":22,"kill_id":"79","game_id":"2","sex_id":"3","kill_dt":"01/10/2017 08:06 AM","wmu_id":"3","county_id":"13","township_id":"295","taken_id":"2","beardlength":"","beardlength_inches":"","spurlength":"","spurlength_inches":"","tag_number":"","dmapunit":"","coupon":"","points_left":"0","points_right":"0","no_harvest":"No","youth_taken":"No","game_desc":"Turkey","sex_desc":"Female","taken_desc":"Shotgun","county_desc":"Potter","wmu_desc":"3A","township_desc":"Eulalia"}]

        if ($firsttime) {
            $firsttime=false;
        }
        else {
            echo ",";
        }
        echo "{";
        echo "  \"user_id\":\"" . $row['user_id']  . '"';
        echo ", \"kill_id\":\"" . $row['kill_id'] . '"';
        echo ", \"game_id\":\"" . $row['game_id']  . '"';
        echo ", \"sex_id\":\"" . $row['sex_id']  . '"';
        $ge = $row['kill_date_time'];
        //echo '<br>' . $ge . '<br>';
        echo ", \"kill_dt\":\"" . date_format(date_create($row['kill_date_time']), "m/d/Y g:i A")  . '"';
        echo ", \"wmu_id\":\"" . $row['wmu_id']  . '"';
        echo ", \"county_id\":\"" . $row['county_id']  . '"';
        echo ", \"township_id\":\"" . $row['township_id']  . '"';
        echo ", \"taken_id\":\"" . $row['taken_id']  . '"';
        echo ", \"beardlength\":\"" . $row['beard_length']  . '"';
        echo ", \"beardlength_inches\":\"" . $row['beardlength_inches']  . '"';
        echo ", \"spurlength\":\"" . $row['spur_length']  . '"';
        echo ", \"spurlength_inches\":\"" . $row['spurlength_inches'] . '"';
        echo ", \"tag_number\":\"" . $row['tag_number']  . '"';
        echo ", \"dmapunit\":\"" . $row['dmapunit']  . '"';
        echo ", \"coupon\":\"" . $row['coupon']  . '"';
        echo ", \"points_right\":\"" . $row['points_right']  . '"';
        echo ", \"points_left\":\"" . $row['points_left']  . '"';
        $v = "No";
        if ($row['no_harvest']=='1')
            $v='Yes';
        echo ", \"no_harvest\":\"" . $v  . '"';
        $v = "No";
        if ($row['youth_taken']=='1')
            $v='Yes';
        echo ", \"youth_taken\":\"" . $v  . '"';
      
        echo ", \"game_desc\":\"" . findIt($j_gametypes, 'game_id', $row['game_id'], 'description')  . '"';
        echo ", \"sex_desc\":\"" . findIt($j_gamesextypes, 'sex_id', $row['sex_id'], 'description')  . '"';
        echo ", \"taken_desc\":\"" . findIt($j_taken, 'taken_id', $row['taken_id'], 'description')  . '"';
        echo ", \"county_desc\":\"" . findIt($j_counties, 'county_id', $row['county_id'], 'description')  . '"';
        echo ", \"wmu_desc\":\"" . findIt($j_wmu, 'wmu_id', $row['wmu_id'], 'description')  . '"';
        echo ", \"township_desc\":\"" . findIt($j_towns, 'town_id', $row['town_id'], 'description')  . '"';


        //user kills response = [{"user_id":22,"kill_id":"79","game_id":"2","sex_id":"3","kill_dt":"01/10/2017 08:06 AM","wmu_id":"3","county_id":"13","township_id":"295","taken_id":"2","beardlength":"","beardlength_inches":"","spurlength":"","spurlength_inches":"","tag_number":"","dmapunit":"","coupon":"","points_left":"0","points_right":"0","no_harvest":"No","youth_taken":"No","game_desc":"Turkey","sex_desc":"Female","taken_desc":"Shotgun","county_desc":"Potter","wmu_desc":"3A","township_desc":"Eulalia"}]
  echo "}"; 
        }
}
echo ']';
$conn->close();

//echo "DONE";
?>
