d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Couple Questions
Add Reply New Topic New Poll
Member
Posts: 10,803
Joined: Apr 5 2010
Gold: 20.00
Oct 17 2016 03:16pm
i have to write a exercise web application that requires sign in/sign up/logout information which is stored then after login the user can either edit profile to add info, view his or her profile with the recently documented exercises displayed. and a log exercise page where they type in type heartrate etc. with a home page that has link to these.

got a question how do you implement math in php theres a code that calculates calaries burned, he wants it calculated and displayed after the user hits submit on the log exercise page.

is it bad practice to copy paste because i have pretty much all the autherize parts already created on weak 7 exercises and technically all the varriables should be the same so simple copy paste would work after updating the user table
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Oct 17 2016 05:11pm
Quote (jsbb @ Oct 17 2016 05:16pm)
is it bad practice to copy paste

No. If you've already done this work there is no reason to program it again.

You should be wary of copy/pasting things from the internet and plan to use the code as part of a commercial product because of IP laws, but for the most part it is alright.

How do you calculate math?
$burned_per_hour = 300;
$hours = 2;
$calories_burned = $hours * $burned_per_hour;
Member
Posts: 10,803
Joined: Apr 5 2010
Gold: 20.00
Oct 18 2016 04:08pm


how would i put these 2 formulas in php assuming those 4 entries are user inputs. sorry he never showed us
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Oct 18 2016 04:22pm
Member
Posts: 10,803
Joined: Apr 5 2010
Gold: 20.00
Oct 19 2016 04:41pm
lets say i have 2 tables with user inputted data

ones callex exercise_log other called exercise_user. the user table contains the f and l name gender birthdate and weoght, exercise;log contains useril_id username password for login and exerciswtype dateofexercise timeinminutes and heartrate.

the main problem is the want the uaername displayed above the firstnale lastname all the user data. how do you fetch the info drom both tables

im 100% sure but ive been messing with different methods for awhile and getting annoyed, id imagine you have to write 2 querys 1 for each table 2 seperate variable. i can post my code or the specific part when i get home
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 19 2016 04:43pm
Quote (jsbb @ Oct 19 2016 06:41pm)
lets say i have 2 tables with user inputted data

ones callex exercise_log other called exercise_user. the user table contains the f and l name gender birthdate and weoght, exercise;log contains useril_id username password for login and exerciswtype dateofexercise timeinminutes and heartrate.

the main problem is the want the uaername displayed above the firstnale lastname all the user data. how do you fetch the info drom both tables

im 100% sure but ive been messing with different methods for awhile and getting annoyed, id imagine you have to write 2 querys 1 for each table 2 seperate variable. i can post my code or the specific part when i get home


http://www.w3schools.com/sql/sql_join.asp
Member
Posts: 10,803
Joined: Apr 5 2010
Gold: 20.00
Oct 19 2016 04:49pm
Quote (carteblanche @ Oct 19 2016 05:43pm)


our instructor told us we are not using joins for this project as our sql course has not gone over them yet

both the edit profile page and exercise log page both on submit upload it to the database all is workin fine until i try to view it. i am having trouble grabbing data from multiple tables do i have to indivudually select the table and assign the data to variables becauae thats the only non join method i can think of. please help lol maybe i should just use a join although he might not aprove of it.

Code
<?php
session_start();

// If the session vars aren't set, try to set them with a cookie
if (!isset($_SESSION['user_id'])) {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mismatch - View Profile</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h3>Exercise - View Profile</h3>

<?php
require_once('connectvars.php');

// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Make sure the user is logged in before going any further.
if (!isset($_SESSION['user_id'])) {
echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>';
exit();
}
else {
echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '. <a href="logout.php">Log out</a>.</p>');
}

// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Grab the profile data from the database
if (!isset($_GET['user_id'])) {
$query = "SELECT username, first_name, last_name, gender, birthdate, weight, FROM exercise_user , exercise_log WHERE user_id = '" . $_SESSION['user_id'] . "'";
}
else {
$query = "SELECT username, first_name, last_name, gender, birthdate, weight, FROM exercise_user , exercise_log WHERE user_id = '" . $_SESSION['user_id'] . "'";
}
$data = mysqli_query($dbc, $query);

if (mysqli_num_rows($data) == 1) {
// The user row was found so display the user data
$row = mysqli_fetch_array($data);
echo '<table>';
if (!empty($row['username'])) {
echo '<tr><td class="label">Username:</td><td>' . $row['username'] . '</td></tr>';
}
if (!empty($row['first_name'])) {
echo '<tr><td class="label">First name:</td><td>' . $row['first_name'] . '</td></tr>';
}
if (!empty($row['last_name'])) {
echo '<tr><td class="label">Last name:</td><td>' . $row['last_name'] . '</td></tr>';
}
if (!empty($row['gender'])) {
echo '<tr><td class="label">Gender:</td><td>';
if ($row['gender'] == 'M') {
echo 'Male';
}
else if ($row['gender'] == 'F') {
echo 'Female';
}
else {
echo '?';
}
echo '</td></tr>';
}
if (!empty($row['birthdate'])) {
if (!isset($_GET['user_id']) || ($_SESSION['user_id'] == $_GET['user_id'])) {
// Show the user their own birthdate
echo '<tr><td class="label">Birthdate:</td><td>' . $row['birthdate'] . '</td></tr>';
}
else {
// Show only the birth year for everyone else
list($year, $month, $day) = explode('-', $row['birthdate']);
echo '<tr><td class="label">Year born:</td><td>' . $year . '</td></tr>';
}
}
if (!empty($row['weight'])) {
echo '<tr><td class="label">weight:</td><td>' . $row['weight'] . '</td></tr>';
}
if (!empty($row['picture'])) {
echo '<tr><td class="label">Picture:</td><td><img src="' . MM_UPLOADPATH . $row['picture'] .
'" alt="Profile Picture" /></td></tr>';
}
echo '</table>';
if (!isset($_GET['user_id']) || ($_SESSION['user_id'] == $_GET['user_id'])) {
echo '<p>Would you like to <a href="editprofile.php">edit your profile</a>?</p>';
}
} // End of check for a single row of user results
else {
echo '<p class="error">There was a problem accessing your profile.</p>';
}

mysqli_close($dbc);
?>
</body>
</html>


This post was edited by jsbb on Oct 19 2016 05:13pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll