function add_member($activityId) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userId = get_userId(); $sql = <<<EOF SELECT * FROM ActivityMember WHERE userId={$userId} and activityId={$activityId}; EOF; $ret = $db->query($sql); if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $db->close(); header("Location: http://www.kmoving.com/user/groups/activity.php?msg=memberExist"); } else { $data = $_COOKIE['date']; $sql = <<<EOF INSERT INTO ActivityMember (userId, activityId, createAt) VALUES ('{$userId}', '{$activityId}', '{$data}'); EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { $db->close(); header("Location: http://www.kmoving.com/user/groups/activity.php"); } } }
function update_mood($mood_image) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userName = $_COOKIE['username']; $date = $_COOKIE['date']; $sql = <<<EOF SELECT * FROM User,Mood WHERE User.name='{$userName}' and User.id=Mood.userId and Mood.createAt='{$date}'; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $userId = $row['userId']; $sql = <<<EOF UPDATE Mood SET mood='{$mood_image}' where userId={$userId} and createAt={$date}; EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } else { $userId = get_userId($userName); $sql = <<<EOF INSERT INTO Mood (userId, mood, createAt) VALUES ('{$userId}', '{$mood_image}', '{$date}'); EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } } $db->close(); }
function show_activity($last, $amount) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userName = $_COOKIE['username']; $userId = get_userId($db, $userName); $sql = <<<EOF SELECT * FROM Advice WHERE toUserId={$userId} ORDER BY id DESC LIMIT '{$last}','{$amount}'; EOF; $ret = $db->query($sql); while ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $authorId = $row['authorId']; $authorName = get_authorName($db, $authorId); if ($authorName != null) { $sayList[] = array('title' => "标题:" . $row['title'], 'content' => "建议内容: " . $row['content'], 'authorName' => "提出者: " . $authorName); } } echo json_encode($sayList); $db->close(); }
function update_sleeps($asleep_time, $awakenings, $awake, $light, $deep, $duration) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userName = $_COOKIE['username']; $date = $_COOKIE['date']; $sql = <<<EOF SELECT * FROM User,Sleeps WHERE User.name='{$userName}' and User.id=Sleeps.userId and Sleeps.createAt='{$date}'; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $userId = $row['userId']; $sql = <<<EOF UPDATE Sleeps SET asleep='{$asleep_time}',wake_up_time='{$awakenings}', sober='{$awake}',light='{$light}',deep='{$deep}',total='{$duration}' where userId={$userId} and createAt={$date}; EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } else { $userId = get_userId($userName); $sql = <<<EOF INSERT INTO Sleeps (userId, asleep, wake_up_time, sober, light, deep, total, createAt) VALUES ('{$userId}', '{$asleep_time}', '{$awakenings}', '{$awake}', '{$light}', '{$deep}', '{$duration}', '{$date}'); EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } } $db->close(); }
function update_meals($title, $calories, $fat, $protein, $calcium, $carbohydrate, $sugar, $cholesterol, $vitamin_c, $vitamin_a) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userName = $_COOKIE['username']; $date = $_COOKIE['date']; $sql = <<<EOF SELECT * FROM User,Meals WHERE User.name='{$userName}' and User.id=Meals.userId and Meals.createAt='{$date}'; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $userId = $row['userId']; $sql = <<<EOF UPDATE Meals SET title='{$title}',calories='{$calories}',fat='{$fat}', protein='{$protein}',calcium='{$calcium}',carbohydrate='{$carbohydrate}', vitamin_c='{$vitamin_c}',vitamin_a='{$vitamin_a}' where userId={$userId} and createAt={$date}; EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } else { $userId = get_userId($userName); $sql = <<<EOF INSERT INTO Meals (userId, title, calories, fat, protein, calcium, carbohydrate, sugar, cholesterol, vitamin_c, vitamin_a, createAt) VALUES ('{$userId}', '{$title}', '{$calories}', '{$fat}', '{$protein}', '{$calcium}', '{$carbohydrate}', '{$sugar}', '{$cholesterol}', '{$vitamin_c}', '{$vitamin_a}', '{$date}'); EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } } $db->close(); }
function update_workouts($title, $steps, $km, $calories, $time) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userName = $_COOKIE['username']; $date = $_COOKIE['date']; $sql = <<<EOF SELECT * FROM User,Workouts WHERE User.name='{$userName}' and User.id=Workouts.userId and Workouts.createAt='{$date}'; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $userId = $row['userId']; $sql = <<<EOF UPDATE Workouts SET title='{$title}',steps='{$steps}', distance='{$km}',calories='{$calories}',time='{$time}' where userId={$userId} and createAt={$date}; EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } else { $userId = get_userId($userName); $sql = <<<EOF INSERT INTO Workouts (userId, title, steps, distance, calories, time, createAt) VALUES ('{$userId}', '{$title}', '{$steps}', '{$km}', '{$calories}', '{$time}', '{$date}'); EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } } $db->close(); }
function update_moves($active_time, $inactive_time, $calories, $wo_calories, $bg_calories, $bmr_day, $steps, $km) { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userName = $_COOKIE['username']; $date = $_COOKIE['date']; $sql = <<<EOF SELECT * FROM User,Moves WHERE User.name='{$userName}' and User.id=Moves.userId and Moves.createAt='{$date}'; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $userId = $row['userId']; $sql = <<<EOF UPDATE Moves SET active='{$active_time}',free='{$inactive_time}', total_calories='{$calories}',workouts_calories='{$wo_calories}', static_calories='{$bg_calories}',daixie='{$bmr_day}',steps='{$steps}',distance='{$km}' where userId={$userId} and createAt={$date}; EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } else { $userId = get_userId($userName); $sql = <<<EOF INSERT INTO Moves (userId, active, free, total_calories, workouts_calories, static_calories, daixie, steps, distance, createAt) VALUES ('{$userId}', '{$active_time}', '{$inactive_time}', '{$calories}', '{$wo_calories}', '{$bg_calories}', '{$bmr_day}', '{$steps}', '{$km}' , '{$date}'); EOF; $ret = $db->exec($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { } } } $db->close(); }
function analysis() { $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { } $userId = get_userId($db); //moves $sql = <<<EOF SELECT sum(steps),count(createAt) FROM Moves WHERE userId={$userId}; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $total_steps = $row['sum(steps)']; $times = $row['count(createAt)']; $avg_steps = number_format($total_steps / $times, 2, '.', ''); } } //sleeps $sql = <<<EOF SELECT sum(total),count(createAt) FROM Sleeps WHERE userId={$userId}; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $total_sleeps = $row['sum(total)']; $times = $row['count(createAt)']; $avg_sleep = number_format($total_sleeps / $times, 2, '.', ''); } } //workouts $sql = <<<EOF SELECT sum(distance),count(createAt) FROM Workouts WHERE userId={$userId}; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $total_run = $row['sum(distance)']; $times = $row['count(createAt)']; $avg_run = number_format($total_run / $times, 2, '.', ''); } } //meals $total_fat = 0; $total_protein = 0; $total_calcium = 0; $total_carbohydrate = 0; $total_sugar = 0; $total_cholesterol = 0; $total_vitamin_c = 0; $total_vitamin_a = 0; $sql = <<<EOF SELECT sum(fat),sum(protein), sum(calcium),sum(carbohydrate), sum(sugar),sum(cholesterol), sum(vitamin_c),sum(vitamin_a) FROM Meals WHERE userId={$userId}; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { if ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $total_fat = $row['sum(fat)']; $total_protein = $row['sum(protein)']; $total_calcium = $row['sum(calcium)']; $total_carbohydrate = $row['sum(carbohydrate)']; $total_sugar = $row['sum(sugar)']; $total_cholesterol = $row['sum(cholesterol)']; $total_vitamin_c = $row['sum(vitamin_c)']; $total_vitamin_a = $row['sum(vitamin_a)']; } } //mood $amazing = 0; $very_good = 0; $good = 0; $usual = 0; $bad = 0; $very_bad = 0; $fuck_dog = 0; $sql = <<<EOF SELECT mood FROM Mood WHERE userId={$userId}; EOF; $ret = $db->query($sql); if (!$ret) { echo $db->lastErrorMsg(); } else { while ($row = $ret->fetchArray(SQLITE3_ASSOC)) { $mood_type = $row['mood']; if ($mood_type == "../../img/Amazing.png") { $mood_type = 1; } else { if ($mood_type == "../../img/Pumped_UP.png") { $mood_type = 2; } else { if ($mood_type == "../../img/Energized.png") { $mood_type = 3; } else { if ($mood_type == "../../img/Meh.png") { $mood_type = 4; } else { if ($mood_type == "../../img/Dragging.png") { $mood_type = 5; } else { if ($mood_type == "../../img/Exhausted.png") { $mood_type = 6; } else { if ($mood_type == "../../img/Totally_Done.png") { $mood_type = 7; } } } } } } } switch ($mood_type) { case 1: $amazing++; break; case 2: $very_good++; break; case 3: $good++; break; case 4: $usual++; break; case 5: $bad++; break; case 6: $very_bad++; break; case 7: $fuck_dog++; break; } } } echo "\n <div class='col-md-12'>\n <div id='analysis-moves'>\n <div style='margin-left: 10%'>\n <h3>你总共走了 <strong style='color: white'>{$total_steps}</strong> 步,平均每天走了 <strong style='color: white'>{$avg_steps}</strong> 步</h3>\n </div>\n </div>\n <div id='analysis-sleeps'>\n <div style='margin-left: 10%'>\n <h3>你总共睡了 <strong style='color: white'>{$total_sleeps}</strong> 小时,平均每天睡了 <strong style='color: white'>{$avg_sleep}</strong> 小时</h3>\n </div>\n </div>\n <div id='analysis-workouts'>\n <div style='margin-left: 10%'>\n <h3>你总共跑了 <strong style='color: white'>{$total_run}</strong> 公里,平均每天跑了 <strong style='color: white'>{$avg_run} </strong> 公里</h3>\n </div>\n </div>\n <div id='analysis-meals'>\n <div style='margin-left: 10%'>\n <h4>共摄入 <strong style='color: white'>{$total_fat}</strong> 克脂肪</h4>\n <h4>共摄入 <strong style='color: white'>{$total_protein}</strong> 毫克蛋白质</h4>\n <h4>共摄入 <strong style='color: white'>{$total_calcium}</strong> 毫克钙</h4>\n <h4>共摄入 <strong style='color: white'>{$total_carbohydrate}</strong> 毫克碳水化合物</h4>\n <h4>共摄入 <strong style='color: white'>{$total_sugar}</strong> 毫克糖</h4>\n <h4>共摄入 <strong style='color: white'>{$total_cholesterol}</strong> 毫克胆固醇</h4>\n <h4>共摄入 <strong style='color: white'>{$total_vitamin_c}</strong> 毫克维他命C</h4>\n <h4>共摄入 <strong style='color: white'>{$total_vitamin_a}</strong> 毫克维他命A</h4>\n\n </div>\n </div>\n <div id='analysis-mood'>\n <div style='margin-left: 10%'>\n <h3>心情状况为</h3>\n <h4>“Amazing好” <strong style='color: white'>{$amazing}</strong> 次</h4>\n <h4>“非常好” <strong style='color: white'>{$very_good}</strong> 次</h4>\n <h4>“好” <strong style='color: white'>{$good}</strong> 次</h4>\n <h4>“一般” <strong style='color: white'>{$usual}</strong> 次</h4>\n <h4>“坏” <strong style='color: white'>{$bad}</strong> 次</h4>\n <h4>“非常坏” <strong style='color: white'>{$very_bad}</strong> 次</h4>\n <h4>“槽糕透了” <strong style='color: white'>{$fuck_dog}</strong> 次</h4>\n </div>\n </div>\n </div>\n "; }