Jump to content

Code to keep track of my weight on certain workouts


rec0il

Recommended Posts

Hello,
I'm trying to create a code that can help me and my friend keep track on how much weight we have used in past workouts and update it regularly while at the gym.
To visualise what I'm trying to create, here is a link to the design. Click me
 
I'm not very good at php coder nor SQL, so it's pretty difficult for me, but I've tried to create the code for the whole design but I'm stuck and can't figure out how to create the rest, I was hoping someone could help me.
 

What I'm trying to achieve;

To have the name of whoever using it at the top.

To have the dropdown menu automaticly generated from my database

To have it show the current weight on the selected exercise

Be able to update the weight fast and easy

 
What I have so far is
<?php
include ("connect.php");

$sql = "SELECT * FROM `workout` WHERE exercise='dumbell bench press'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> Ali: ". $row["Ali"] . "<br> Emil: ". $row["Emil"] . "<br><br>" ;
     }
} else {
     echo "0 results";
}

function updater($value,$id){
include ("connect.php");

       $value=mysqli_real_escape_string($conn,$value);
       $id=mysqli_real_escape_string($conn,$id);

       $sql = "UPDATE `workout` SET Ali='{$value}' WHERE exercise='{$id}'";
       if ($conn->query($sql) === TRUE) {
           header("Refresh:0");
       } else {
           echo "Error updating record: " . $conn->error;
       }
       $conn->close();
   }   
   
   if (isset($_POST['Ali'])) updater($_POST['Ali'],$_POST['id']) 
?>  


<html>
<body>


<?php
include ("connect.php");

$sql = "SELECT exercise FROM `workout`";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
	$select= '<select name="select">';
     // output data of each row
     while($row = $result->fetch_assoc()) {
$select.='<option value="'.$row['exercise'].'">'.$row['exercise'].'</option>';
     }
} else {
     echo "rest in peace";
}

$select.='</select>';
echo $select;


?>  

<br><br>


<form action="" method="post" style="height:50px;width:50px;">
    <input type="hidden" name="id" value="dumbell bench press" />           
    <input type="text" name="Ali" /><br><br>
    <input type="submit" /><br/>
</form>

</body>
</html>

Where I've created my database like so:

http://i1227.photobucket.com/albums/ee433/rec0ill/12312321_zpsbkamhfry.png

 

 

post-164572-0-09866100-1486778092_thumb.png

post-164572-0-92599500-1486778095_thumb.png

Edited by rec0il
Link to comment
Share on other sites

to create a multi-user system, you must first have a login system. this will define user ids that you would store in any related data table to identify who the data is for. you would NOT have columns in your data table for each user. you would instead store a row in your data table for each user/exercise combination. if your page(s) can only be accessed when there is a logged in user, you would detect if there is a logged user first and only display information and process forms if there is. if there is not a logged in user, you would display a login form and enable the processing of the login form.

 

next, you would have a table where the exercises are defined, that has an id (auto-increment) column and an exercise name column. you would query this table to get a list of the exercises for the select/option menu. you would store the exercise id in any table holding data related to the exercises.

 

your data table would then have columns for the user id, the exercise id, and the weight. you would insert a new row if the data for a user/exercise doesn't exist and update the row if it does. there's a single query that will do this, an INSERT ... ON DUPLICATE KEY UPDATE ... query. you would define the user id and exercise id columns as a composite index to allow this query to work.

 

lastly, your code needs to be organized as follows - 

  1. define or require any configuration/settings and functions that your main code needs.
  2. start the session.
  3. create the database connection. you should only do this once.
  4. process any post method form data. there would be a section of code for each form the page must process.
  5. get/produce any data needed to display the page. the data should be stored in php variables. there would be a section of code for each different thing that can be displayed on the page.
  6. output the html document.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.