Solved

problem with random number of variables

Posted on 2002-03-25
5
188 Views
Last Modified: 2008-03-06
I have a bunch of variables, and the number varies depending on the number of dates the user selects (i.e. date1, date2, date3, ...). There is no limit on how many they can choose. I also have a variable 'sets' that tells me how many dates they have choosen. I'm using a for loop to add the info to a database. Why isn't $date1 the same as $date$i if $i=1? Any suggestions on how I should do this?


for($i=1; $i<=$sets; $i++){
$query = "insert into ride values ('$userid','$date$i,'$distance$i','$duration$i','$avespeed$i')";
$result = mysql_query($query);
}
0
Comment
Question by:kellih
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:andriv
ID: 6895178
It should be the same, your problem may be a syntax error. You are missing the closing sigle quote around '$date$i within the query.

('$userid','$date$i,'

If that is not it and you made the error pasting it here try the following:

for($i=1; $i<=$sets; $i++){
$datevar = $date . $i;

$query = "insert into ride values ('$userid','$datevar','$distance$i','$duration$i','$avespeed$i')";
$result = mysql_query($query);
}
0
 

Author Comment

by:kellih
ID: 6895247
That missing quote was just a pasting error.

As for your other suggestion, I don't think that will work because $date does not exist and wouldn't $date.$i=(whatever $i is).

The script I have created allows someone to enter the date, how many miles they biked, speed, comments, etc. They can then view a graph of their progress, or edit/view previous dates. It's with editing previous dates that I'm having a problem. The user is not limited to a set number of dates to look at and update. Some may want to view/update 7, others may want to view/update 30. If someone selected the dates 03/23/2002 - 03/25/2002, they would have selected 3 dates and the variables are as follows:

$date1="2002-03-23"
$distance1="25"
$date2="2002-03-24"
$distance2="28"
$date3="2002-03-25"
$distance2="27"
$set="3"

for($i=1; $i<=$sets; $i++){
$query = "insert into ride values ('$userid','$date$i','$distance$i','$duration$i','$avespeed$i')";
$result = mysql_query($query);
}

If I use the for loop above, it inputs 1 for $date$i when $i=1 and 2 for $date$i when $i=2 and so on since $date equals nothing. Is there another way to write this so that it is understood as $date1, $date2, $date3?
0
 
LVL 5

Accepted Solution

by:
andriv earned 50 total points
ID: 6895260
Got you, the easiest way is when you are assigning the values put them in a array, if the variable name is comming from the form you can do it as such:

<input type=text name="date[]">

then all you need is to use the foreach loop to loop through each one.


Or you can do this:

for($i=1; $i<=$sets; $i++){
$datevar = date . $i;

$query = "insert into ride values ('$userid','{$$datevar}','$distance$i','$duration$i','$avespeed$i')";
0
 

Author Comment

by:kellih
ID: 6896637
<input type=text name="date[]">

This worked great. Thanks!
0
 
LVL 5

Expert Comment

by:andriv
ID: 6898196
No problem, glad I can help
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now