Solved

accessing dynamic var

Posted on 2004-04-07
5
188 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
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.

809 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