Page 1 of 1

Singling out user data

Posted: Fri Sep 17, 2010 9:11 am
by dj262501
Hello all,

I need some help with php code. I have two pages. The first is used to get the user info and the second to post it. Here's the first page:

Code: Select all

<?php
include "include/dbc.php";

include "include/header.inc";
?>

<style type="text/css">
.mydate{
color:#00F;
text-decoration:underline;
cursor:pointer;
}
</style>

<script type="text/javascript">
function displayDate(d){
var date=new Date();
var D=date.getDate();
date.setDate(D+d);
var YYYY=date.getFullYear();
var MM=date.getMonth()+1;
MM<10?MM='0'+MM:null;
var DD=date.getDate();
DD<10?DD='0'+DD:null;
var span=document.getElementById('date');
span.innerHTML= 'Entries for '+MM+'/'+DD+'/'+YYYY;
}
onload=function(){displayDate(0)};
</script>

<h1>Food Diary</h1>

<div class="full">
<center><div><span class="mydate" onclick="displayDate(-1)"><img src="images/left_arrow.png" border="0">Yesterday</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="date" style="font-size:2em;"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="mydate" onclick="displayDate(1)">Tomorrow<img src="images/right_arrow.png" border="0"></span></div><br />
<a href="#" onclick="displayDate(0);return false;">Today</a>
</center>

<div class="full">
<form name="exercise" id="exercise" method="GET" action="">
  <center><table>
	<tr>
    	<td><h3>Add an Activity</h3></td>
	</tr>
	<tr>
    	<td><input name="NewSearchString" style="width: 100px" type="text"/>&nbsp;&nbsp;<input type="submit" value="Search" /> </td>
	</tr>
    <tr>
    	<td>
        	<select name="activity">
            	<option value="_">Activity Browse...</option>
                <option value="all">All Activities</option>
                <option value="biking">Biking</option>
                <option value="condition">Conditioning</option>
                <option value="dancing">Dancing</option>
                <option value="fish">Fishing & Hunting</option>
                <option value="Home">Home Activities</option>
                <option value="misc">Miscellaneous</option>
                <option value="music">Music Playing</option>
                <option value="occupation">Occupation</option>
                <option value="running">Running</option>
                <option value="sports">Sports</option>
                <option value="walking">Walking</option>
                <option value="water">Water Activities</option>
                <option value="winter">Winter Activities</option>
			</select> &nbsp;&nbsp;<input type="submit" value="Submit" /></td></tr></table></center></form>
		</td>
	</tr>
</table> </center>
<table width="100%">
   	<tr bgcolor="#66CC33">
    	<td><div>Activity</div></td>
        <td><div>Specific Activity</div></td>
        <td><div>Time (hh:mm)</div></td>
        <td><div>Distance</div></td>
        <td><div>Units</div></td>
	</tr>
    <tr bgcolor="#66CC33">
    	<td><div></div></td>
     	<td><div></div></td>
       <td><div></div></td>
        <td><div class="Float"></div></td>
        <td class="cp_Distance"><div></div></td>
	</tr>
<?php
if(isset($_GET[activity])) {
$category=$_GET[activity];
$result = mysql_query("SELECT * FROM exercise WHERE type='$category'");
?>
<form action="add_activity.php" method="POST">
<?php
while($row = mysql_fetch_array($result)) {
echo '<tr><td><div>'.$row[Type].'</div></td>';
echo '<td><div>'.$row[Name].'<input type="hidden" name="exerciseid[]" value="'.$row[Name].'"></div></td>';
echo '<td><div><input type="text" name="duration['.$row['Name'].']"></div></td>';
echo '<td><div><input type="text" name="distance['.$row['Name'].']"></div></td>';
echo '<td><div><select name="metric[]">
        		<option value="mile" name="mile">mile</option>
            	<option value="Km" name="Km">km</option>
            	<option value="M" name="M">m</option>
            	<option value="Yard" name="yard">yrd</option>
            	<option value="Feet" name="feet">ft</option>
            </select></div></td></tr>';

}
mysql_close();
?>
<tr><td colspan="6" align="center"><input type="submit" name="submit" value="Add Activities" onClick="return confirm(
  'Are you sure you want to submit the activities?');"></td></tr>
</form>
<?php
}
?>
    <tr bgcolor="#66CC33">
    	<td><div></div></td>
     	<td><div></div></td>
       <td><div></div></td>
        <td><div class="Float"></div></td>
        <td class="cp_Distance"><div></div></td>
	</tr></table>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
And here is the second:

Code: Select all

<?php
include "include/dbc.php";

include "include/header.inc";

$exerciseid = $_POST["exerciseid"];
$duration = $_POST["duration"];
$distance = $_POST["distance"];
$metric = $_POST["metric"];

echo'<h1>Added Activities</h1>';

// name of array
echo '<h1>Exercise</h1>';
if (is_array($exerciseid)) {
        foreach ($exerciseid as $key => $value) {
                        echo $key .' : '. $value .'<br />';
        }
}

// name of array
echo '<h1>Duration</h1>';
if (is_array($duration)) {
        foreach ($duration as $key => $value) {

                        echo $key .' : '. $value .'<br />';

        }
}

// name of array
echo '<h1>Distance</h1>';
if (is_array($distance)) {
        foreach ($distance as $key => $value) {

                        echo $key .' : '. $value .'<br />';

        }
}

// name of array
echo '<h1>Metric</h1>';
if (is_array($metric)) {
        foreach ($metric as $key => $value) {

                        echo $key .' : '. $value .'<br />';

        }
}

?>
The problem is that the output returns everything instead of what they user puts in:

[text]
Added Activities
Exercise
0 : Automobile repair
1 : Carpentry, general
2 : Carrying heavy loads, such as bricks
3 : Construction, outside, remodeling
4 : Electrical work, plumbing
5 : Farming, baling hay, cleaning barn
6 : Farming, milking by hand
7 : Farming, shoveling grain
8 : Marching, rapidly, military
9 : Painting, papering, plastering, scraping

Duration
Automobile repair : 23
Carpentry, general :
Carrying heavy loads, such as bricks :
Construction, outside, remodeling :
Electrical work, plumbing :
Farming, baling hay, cleaning barn :
Farming, milking by hand :
Farming, shoveling grain :
Marching, rapidly, military :
Painting, papering, plastering, scraping :

Distance
Automobile repair : 23
Carpentry, general :
Carrying heavy loads, such as bricks :
Construction, outside, remodeling :
Electrical work, plumbing :
Farming, baling hay, cleaning barn :
Farming, milking by hand :
Farming, shoveling grain :
Marching, rapidly, military :
Painting, papering, plastering, scraping :

Metric
0 : mile
1 : mile
2 : mile
3 : mile
4 : mile
5 : mile
6 : mile
7 : mile
8 : mile
9 : mile
[/text]

I need a way to ONLY display what the user puts in and not everything else. Any help is appreciated. Thank you

Re: Singling out user data

Posted: Sat Sep 18, 2010 10:42 am
by greatme
If you want only the current user to display, remove the loop "while" and i think you should get the desired result.
Just something like this without the while structure included.
<?php
($row = mysql_fetch_array($result))
echo '<tr><td><div>'.$row[Type].'</div></td>';
echo '<td><div>'.$row[Name].'<input type="hidden" name="exerciseid[]" value="'.$row[Name].'"></div></td>';
echo '<td><div><input type="text" name="duration['.$row['Name'].']"></div></td>';
echo '<td><div><input type="text" name="distance['.$row['Name'].']"></div></td>';
echo '<td><div><select name="metric[]">
<option value="mile" name="mile">mile</option>
<option value="Km" name="Km">km</option>
<option value="M" name="M">m</option>
<option value="Yard" name="yard">yrd</option>
<option value="Feet" name="feet">ft</option>
</select></div></td></tr>';


mysql_close();
?>

Re: Singling out user data

Posted: Mon Sep 20, 2010 12:20 pm
by dj262501
Thanks, but that's not working. It returns a syntax error:

[text]
Parse error: syntax error, unexpected T_ECHO in /home/fffinc/public_html/i/act3.php on line 92
[/text]