Solved

problem with random number of variables

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
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.

627 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