iT邦幫忙

2025 iThome 鐵人賽

0
自我挑戰組

飲食營養追蹤 + 健康分析 Web 系統系列 第 17

查詢日期範圍紀錄與營養功能

  • 分享至 

  • xImage
  •  

目的

提供查詢一個日期範圍 , 使用者的食物總熱量和蛋白質...總含量。

1.主選單(Main)
新增8.查詢日期範圍記錄與營養功能。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845wvUz0L0WCs.jpg


2.Main
加開始日期和結束日期 , 並進行錯誤處理。

dao.getLogsByDateRange() : 呼叫FoogLogDAO的getLogsByDateRange方法(後面會創), 把上面寫的參數丟入。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845qJoMQV4vCT.jpg
https://ithelp.ithome.com.tw/upload/images/20251015/20178845WxW2xmn4GQ.jpg


3.FoodLogDAO
新創getLogsByDateRange方法 , 丟入userId , StarDate , endDate參數。

SELECT : 指定要查詢的欄位 , f為food表、fl為foodlog表。

JOIN food f ON fl.food_id = f.food_id : 把另一張表food連接進來(JOIN) , ON加連接條件。

WHERE fl.id = ? AND fl.date BETWEEN?AND? : 指定查詢的條件為某個使用者 , BETWEEN?AND?用於範圍查詢。

ORDER BY fl.date ASC : 根據date欄位進行排序 , ASC是升冪排序、DESC是降冪。

宣告4個變數 , 並設初始值為0。

while(rs.next()) : 有讀到資料hasDecord改為true。

最後把營養統計印出 , 並處理例外。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845zAoHInKsRC.jpg
https://ithelp.ithome.com.tw/upload/images/20251015/20178845auh1uBgumb.jpg


4.結果
https://ithelp.ithome.com.tw/upload/images/20251015/20178845eUXdi3AuZF.jpg


上一篇
查詢每日營養統計功能
下一篇
使用者登入和註冊
系列文
飲食營養追蹤 + 健康分析 Web 系統21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言