Solved

problem with random number of variables

Posted on 2002-03-25
5
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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