Solved

accessing dynamic var

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now