Track Your Diet & Exercise With NAHA Consumption Tool

by Alex Johnson 54 views

Welcome to the NAHA Consumption Tracker, a streamlined tool designed to help you monitor your daily food intake and physical activities. This tool allows you to quickly log what you eat and how you exercise, making it easier to manage your caloric balance and stay on top of your health goals. Let's dive into how you can make the most of this feature.

Getting Started with Your Consumption Log

First things first, ensure you're logged in. If you're not, the system will redirect you to the login page. Once you're in, you'll find a user-friendly interface ready to accept your daily consumption and activity data. This user-friendly interface ensures that tracking your health metrics is as easy and intuitive as possible.

Navigating the Interface

The interface is divided into two main sections: Product and Sport. Each section is designed to capture different aspects of your daily routine, allowing for a comprehensive overview of your habits. The Product section focuses on what you eat, while the Sport section tracks your physical activities. Using these two sections together provides a complete picture of your energy balance.

Understanding the Product Section

In the Product section, you'll find fields to log the food items you've consumed. Start by selecting the food item from the dropdown menu. The menu is populated via AJAX with data fetched from the produits table in the database, listing each nom_produit alphabetically. Next, input the quantity consumed in grams. As you enter the quantity, the system automatically calculates the approximate kilocalories (kcal) based on the energie_kcal value associated with the selected product. This immediate feedback helps you understand the caloric impact of your food choices in real-time.

Utilizing the Sport Section

The Sport section operates similarly, allowing you to log your physical activities. Choose your activity from the dropdown menu, which is also populated via AJAX from the sports table, displaying each nom_sport in alphabetical order. Then, enter the duration of the activity in minutes. The system calculates the approximate kilocalories (kcal) burned based on the MET (Metabolic Equivalent of Task) and kcal_h_70kg values associated with the selected sport. This feature gives you an estimate of how many calories you've burned during your workout, helping you balance your energy intake and expenditure.

Diving Deeper into the Code

Let's explore the underlying code that powers this dynamic tool. The consommation.php script manages user sessions, database connections, and AJAX requests to provide a seamless user experience.

Session Management and Authentication

The script begins by starting a PHP session to manage user login status. If a user is not logged in (i.e., $_SESSION['utilisateur'] is not set), the script redirects them to the seconnecter.php page, ensuring that only authenticated users can access the consumption tracker. This security measure protects user data and ensures that personal information is only accessible to the user.

Database Connection

The script establishes a connection to the database using the bdd.php file, which contains the necessary credentials and connection logic. The getBD() function returns a PDO (PHP Data Objects) instance, allowing the script to execute SQL queries and interact with the database. This connection is crucial for retrieving product and sport data, as well as logging user consumption and activity.

AJAX Handling

The script uses AJAX to handle asynchronous requests from the client-side JavaScript. When the ajax parameter is set in the GET request, the script responds with JSON-encoded data. This allows the webpage to update dynamically without requiring a full page reload.

Fetching Products

When $_GET['ajax'] is set to produits, the script queries the produits table to retrieve the id_produit, nom_produit, and energie_kcal for all products. The results are ordered alphabetically by nom_produit and returned as a JSON-encoded array. This data is then used to populate the product dropdown menu on the webpage.

Fetching Sports

Similarly, when $_GET['ajax'] is set to sports, the script queries the sports table to retrieve the id_sport, nom_sport, MET, and kcal_h_70kg for all sports. The results are ordered alphabetically by nom_sport and returned as a JSON-encoded array. This data is used to populate the sport dropdown menu on the webpage.

Logging Consumption and Activity

When $_GET['ajax'] is set to log, the script handles the logging of user consumption and activity data. The script retrieves the id_produit, quantite, id_sport, and duree from the POST request. It then uses a transaction to ensure that both the consumption and activity data are logged correctly or not at all.

If idProduit and quantite are provided, the script inserts a new record into the consommation table with the user's ID, product ID, quantity, and current timestamp. If idSport and duree are provided, the script inserts a new record into the activite table with the user's ID, sport ID, current timestamp, and duration in minutes.

The script uses prepared statements to prevent SQL injection attacks and ensure data integrity. If any error occurs during the transaction, the script rolls back the changes and returns an error message as a JSON-encoded response.

HTML Structure and CSS Styling

The HTML structure is straightforward, providing a clean and responsive layout. The page includes a header with navigation links, a main content area with the consumption tracker, and a footer. The CSS styling is handled by accueil-style.css and consommation-style.css, which provide a visually appealing and user-friendly interface.

JavaScript Interaction

The consommation-script.js file handles the client-side logic, including fetching data from the server using AJAX, updating the UI based on user input, and submitting the consumption and activity data to the server. This script enhances the user experience by providing dynamic updates and real-time feedback.

How to Effectively Use the Tracker

  1. Be Consistent: Make it a daily habit to log your food intake and activities. The more consistent you are, the better you can understand your patterns and make informed decisions.
  2. Be Accurate: Try to be as precise as possible when entering quantities and durations. Even small discrepancies can add up over time and affect your overall assessment.
  3. Review Regularly: Take time each week to review your logs. Look for trends, identify areas where you can improve, and adjust your habits accordingly.
  4. Use the Feedback: Pay attention to the kcal calculations provided by the tool. This real-time feedback can help you make better choices throughout the day.

Benefits of Tracking Your Consumption

Tracking your consumption and activity offers several benefits:

  • Increased Awareness: You become more aware of what you're eating and how much you're exercising.
  • Improved Decision-Making: You can make more informed choices about your diet and exercise routine.
  • Better Goal Setting: You can set realistic goals based on your actual consumption and activity levels.
  • Enhanced Motivation: Seeing your progress can motivate you to stick to your health goals.

Conclusion

The NAHA Consumption Tracker is a valuable tool for anyone looking to take control of their health and well-being. By consistently logging your food intake and physical activities, you can gain valuable insights into your habits and make informed decisions to achieve your health goals. Start tracking today and take the first step towards a healthier you!

For more information on healthy eating habits and exercise guidelines, visit the National Institutes of Health.