Solved

problem with random number of variables

Posted on 2002-03-25
5
169 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
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 …

706 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

19 Experts available now in Live!

Get 1:1 Help Now