Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with random number of variables

Posted on 2002-03-25
5
Medium Priority
?
217 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

705 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