Solved

accessing dynamic var

Posted on 2004-04-07
5
189 Views
Last Modified: 2006-11-17
in a form i use this code

// $numbOpt is set else where for this lets say $numbOpt = 5
$num=0;
while($num < $numbOpt) {
$num++;
print'
input name="opt$num" type="text">';
}      

in the next page is use this code which doesn't work

while($numbOpt > 0){
$Opt = "$opt".$numbOpt;
$query = "insert into PollOptions"
." ( `id` , `Qnumb` , `option` ) values "
."('','$Qid','$Opt')";
mysql_query($query);              
$numbOpt--;
}  

does anyone know how to do this, i can't figure it out. thanks

0
Comment
Question by:ocsurf
5 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 10779733
Try this:


while($numbOpt > 0){
$Opt = "$opt".$numbOpt;
$query = "insert into PollOptions('id','Qnumb','option') values ('','$Qid','$Opt')";
mysql_query($query);            
$numbOpt--;
}  
0
 
LVL 1

Accepted Solution

by:
ocsurf earned 0 total points
ID: 10779800
you just reposted my code that was cleaned up.

I figured it out  myself

I will post the code for everyone else:

            foreach($_POST as $key => $value)
            {
               if (strpos($key, "opt") === false) {
               }else{
                  $query = "insert into PollOptions"
                  ." ( `id` , `Qnumb` , `option` ) values "
                  ."('','$Qid','$value')";
                  mysql_query($query);                
               }
            }
0
 
LVL 19

Expert Comment

by:Michael701
ID: 10780400
i think you could have also used

$Opt = "$opt".$numbOpt;

echo $Opt.'='.$$Opt;
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

726 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